Class: Liquid::Drop::HashOrArray

Inherits:
Liquid::Drop show all
Extended by:
Forwardable::Extended
Defined in:
lib/liquid/drop/hash_or_array.rb,
lib/liquid/drop/hash_or_array/version.rb

Constant Summary collapse

VERSION =
"1.0.0"

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ <HashOrArray>

– There is no need for explaining it. –



17
18
19
# File 'lib/liquid/drop/hash_or_array.rb', line 17

def initialize(data)
  @data = data
end

Instance Method Details

#[](key) ⇒ <Any>

– Standard hash method so that you can also access

the data like a hash/array.

Returns:

  • (<Any>)

    the value or nil.



26
27
28
# File 'lib/liquid/drop/hash_or_array.rb', line 26

def [](key)
  liquid_method_missing(key)
end

#liquid_method_missing(key) ⇒ <Any>

– Uses Liquids method missing method to get the key

when you try and access the drop like a chained method
we do the case because oen day we might split out the
class and it would require minimal refactor.

Returns:

  • (<Any>)

    the value or nil.



37
38
39
40
41
42
43
44
45
46
# File 'lib/liquid/drop/hash_or_array.rb', line 37

def liquid_method_missing(key)
  val = @data.fetch(key.to_s, @data[key.to_sym])

  case true
  when val.is_a?(Array) then HashOrArray.new(val)
  when val.is_a?( Hash) then HashOrArray.new(val)
  else
    val
  end
end