Class: CukeFig::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/cuke_fig.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

Returns a new instance of Config.



10
11
12
# File 'lib/cuke_fig.rb', line 10

def initialize
  @methods ||= []
end

Instance Attribute Details

#global_configObject (readonly)

Returns the value of attribute global_config.



4
5
6
# File 'lib/cuke_fig.rb', line 4

def global_config
  @global_config
end

#methodsObject (readonly)

Returns the value of attribute methods.



4
5
6
# File 'lib/cuke_fig.rb', line 4

def methods
  @methods
end

Class Method Details

.getObject



6
7
8
# File 'lib/cuke_fig.rb', line 6

def self.get
  @global_config ||= Config.new
end

Instance Method Details

#clearObject



14
15
16
# File 'lib/cuke_fig.rb', line 14

def clear
  @global_config = Config.new
end

#load(conf_files) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/cuke_fig.rb', line 18

def load(conf_files)
  conf_files = [conf_files] unless conf_files.is_a? Array
  conf_files.each do |conf|
    attr_name = File.basename(conf, '.yml').gsub('-', '_')
    attr_value = YAML.load_file conf
    set attr_name, attr_value
  end
end

#set(attr_name, attr_value) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/cuke_fig.rb', line 27

def set(attr_name, attr_value)
  var_name = ('@' + attr_name.to_s).to_sym
  if instance_variable_defined?(var_name) && attr_value.is_a?(Hash)
    # Get and merge. Keep merging if hash values are hashes
    merger = proc { |_key, v1, v2| v1.is_a?(Hash) && v2.is_a?(Hash) ? v1.merge(v2, &merger) : (v2.nil? ? v1 : v2) }
    instance_variable_get(var_name).merge! attr_value, &merger
  else
    # Create
    instance_variable_set(var_name, attr_value)
    singleton_class.class_eval { attr_reader attr_name }
    @methods << attr_name.to_s
  end
end