Class: Rhoconnect::RhoHashWithIndifferentAccess

Inherits:
Hash
  • Object
show all
Defined in:
lib/rhoconnect/rho_indifferent_access.rb

Instance Method Summary collapse

Methods inherited from Hash

#with_indifferent_access

Constructor Details

#initialize(constructor = {}) ⇒ RhoHashWithIndifferentAccess

Returns a new instance of RhoHashWithIndifferentAccess.



7
8
9
10
11
12
# File 'lib/rhoconnect/rho_indifferent_access.rb', line 7

def initialize(constructor = {})
  if constructor.is_a?(Hash)
    super()
    update(constructor)
  end
end

Instance Method Details

#[]=(key, value) ⇒ Object

Assigns a new value to the hash:

hash = HashWithIndifferentAccess.new
hash[:key] = "value"


30
31
32
# File 'lib/rhoconnect/rho_indifferent_access.rb', line 30

def []=(key, value)
  regular_writer(convert_key(key), convert_value(value))
end

#default(key = nil) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/rhoconnect/rho_indifferent_access.rb', line 14

def default(key = nil)
  if key.is_a?(Symbol) && include?(key = key.to_s)
    self[key]
  else
    super
  end
end

#key?(key) ⇒ Boolean Also known as: include?, has_key?, member?

Checks the hash for a key matching the argument passed in:

hash = HashWithIndifferentAccess.new
hash["key"] = "value"
hash.key? :key  # => true
hash.key? "key" # => true

Returns:

  • (Boolean)


58
59
60
# File 'lib/rhoconnect/rho_indifferent_access.rb', line 58

def key?(key)
  super(convert_key(key))
end

#regular_updateObject

unless method_defined?(:regular_update)



23
# File 'lib/rhoconnect/rho_indifferent_access.rb', line 23

alias_method :regular_update, :update

#regular_writerObject

unless method_defined?(:regular_writer)



22
# File 'lib/rhoconnect/rho_indifferent_access.rb', line 22

alias_method :regular_writer, :[]=

#stringify_keys!Object



66
# File 'lib/rhoconnect/rho_indifferent_access.rb', line 66

def stringify_keys!; self end

#symbolize_keys!Object



67
# File 'lib/rhoconnect/rho_indifferent_access.rb', line 67

def symbolize_keys!; self end

#to_options!Object



68
# File 'lib/rhoconnect/rho_indifferent_access.rb', line 68

def to_options!; self end

#update(other_hash) ⇒ Object Also known as: merge!

Updates the instantized hash with values from the second:

hash_1 = HashWithIndifferentAccess.new
hash_1[:key] = "value"

hash_2 = HashWithIndifferentAccess.new
hash_2[:key] = "New Value!"

hash_1.update(hash_2) # => {"key"=>"New Value!"}


44
45
46
47
# File 'lib/rhoconnect/rho_indifferent_access.rb', line 44

def update(other_hash)
  other_hash.each_pair { |key, value| regular_writer(convert_key(key), convert_value(value)) }
  self
end