Class: Section
Overview
A Configuration consists of one or more Sections. A section is a hash-like object that responds to all keys in the hash as if they were methods:
> s = Section.from_hash({:v1 => 2, :nested => {:v2 => 1}})
> s.v1
=> 2
> s.nested.v2
=> 1
Direct Known Subclasses
Class Method Summary collapse
-
.from_hash(hsh) ⇒ Section
Create a new section from the given hash-like object.
Instance Method Summary collapse
-
#mixin(value) ⇒ void
Mixin a configuration snippet into the current section.
Methods inherited from Hash
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args) ⇒ Object
Build the call chain including NilSections.
72 73 74 75 76 77 78 79 80 81 |
# File 'lib/madvertise/ext/config.rb', line 72 def method_missing(name, *args) if name.to_s =~ /(.*)=$/ self[$1.to_sym] = Section.from_value(args.first) else value = self[name] value = value.call if value.is_a?(Proc) value = NilSection.new if value.nil? self[name] = value end end |
Class Method Details
.from_hash(hsh) ⇒ Section
Create a new section from the given hash-like object.
21 22 23 24 25 26 27 |
# File 'lib/madvertise/ext/config.rb', line 21 def from_hash(hsh) result = new.tap do |result| hsh.each do |key, value| result[key.to_sym] = from_value(value) end end end |
Instance Method Details
#mixin(value) ⇒ void
This method returns an undefined value.
Mixin a configuration snippet into the current section.
54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/madvertise/ext/config.rb', line 54 def mixin(value) unless value.is_a?(Hash) value = Section.from_hash(YAML.load(File.read(value))) end self.deep_merge!(value[:default]) if value.has_key?(:default) self.deep_merge!(value[:generic]) if value.has_key?(:generic) if value.has_key?(@mode) self.deep_merge!(value[@mode]) else self.deep_merge!(value) end end |