class RegApi2::SymHash

Hash with indifferent access to its elements. Also have no difference between {String} ans {Symbol} keys. @see ResultContract

Public Class Methods

from(source) click to toggle source

Forms data with indifferent access from specified source. @return [Object] Data with indifferent access

# File lib/reg_api2/sym_hash.rb, line 8
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

Public Instance Methods

[](key) click to toggle source

Element Reference — Retrieves the value object corresponding to the key object. If not found, returns the default value (see {Hash::new} for details).

Calls superclass method
# File lib/reg_api2/sym_hash.rb, line 36
def [](key)
  key.kind_of?(Symbol) ? self[key.to_s] : super(key)
end
[]=(key, new_value) click to toggle source

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).

Calls superclass method
# File lib/reg_api2/sym_hash.rb, line 41
def []=(key, new_value)
  key.kind_of?(Symbol) ? self[key.to_s]=new_value : super(key, new_value)
end
has_key?(key) click to toggle source

Returns true if the given key is present in hsh.

Calls superclass method
# File lib/reg_api2/sym_hash.rb, line 26
def has_key?(key)
  key.kind_of?(Symbol) ? self.has_key?(key.to_s) : super(key)
end
include?(key) click to toggle source

Returns true if the given key is present in hsh.

# File lib/reg_api2/sym_hash.rb, line 31
def include?(key)
  has_key?(key)
end
method_missing(key, *args, &block) click to toggle source

Sets or gets field in the hash.

# File lib/reg_api2/sym_hash.rb, line 51
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
respond_to?(key) click to toggle source

Always true

# File lib/reg_api2/sym_hash.rb, line 46
def respond_to?(key)
  true
end