Class: HashWithIndifferentAccessCustom

Inherits:
Object
  • Object
show all
Defined in:
lib/helpers/hash_with_indifferent_access_custom.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}) ⇒ HashWithIndifferentAccessCustom

Returns a new instance of HashWithIndifferentAccessCustom.



6
7
8
9
10
11
# File 'lib/helpers/hash_with_indifferent_access_custom.rb', line 6

def initialize(hash = {})
  @attributes = {}
  hash.each { |key, val| self[key] = val }
  @keys = @attributes.keys
  @values = @attributes.values
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



4
5
6
# File 'lib/helpers/hash_with_indifferent_access_custom.rb', line 4

def attributes
  @attributes
end

#keysObject (readonly)

Returns the value of attribute keys.



4
5
6
# File 'lib/helpers/hash_with_indifferent_access_custom.rb', line 4

def keys
  @keys
end

#valuesObject (readonly)

Returns the value of attribute values.



4
5
6
# File 'lib/helpers/hash_with_indifferent_access_custom.rb', line 4

def values
  @values
end

Class Method Details

.[](*arr) ⇒ Object



21
22
23
# File 'lib/helpers/hash_with_indifferent_access_custom.rb', line 21

def self.[](*arr)
  new(Hash[*arr])
end

Instance Method Details

#[](key) ⇒ Object



17
18
19
# File 'lib/helpers/hash_with_indifferent_access_custom.rb', line 17

def [](key)
  @attributes[key.to_sym]
end

#[]=(key, value) ⇒ Object



13
14
15
# File 'lib/helpers/hash_with_indifferent_access_custom.rb', line 13

def []=(key, value)
  @attributes[key.to_sym] = value
end

#delete(key) ⇒ Object



25
26
27
28
# File 'lib/helpers/hash_with_indifferent_access_custom.rb', line 25

def delete(key)
  @attributes.delete(key)
  self.class.new(@attributes)
end

#to_json(*_args) ⇒ Object



30
31
32
# File 'lib/helpers/hash_with_indifferent_access_custom.rb', line 30

def to_json(*_args)
  @attributes.to_json
end