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..55727c0c --- /dev/null +++ b/lib/qonfig/commands/definition/load_from_file.rb @@ -0,0 +1,72 @@ +# 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 [Symbol] + # + # @api private + # @since 0.24.0 + SELF_FILE_PATH = :self + + # @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: 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] + # @return [void] + # + # @api private + # @since 0.24.0 + 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 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]