Class: Hashie::Mash

Inherits:
Hash show all
Defined in:
lib/chozo/hashie_ext/mash.rb

Overview

Author:

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Hash

#to_hash

Methods inherited from Hash

#assert_valid_keys, #deep_stringify_keys, #deep_stringify_keys!, #deep_symbolize_keys, #deep_symbolize_keys!, #deep_transform_keys, #deep_transform_keys!, #dig, #dotted_paths, from_dotted_path, #stringify_keys, #stringify_keys!, #symbolize_keys, #symbolize_keys!, #transform_keys, #transform_keys!

Instance Attribute Details

#coercionsHash

Returns:



10
11
12
# File 'lib/chozo/hashie_ext/mash.rb', line 10

def coercions
  @coercions ||= HashWithIndifferentAccess.new
end

Instance Method Details

#[]=(key, value) ⇒ Object

Override setter to coerce the given value if a coercion is defined



23
24
25
26
# File 'lib/chozo/hashie_ext/mash.rb', line 23

def []=(key, value)
  coerced_value = coercion(key).present? ? coercion(key).call(value) : value
  old_setter(key, coerced_value)
end

#coercion(key) ⇒ Object



14
15
16
# File 'lib/chozo/hashie_ext/mash.rb', line 14

def coercion(key)
  self.coercions[key]
end

#container(path) ⇒ Hashie::Mash?

Return the containing Hashie::Mash of the given dotted path

Parameters:

  • path (String)

Returns:



33
34
35
36
37
38
39
40
41
# File 'lib/chozo/hashie_ext/mash.rb', line 33

def container(path)
  parts = path.split('.', 2)
  match = (self[parts[0].to_s] || self[parts[0].to_sym])
  if !parts[1] or match.nil?
    self
  else
    match.container(parts[1])
  end
end

#dupObject



43
44
45
46
47
# File 'lib/chozo/hashie_ext/mash.rb', line 43

def dup
  mash = old_dup
  mash.coercions = self.coercions
  mash
end

#old_dupObject



5
# File 'lib/chozo/hashie_ext/mash.rb', line 5

alias_method :old_dup, :dup

#old_setterObject



4
# File 'lib/chozo/hashie_ext/mash.rb', line 4

alias_method :old_setter, :[]=

#set_coercion(key, fun) ⇒ Object



18
19
20
# File 'lib/chozo/hashie_ext/mash.rb', line 18

def set_coercion(key, fun)
  self.coercions[key] = fun
end