Class: JMESPath::Nodes::ChainedField Private

Inherits:
Field
  • Object
show all
Defined in:
lib/jmespath/nodes/field.rb

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Instance Method Summary collapse

Methods inherited from Field

#chains_with?

Methods inherited from Node

#chains_with?, #optimize

Constructor Details

#initialize(keys) ⇒ ChainedField

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of ChainedField.



42
43
44
45
46
47
# File 'lib/jmespath/nodes/field.rb', line 42

def initialize(keys)
  @keys = keys
  @key_syms = keys.each_with_object({}) do |k, syms|
    syms[k] = k.to_sym if k.respond_to?(:to_sym)
  end
end

Instance Method Details

#chain(other) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



66
67
68
# File 'lib/jmespath/nodes/field.rb', line 66

def chain(other)
  ChainedField.new([*@keys, *other.keys])
end

#visit(obj) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/jmespath/nodes/field.rb', line 49

def visit(obj)
  @keys.reduce(obj) do |value, key|
    if value.respond_to?(:to_ary) && key.is_a?(Integer)
      value.to_ary[key]
    elsif value.respond_to?(:to_hash)
      value = value.to_hash
      if !(v = value[key]).nil?
        v
      elsif (sym = @key_syms[key]) && !(v = value[sym]).nil?
        v
      end
    elsif value.is_a?(Struct) && value.respond_to?(key)
      value[key]
    end
  end
end