Class: Vapir::YamlConfiguration

Inherits:
HashConfigurationWithOtherStuff show all
Defined in:
lib/vapir-common/config.rb

Instance Attribute Summary collapse

Attributes inherited from HashConfigurationWithOtherStuff

#key_prefix

Attributes inherited from Configuration

#parent

Instance Method Summary collapse

Methods inherited from HashConfigurationWithOtherStuff

#unprefixed_recognized_key, #update_from_source

Methods inherited from Configuration

#[], #[]=, #create, #create_update, #define_key_methods, #defined_hash, #defined_key?, #defined_keys, #delete, #locally_defined_key?, #method_missing, #read, #recognize_key!, #recognized_key?, #recognized_keys, #update, #update_hash, #validate_key_format!, #with_config

Constructor Details

#initialize(parent, yaml_file) ⇒ YamlConfiguration

Returns a new instance of YamlConfiguration.



297
298
299
300
301
# File 'lib/vapir-common/config.rb', line 297

def initialize(parent, yaml_file)
  super(parent)
  @yaml_file=yaml_file
  update_from_source
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Vapir::Configuration

Instance Attribute Details

#yaml_fileObject (readonly)

Returns the value of attribute yaml_file.



296
297
298
# File 'lib/vapir-common/config.rb', line 296

def yaml_file
  @yaml_file
end

Instance Method Details

#config_hashObject



302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# File 'lib/vapir-common/config.rb', line 302

def config_hash
  if yaml_file && File.exists?(yaml_file)
    require 'yaml'
    @loaded_yaml ||= YAML.load_file(yaml_file) # don't want to reload this every time #update_from_source is called 
    if @loaded_yaml.is_a?(Hash)
      @loaded_yaml
    elsif @loaded_yaml == false || @loaded_yaml == nil
      {}
    else
      raise ArgumentError, "Attempted to load Vapir configuration from the YAML file at #{yaml_file.inspect}, but its contents parsed as a #{@loaded_yaml.class}; expected a hash.\nYAML.load_file(#{yaml_file.inspect}) = #{@loaded_yaml.inspect}"
    end
  else
    {}
  end
end