lib/qonfig/data_set.rb in qonfig-0.16.0 vs lib/qonfig/data_set.rb in qonfig-0.17.0

- old
+ new

@@ -11,23 +11,28 @@ # @since 0.13.0 extend Qonfig::Validator::DSL class << self + # @param base_dataset_klass [Class<Qonfig::DataSet>] # @param config_klass_definitions [Proc] # @return [Qonfig::DataSet] # # @api public # @since 0.16.0 - def build(&config_klass_definitions) - Class.new(self, &config_klass_definitions).new + def build(base_dataset_klass = self, &config_klass_definitions) + unless base_dataset_klass <= Qonfig::DataSet + raise(Qonfig::ArgumentError, 'Base inherited class should be a type of Qonfig::DataSet') + end + + Class.new(base_dataset_klass, &config_klass_definitions).new end end # @return [Qonfig::Settings] # - # @api private + # @api public # @since 0.1.0 attr_reader :settings # @param settings_map [Hash] # @param configurations [Proc] @@ -68,10 +73,69 @@ raise Qonfig::FrozenSettingsError, 'Frozen config can not be reloaded' if frozen? load!(settings_map, &configurations) end end + # @param file_path [String, Symbol] + # @option format [String, Symbol] + # @option strict [Boolean] + # @option expose [NilClass, String, Symbol] Environment key + # @return [void] + # + # @see Qonfig::DataSet#load_setting_values_from_file + # + # @api public + # @since 0.17.0 + def load_from_file(file_path, format: :dynamic, strict: true, expose: nil) + thread_safe_access do + load_setting_values_from_file(file_path, format: format, strict: strict, expose: expose) + end + end + + # @param file_path [String] + # @option strict [Boolean] + # @option expose [NilClass, String, Symbol] Environment key + # @return [void] + # + # @see Qonfig::DataSet#load_from_file + # + # @api public + # @since 0.17.0 + def load_from_yaml(file_path, strict: true, expose: nil) + load_from_file(file_path, format: :yml, strict: strict, expose: expose) + end + + # @param file_path [String] + # @option strict [Boolean] + # @option expose [NilClass, String, Symbol] Environment key + # @return [void] + # + # @see Qonfig::DataSet#load_from_file + # + # @api public + # @since 0.17.0 + def load_from_json(file_path, strict: true, expose: nil) + load_from_file(file_path, format: :json, strict: strict, expose: expose) + end + + # @option format [String, Symbol] + # @option strict [Boolean] + # @option expose [NilClass, String, Symbol] + # @return [void] + # + # @api public + # @since 0.17.0 + def load_from_self(format: :dynamic, strict: true, expose: nil) + caller_location = caller(1, 1).first + + thread_safe_access do + load_setting_values_from_file( + :self, format: format, strict: strict, expose: expose, caller_location: caller_location + ) + end + end + # @param settings_map [Hash] # @return [void] # # @api public # @since 0.1.0 @@ -182,10 +246,21 @@ # @since 0.16.0 def subset(*keys) thread_safe_access { settings.__subset__(*keys) } end + # @param key_path [Array<String, Symbol>] + # @return [Boolean] + # + # @api public + # @since 0.17.0 + def key?(*key_path) + thread_safe_access { settings.__has_key__(*key_path) } + end + alias_method :option?, :key? + alias_method :setting?, :key? + # @return [void] # # @api public # @since 0.2.0 def clear! @@ -232,10 +307,45 @@ # @since 0.13.0 def validate! thread_safe_access { validator.validate! } end + # @param temporary_configurations [Hash<Symbol|String,Any>] + # @param arbitary_code [Block] + # @return [void] + # + # @api public + # @since 0.17.0 + def with(temporary_configurations = {}, &arbitary_code) + with_arbitary_access do + begin + original_settings = @settings + + temporary_settings = self.class.build.dup.tap do |copied_config| + copied_config.configure(temporary_configurations) + end.settings + + @settings = temporary_settings + yield if block_given? + ensure + @settings = original_settings + end + end + end + + # @return [Qonfig::DataSet] + # + # @api public + # @since 0.17.0 + def dup + thread_safe_definition do + self.class.build.tap do |duplicate| + duplicate.configure(to_h) + end + end + end + private # @return [Qonfig::Validator] # # @api private @@ -268,22 +378,56 @@ def apply_settings(settings_map = {}, &configurations) settings.__apply_values__(settings_map) yield(settings) if block_given? end + # @return [void] + # + # @api private + # @since 0.17.0 + def call_instance_management_commands + self.class.instance_commands.each do |instance_command| + instance_command.call(self, settings) + end + end + # @param settings_map [Hash] # @param configurations [Proc] # @return [void] # # @api private # @since 0.2.0 def load!(settings_map = {}, &configurations) build_validator build_settings + call_instance_management_commands apply_settings(settings_map, &configurations) end + # @param file_path [String, Symbol] + # @option format [String, Symbol] + # @option strict [Boolean] + # @option expose [NilClass, String, Symbol] + # @option callcer_location [NilClass, String] + # @return [void] + # + # @see Qonfig::Commands::Instantiation::ValuesFile + # + # @api private + # @since 0.17.0 + def load_setting_values_from_file( + file_path, + format: :dynamic, + strict: true, + expose: nil, + caller_location: nil + ) + Qonfig::Commands::Instantiation::ValuesFile.new( + file_path, caller_location, format: format, strict: strict, expose: expose + ).call(self, settings) + end + # @param instructions [Proc] # @return [Object] # # @api private # @since 0.2.0 @@ -296,7 +440,11 @@ # # @api private # @since 0.2.0 def thread_safe_definition(&instructions) @__lock__.thread_safe_definition(&instructions) + end + + def with_arbitary_access(&instructions) + @__lock__.with_arbitary_access(&instructions) end end