Module: Dottie::Methods

Included in:
Freckle
Defined in:
lib/dottie/methods.rb

Instance Method Summary collapse

Instance Method Details

#[](key) ⇒ Object

Reads from the Hash or Array with special handling for Dottie-style keys.



7
8
9
10
11
12
13
# File 'lib/dottie/methods.rb', line 7

def [](key)
  if Dottie.dottie_key?(key)
    Dottie.get(wrapped_object_or_self, key)
  else
    super
  end
end

#[]=(key, value) ⇒ Object

Writes to the Hash or Array with special handling for Dottie-style keys, adding missing Hash nodes or Array elements where necessary.



19
20
21
22
23
24
25
# File 'lib/dottie/methods.rb', line 19

def []=(key, value)
  if Dottie.dottie_key?(key)
    Dottie.set(wrapped_object_or_self, key, value)
  else
    super
  end
end

#delete(key) ⇒ Object

Deletes the value at the specified key and returns it.



62
63
64
65
66
67
68
# File 'lib/dottie/methods.rb', line 62

def delete(key)
  if Dottie.dottie_key?(key)
    Dottie.delete(wrapped_object_or_self, key)
  else
    super
  end
end

#dottie_flattenObject



73
74
75
# File 'lib/dottie/methods.rb', line 73

def dottie_flatten
  Dottie.flatten(wrapped_object_or_self)
end

#dottie_keys(intermediate = false) ⇒ Object



80
81
82
# File 'lib/dottie/methods.rb', line 80

def dottie_keys(intermediate = false)
  Dottie.keys(wrapped_object_or_self, intermediate: intermediate)
end

#fetch(key, default = :_fetch_default_, &block) ⇒ Object

Fetches a value from the Hash with special handling for Dottie-style keys. Handles the optional default value and block the same as Hash#fetch.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/dottie/methods.rb', line 43

def fetch(key, default = :_fetch_default_, &block)
  if Dottie.dottie_key?(key)
    if default != :_fetch_default_
      Dottie.fetch(wrapped_object_or_self, key, default, &block)
    else
      Dottie.fetch(wrapped_object_or_self, key, &block)
    end
  else
    if default != :_fetch_default_
      wrapped_object_or_self.fetch(key, default, &block)
    else
      wrapped_object_or_self.fetch(key, &block)
    end
  end
end

#has_key?(key) ⇒ Boolean

Checks whether the Hash has the specified key with special handling for Dottie-style keys.

Returns:

  • (Boolean)


31
32
33
34
35
36
37
# File 'lib/dottie/methods.rb', line 31

def has_key?(key)
  if Dottie.dottie_key?(key)
    Dottie.has_key?(wrapped_object_or_self, key)
  else
    super
  end
end