Class: Grape::Util::LazyValueEnumerable

Inherits:
LazyValue
  • Object
show all
Defined in:
lib/grape/util/lazy_value.rb

Direct Known Subclasses

LazyValueArray, LazyValueHash

Instance Attribute Summary

Attributes inherited from LazyValue

#access_keys

Instance Method Summary collapse

Methods inherited from LazyValue

#evaluate, #evaluate_from, #initialize, #lazy?, #reached_by, #to_s

Constructor Details

This class inherits a constructor from Grape::Util::LazyValue

Instance Method Details

#[](key) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/grape/util/lazy_value.rb', line 37

def [](key)
  if @value_hash[key].nil?
    LazyValue.new(nil).reached_by(access_keys, key)
  else
    @value_hash[key].reached_by(access_keys, key)
  end
end

#[]=(key, value) ⇒ Object



51
52
53
54
55
56
57
58
59
60
# File 'lib/grape/util/lazy_value.rb', line 51

def []=(key, value)
  @value_hash[key] = case value
                     when Hash
                       LazyValueHash.new(value)
                     when Array
                       LazyValueArray.new(value)
                     else
                       LazyValue.new(value)
                     end
end

#fetch(access_keys) ⇒ Object



45
46
47
48
49
# File 'lib/grape/util/lazy_value.rb', line 45

def fetch(access_keys)
  fetched_keys = access_keys.dup
  value = self[fetched_keys.shift]
  fetched_keys.any? ? value.fetch(fetched_keys) : value
end