Class: DeadSimpleConf::ConfigBlock

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ ConfigBlock

Returns a new instance of ConfigBlock.



24
25
26
# File 'lib/dead_simple_conf.rb', line 24

def initialize(attributes)
  _load_data(attributes)
end

Class Method Details

.sub_array(name, klass) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/dead_simple_conf.rb', line 15

def self.sub_array(name, klass)
  attr_reader(name)
  
  define_method("#{name}=") do |arr|
    res = arr.map{|itm| klass.new(itm) }
    instance_variable_set("@#{name}", res)
  end
end

.sub_section(name, klass) ⇒ Object



7
8
9
10
11
12
13
# File 'lib/dead_simple_conf.rb', line 7

def self.sub_section(name, klass)
  attr_reader(name)
  
  define_method("#{name}=") do |h|
    instance_variable_set("@#{name}", klass.new(h))
  end
end

Instance Method Details

#_load_data(attributes) ⇒ Object



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

def _load_data(attributes)
  unknown = attributes.reject do |key, value|
    if respond_to?("#{key}=")
      send("#{key}=", value)
      true
    else
      false
    end
  end

  unless unknown.empty?
    fail "unknown parameter(s) in config block #{self.class}: #{unknown.inspect}"
  end
end