From fbe844612ca4a434cb2f79163ec8bef15b0b6adc Mon Sep 17 00:00:00 2001 From: Rustam Ibragimov Date: Thu, 12 Dec 2019 15:01:16 +0300 Subject: [PATCH 1/2] [dsl/load-from-file/expose-file] Skeleton --- lib/qonfig/commands/definition.rb | 2 + lib/qonfig/commands/definition/expose_file.rb | 52 +++++++++++++++++++ .../commands/definition/load_from_file.rb | 43 +++++++++++++++ lib/qonfig/dsl.rb | 28 ++++++++++ 4 files changed, 125 insertions(+) create mode 100644 lib/qonfig/commands/definition/expose_file.rb create mode 100644 lib/qonfig/commands/definition/load_from_file.rb diff --git a/lib/qonfig/commands/definition.rb b/lib/qonfig/commands/definition.rb index 631bcc25..a23b53ef 100644 --- a/lib/qonfig/commands/definition.rb +++ b/lib/qonfig/commands/definition.rb @@ -14,4 +14,6 @@ module Qonfig::Commands::Definition require_relative 'definition/expose_yaml' require_relative 'definition/expose_json' require_relative 'definition/expose_self' + require_relative 'definition/expose_file' + require_relative 'definition/load_from_file' end diff --git a/lib/qonfig/commands/definition/expose_file.rb b/lib/qonfig/commands/definition/expose_file.rb new file mode 100644 index 00000000..ff2fa254 --- /dev/null +++ b/lib/qonfig/commands/definition/expose_file.rb @@ -0,0 +1,52 @@ +# frozen_string_literal: true + +# @api private +# @since 0.24.0 +class Qonfig::Commands::Definition::ExposeFile < Qonfig::Commands::Base + # @since 0.24.0 + self.inheritable = true + + # @return [String, Pathname] + # + # @api private + # @since 0.24.0 + attr_reader :file_path + + # @return [String, Symbol] + # + # @api private + # @since 0.24.0 + attr_reader :env + + # @return [Symbol] + # + # @api private + # @since 0.24.0 + attr_reader :via + + # @return [Boolean] + # + # @api private + # @since 0.24.0 + attr_reader :strict + + # @param file_path [String, Pathname] + # @option env [String, Symbol] + # @option via [Symbol] + # @option strict [Boolean] + # @option format [String, Symbol] + # @return [void] + # + # @api private + # @since 0.24.0 + def initialize(file_path, env:, via:, strict:, format:) + end + + # @param data_set [Qonfig::DataSet] + # @param settings [Qonfig::Settings] + # @return [void] + # + # @api private + # @since 0.24.0 + def call(data_set, settings); end +end diff --git a/lib/qonfig/commands/definition/load_from_file.rb b/lib/qonfig/commands/definition/load_from_file.rb new file mode 100644 index 00000000..040f7ce9 --- /dev/null +++ b/lib/qonfig/commands/definition/load_from_file.rb @@ -0,0 +1,43 @@ +# frozen_string_literal: true + +# @api private +# @since 0.24.0 +class Qonfig::Commands::Definition::LoadFromFile < Qonfig::Commands::Base + # @since 0.24.0 + self.inheritable = true + + # @return [String, Pathname] + # + # @api private + # @since 0.24.0 + attr_reader :file_path + + # @return [Boolean] + # + # @api private + # @since 0.24.0 + attr_reader :strict + + # @return [String, Symbol] + # + # @api private + # @since 0.24.0 + attr_reader :format + + # @param file_path [String, Pathname] + # @option strcit [Boolean] + # @option format [String, Symbol] + # @return [void] + # + # @api private + # @since 0.24.0 + def initialize(file_path, strict:, format:); end + + # @param data_set [Qonfig::DataSet] + # @param settings [Qonfig::Settings] + # @return [void] + # + # @api private + # @since 0.24.0 + def call(data_set, settings); end +end diff --git a/lib/qonfig/dsl.rb b/lib/qonfig/dsl.rb index 54d8e183..4f0f0e8f 100644 --- a/lib/qonfig/dsl.rb +++ b/lib/qonfig/dsl.rb @@ -212,6 +212,19 @@ def load_from_json(file_path, strict: true) definition_commands << Qonfig::Commands::Definition::LoadFromJSON.new(file_path, strict: strict) end + # @option file_path [String, Pathname] + # @option strict [Boolean] + # @option format [Symbol, String] + # @return [void] + # + # @api public + # @version 0.24.0 + def load_from_file(file_path, strict: true, format: :dynamic) + definition_commands << Qonfig::Commands::Definition::LoadFroMfile.new( + file_path, strict: strict, format: format + ) + end + # @param file_path [String, Pathname] # @option strict [Boolean] # @option via [Symbol] @@ -261,6 +274,21 @@ def expose_self(env:, format: :dynamic) ) end + # @option [String, Pathname] + # @option via [Symbol] + # @option env [Symbol, String] + # @option strict [Boolean] + # @option format [Symbol, String] + # @return [void] + # + # @api public + # @version 0.24.0 + def expose_file(file_path, via:, env:, strict: true, format: :dynamic) + definition_commands << Qonfig::Commands::Definition::ExposeFile.new( + file_path, strict: strict, via: via, env: env, format: format + ) + end + # @param file_path [String, Pathname] # @option format [String, Symbol] # @option strict [Boolean] From 2693d53c38eafca2dff1d9a0a87dbf4c41d93028 Mon Sep 17 00:00:00 2001 From: Rustam Ibragimov Date: Sat, 14 Dec 2019 15:56:38 +0300 Subject: [PATCH 2/2] [DSL/load-from-file/expose-file] Skeleton updates --- .../commands/definition/load_from_file.rb | 33 +++++++++++++++++-- .../commands/definition/load_from_self.rb | 2 +- .../commands/instantiation/values_file.rb | 6 ++-- 3 files changed, 36 insertions(+), 5 deletions(-) diff --git a/lib/qonfig/commands/definition/load_from_file.rb b/lib/qonfig/commands/definition/load_from_file.rb index 040f7ce9..55727c0c 100644 --- a/lib/qonfig/commands/definition/load_from_file.rb +++ b/lib/qonfig/commands/definition/load_from_file.rb @@ -6,6 +6,12 @@ class Qonfig::Commands::Definition::LoadFromFile < Qonfig::Commands::Base # @since 0.24.0 self.inheritable = true + # @return [Symbol] + # + # @api private + # @since 0.24.0 + SELF_FILE_PATH = :self + # @return [String, Pathname] # # @api private @@ -31,7 +37,23 @@ class Qonfig::Commands::Definition::LoadFromFile < Qonfig::Commands::Base # # @api private # @since 0.24.0 - def initialize(file_path, strict:, format:); end + def initialize(file_path, strict: true, format: :dynamic) + unless format.is_a?(String) || format.is_a?(Symbol) + raise Qonfig::ArgumentError, 'Formad should be a type of string or symbol' + end + + unless file_path.is_a?(String) || file_path.is_a?(Pathname) || file_path == SELF_FILE_PATH + raise Qonfig::ArgumentError, 'Incorrect file path' + end + + unless strict.is_a?(TrueClass) || strict.is_a?(FalseClass) + raise Qonfig::ArgumentError, ':strict should be a type of boolean' + end + + @file_path = file_path + @strict = strict + @format = format.tap { Qonfig::Loaders.resolve(format) } + end # @param data_set [Qonfig::DataSet] # @param settings [Qonfig::Settings] @@ -39,5 +61,12 @@ def initialize(file_path, strict:, format:); end # # @api private # @since 0.24.0 - def call(data_set, settings); end + def call(data_set, settings) + settings_data = Qonfig::Loaders.resolve(format).load_file(file_path, fail_on_unexist: strict) + + raise( + Qonfig::IncompatibleDataStructureError, + 'Setting values should be represented as a hash-like structure' + ) unless settings_data.is_a?(Hash) + end end diff --git a/lib/qonfig/commands/definition/load_from_self.rb b/lib/qonfig/commands/definition/load_from_self.rb index c20791bb..1a3da1fd 100644 --- a/lib/qonfig/commands/definition/load_from_self.rb +++ b/lib/qonfig/commands/definition/load_from_self.rb @@ -25,7 +25,7 @@ class Qonfig::Commands::Definition::LoadFromSelf < Qonfig::Commands::Base # @since 0.2.0 def initialize(caller_location, format:) unless format.is_a?(String) || format.is_a?(Symbol) - raise Qonfig::ArgumentError, 'Format should be a symbol or a string' + raise Qonfig::ArgumentError, 'Format should be a type of string or symbol' end @caller_location = caller_location diff --git a/lib/qonfig/commands/instantiation/values_file.rb b/lib/qonfig/commands/instantiation/values_file.rb index 6ac83420..5a5cb0ad 100644 --- a/lib/qonfig/commands/instantiation/values_file.rb +++ b/lib/qonfig/commands/instantiation/values_file.rb @@ -116,11 +116,12 @@ def load_settings_values # # @api private # @since 0.17.0 + # @version 0.24.0 def load_from_file Qonfig::Loaders.resolve(format).load_file(file_path, fail_on_unexist: strict).tap do |values| raise( Qonfig::IncompatibleDataStructureError, - 'Setting values must be a hash-like structure' + 'Setting values should be represented as a hash-like structure' ) unless values.is_a?(Hash) end end @@ -129,13 +130,14 @@ def load_from_file # # @api private # @since 0.17.0 + # @version 0.24.0 def load_from_self end_data = Qonfig::Loaders::EndData.extract(caller_location) Qonfig::Loaders.resolve(format).load(end_data).tap do |values| raise( Qonfig::IncompatibleDataStructureError, - 'Setting values must be a hash-like structure' + 'Setting values should be represented as a hash-like structure' ) unless values.is_a?(Hash) end rescue Qonfig::SelfDataNotFoundError => error