Class: Eldr::Configuration
- Inherits:
-
Object
- Object
- Eldr::Configuration
- Defined in:
- lib/eldr/configuration.rb
Overview
This a OpenStruct like class. It wraps a hash and when you hit a method that doesn’t exist it tries to pull it from the hash Probably can break in 100 ways an is dumb, but it works. And it performs astromnomically better than stuff like Hashie::Mash
Instance Attribute Summary collapse
-
#table ⇒ Object
Returns the value of attribute table.
Instance Method Summary collapse
-
#initialize ⇒ Configuration
constructor
A new instance of Configuration.
- #merge!(hash) ⇒ Object
- #method_missing(method, *args) ⇒ Object
- #set(key, value) ⇒ Object
Constructor Details
#initialize ⇒ Configuration
Returns a new instance of Configuration.
11 12 13 14 |
# File 'lib/eldr/configuration.rb', line 11 def initialize defaults = { lock: false } table.merge!(defaults) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args) ⇒ Object
29 30 31 32 33 34 35 |
# File 'lib/eldr/configuration.rb', line 29 def method_missing(method, *args) if !args.empty? # we assume we are setting something set(method.to_s.gsub(/\=$/, '').to_sym, args.pop) else @table[method.to_s.gsub(/\?$/, '').to_sym] end end |
Instance Attribute Details
#table ⇒ Object
Returns the value of attribute table.
9 10 11 |
# File 'lib/eldr/configuration.rb', line 9 def table @table end |
Instance Method Details
#merge!(hash) ⇒ Object
24 25 26 27 |
# File 'lib/eldr/configuration.rb', line 24 def merge!(hash) hash = hash.table unless hash.is_a? Hash @table.merge!(hash) end |
#set(key, value) ⇒ Object
16 17 18 |
# File 'lib/eldr/configuration.rb', line 16 def set(key, value) @table[key] = value end |