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?, #hash_like?, #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


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

def initialize(keys)
  @keys = keys
  @key_syms = keys.each_with_object({}) do |k, syms|
    if k.respond_to?(:to_sym)
      syms[k] = k.to_sym
    end
  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.


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

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
# File 'lib/jmespath/nodes/field.rb', line 49

def visit(obj)
  @keys.reduce(obj) do |value, key|
    if value.is_a?(Array) && key.is_a?(Integer)
      value[key]
    elsif value.is_a?(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