Class: SimpleEnum::EnumHash
- Inherits:
-
Hash
- Object
- Hash
- SimpleEnum::EnumHash
- Defined in:
- lib/simple_enum/enum_hash.rb
Overview
Internal hash class, used to handle the enumerations et al. Works like to original Hash class, but with some added value, like access to
Instance Method Summary collapse
- #default(k = nil) ⇒ Object
-
#initialize(hsh) ⇒ EnumHash
constructor
A new instance of EnumHash.
- #method_missing(symbol, *args) ⇒ Object
Constructor Details
#initialize(hsh) ⇒ EnumHash
Returns a new instance of EnumHash.
9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/simple_enum/enum_hash.rb', line 9 def initialize(hsh) hsh = hsh.to_hash_magic unless hsh.is_a?(Hash) @reverse_sym_lookup = {} @sym_value_lookup = {} hsh.each do |k,v| sym = k.to_enum_sym self[k] = v @reverse_sym_lookup[sym] = k @sym_value_lookup[sym] = v end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(symbol, *args) ⇒ Object
27 28 29 30 31 32 33 34 |
# File 'lib/simple_enum/enum_hash.rb', line 27 def method_missing(symbol, *args) if @sym_value_lookup.has_key?(symbol.to_enum_sym) return @reverse_sym_lookup[symbol.to_enum_sym] if args.first self[symbol] else super end end |
Instance Method Details
#default(k = nil) ⇒ Object
23 24 25 |
# File 'lib/simple_enum/enum_hash.rb', line 23 def default(k = nil) @sym_value_lookup[k.to_enum_sym] if k end |