Class: Templar::Config
- Inherits:
-
Object
- Object
- Templar::Config
- Defined in:
- lib/templar/config.rb
Overview
Note:
would like to use Ribbon, but it only supports 1.9.x from what I’ve seen.
Basic, easy-to-use configuration object.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
-
#initialize(data = { }) ⇒ Config
constructor
A new instance of Config.
- #method_missing(sym, *args) ⇒ Object
- #to_hash ⇒ Object
- #update!(data) ⇒ Object
Constructor Details
#initialize(data = { }) ⇒ Config
Returns a new instance of Config.
9 10 11 12 |
# File 'lib/templar/config.rb', line 9 def initialize(data={ }) @data = { } update!(data) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(sym, *args) ⇒ Object
31 32 33 34 35 36 37 |
# File 'lib/templar/config.rb', line 31 def method_missing(sym, *args) if sym.to_s =~ /(.+)=$/ self[$1] = args.first else self[sym] end end |
Instance Method Details
#[](key) ⇒ Object
14 15 16 |
# File 'lib/templar/config.rb', line 14 def [](key) @data[key.to_sym] end |
#[]=(key, value) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/templar/config.rb', line 18 def []=(key, value) if value.class == Hash @data[key.to_sym] = Config.new(value) elsif value.class == Array @data[key.to_sym] = [] value.each do |v| @data[key.to_sym] << v end else @data[key.to_sym] = value end end |
#to_hash ⇒ Object
45 46 47 |
# File 'lib/templar/config.rb', line 45 def to_hash @data end |
#update!(data) ⇒ Object
39 40 41 42 43 |
# File 'lib/templar/config.rb', line 39 def update!(data) data.each do |key, value| self[key] = value end end |