Class: SemiOpenStruct
- Inherits:
-
OpenStruct
- Object
- OpenStruct
- SemiOpenStruct
- Defined in:
- lib/easyconf-rails.rb
Overview
SemiOpenStruct is like OpenStruct, except that accessing an uninitialized key will raise a KeyError.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Return the value corresponding to
key.to_sym
, or raiseKeyError
if no such value exists. -
#[]=(key, val) ⇒ Object
Set
key.to_sym
toval
. -
#each ⇒ Object
Call the given block for every key and value we have.
-
#has_key?(key) ⇒ Boolean
Return true if the given key is present.
-
#keys ⇒ Object
Return a new array populated with our keys.
- #method_missing(key, *val) ⇒ Object
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(key, *val) ⇒ Object
34 35 36 37 38 39 40 |
# File 'lib/easyconf-rails.rb', line 34 def method_missing(key, *val) if key.to_s.end_with?('=') and (val.length == 1) super(key, val.first) else raise(NameError, "undefined method `#{key}' for #{self}") end end |
Instance Method Details
#[](key) ⇒ Object
Return the value corresponding to key.to_sym
, or raise KeyError
if no such value exists.
8 9 10 |
# File 'lib/easyconf-rails.rb', line 8 def [](key) @table.fetch(key.to_sym) end |
#[]=(key, val) ⇒ Object
Set key.to_sym
to val
.
13 14 15 |
# File 'lib/easyconf-rails.rb', line 13 def []=(key, val) @table[key.to_sym] = val end |
#each ⇒ Object
Call the given block for every key and value we have.
28 29 30 31 32 |
# File 'lib/easyconf-rails.rb', line 28 def each # :yields: key, val @table.each do |key, val| yield(key, val) end end |
#has_key?(key) ⇒ Boolean
Return true if the given key is present.
18 19 20 |
# File 'lib/easyconf-rails.rb', line 18 def has_key?(key) singleton_methods.grep(/[^=]$/).include?(key) end |
#keys ⇒ Object
Return a new array populated with our keys.
23 24 25 |
# File 'lib/easyconf-rails.rb', line 23 def keys @table.keys end |