Class: RegApi2::SymHash
- Inherits:
-
Hash
- Object
- Hash
- RegApi2::SymHash
- Defined in:
- lib/reg_api2/sym_hash.rb
Overview
Hash with indifferent access to its elements. Also have no difference between String ans Symbol keys.
Class Method Summary (collapse)
-
+ (Object) from(source)
Forms data with indifferent access from specified source.
Instance Method Summary (collapse)
-
- (Object) [](key)
Element Reference — Retrieves the value object corresponding to the key object.
-
- (Object) []=(key, new_value)
Element Assignment — Associates the value given by value with the key given by key.
-
- (Boolean) has_key?(key)
Returns true if the given key is present in hsh.
-
- (Boolean) include?(key)
Returns true if the given key is present in hsh.
-
- (Object) method_missing(key, *args, &block)
Sets or gets field in the hash.
-
- (Boolean) respond_to?(key)
Always true.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
- (Object) method_missing(key, *args, &block)
Sets or gets field in the hash.
52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/reg_api2/sym_hash.rb', line 52 def method_missing(key, *args, &block) if key.to_s =~ /\A(.+)=\z/ self[$1] = args.first return args.first end if key.to_s =~ /\A(.+)\?\z/ return !!self[$1] end return self[key] if has_key?(key) nil end |
Class Method Details
+ (Object) from(source)
Forms data with indifferent access from specified source.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/reg_api2/sym_hash.rb', line 9 def self.from(source) case source when Hash res = SymHash.new source.each_pair do |key, value| res[key] = self.from(value) end res when Array source.map do |el| self.from(el) end else source end end |
Instance Method Details
- (Object) [](key)
Element Reference — Retrieves the value object corresponding to the key object. If not found, returns the default value (see Hash::new for details).
37 38 39 |
# File 'lib/reg_api2/sym_hash.rb', line 37 def [](key) key.kind_of?(Symbol) ? self[key.to_s] : super(key) end |
- (Object) []=(key, new_value)
Element Assignment — Associates the value given by value with the key given by key. key should not have its value changed while it is in use as a key (a String passed as a key will be duplicated and frozen).
42 43 44 |
# File 'lib/reg_api2/sym_hash.rb', line 42 def []=(key, new_value) key.kind_of?(Symbol) ? self[key.to_s]=new_value : super(key, new_value) end |
- (Boolean) has_key?(key)
Returns true if the given key is present in hsh.
27 28 29 |
# File 'lib/reg_api2/sym_hash.rb', line 27 def has_key?(key) key.kind_of?(Symbol) ? self.has_key?(key.to_s) : super(key) end |
- (Boolean) include?(key)
Returns true if the given key is present in hsh.
32 33 34 |
# File 'lib/reg_api2/sym_hash.rb', line 32 def include?(key) has_key?(key) end |
- (Boolean) respond_to?(key)
Always true
47 48 49 |
# File 'lib/reg_api2/sym_hash.rb', line 47 def respond_to?(key) true end |