Class: Omniconf::Configuration
- Inherits:
-
BlankSlate
- Object
- BlankSlate
- Omniconf::Configuration
- Defined in:
- lib/omniconf/configuration.rb
Constant Summary
Constants inherited from BlankSlate
Instance Method Summary collapse
- #get_or_default(key, default) ⇒ Object
-
#initialize(adapter, table = {}, parent = nil) ⇒ Configuration
constructor
XXX: Every method defined in this class overrides config value accessors, hence it inherits from BlankSlate (and the gross ‘method_missing’).
- #inspect ⇒ Object
- #method_missing(method, *args) ⇒ Object
-
#to_hash ⇒ Object
Helpful helper which returns the object as a hash.
Constructor Details
#initialize(adapter, table = {}, parent = nil) ⇒ Configuration
XXX: Every method defined in this class overrides config value accessors, hence it inherits from BlankSlate (and the gross ‘method_missing’)
8 9 10 11 |
# File 'lib/omniconf/configuration.rb', line 8 def initialize(adapter, table = {}, parent = nil) @__adapter, @__parent = adapter, parent @__table = table.recursive_stringify_keys! end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/omniconf/configuration.rb', line 28 def method_missing(method, *args) raise NoMethodError, "undefined method `#{method}' for #{self}" \ if method.to_s.start_with? '__' # will save hours tracking down heisenbugs len = args.length if key = method.to_s.chomp!('=') # write unless len == 1 # args[0] is the value raise ArgumentError, "wrong number of arguments (#{len} for 1)" end if @__adapter # should not be nil expect for testing purposes # update the actual source data (e.g. does the SQL query) full_key, parent = [key], @__parent while parent # build the full nested key from parents full_key = full_key.unshift parent[:root] parent = parent[:object].__parent end @__adapter.set_value(full_key, args[0]) # notify the adapter else Omniconf.logger.warn "No adapter to notify" end @__table[key] = args[0] # update our internal config hash if @__adapter.is_a? Omniconf::Adapter::Base # we need to merge the global config Omniconf.merge_configuration! @__adapter.source_id end elsif len == 0 # read key = method.to_s value = @__table[key] if value.is_a?(Hash) Configuration.new(@__adapter, value, {:root => key, :object => self}) else value end else raise NoMethodError, "undefined method `#{method}' for #{self}" end end |
Instance Method Details
#get_or_default(key, default) ⇒ Object
22 23 24 25 26 |
# File 'lib/omniconf/configuration.rb', line 22 def get_or_default(key, default) raise "key cannot be nested" if key.include? '.' __send__ :"#{key}=", default unless value = __send__(:"#{key}") value || default end |
#inspect ⇒ Object
18 19 20 |
# File 'lib/omniconf/configuration.rb', line 18 def inspect @__table.inspect end |
#to_hash ⇒ Object
Helpful helper which returns the object as a hash
14 15 16 |
# File 'lib/omniconf/configuration.rb', line 14 def to_hash @__table end |