Class: JMESPath::Nodes::Field Private

Inherits:
Node
  • 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.

Direct Known Subclasses

ChainedField

Instance Method Summary collapse

Methods inherited from Node

#optimize

Constructor Details

#initialize(key) ⇒ Field

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 Field.



6
7
8
9
# File 'lib/jmespath/nodes/field.rb', line 6

def initialize(key)
  @key = key
  @key_sym = key.respond_to?(:to_sym) ? key.to_sym : nil
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.



30
31
32
# File 'lib/jmespath/nodes/field.rb', line 30

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

#chains_with?(other) ⇒ Boolean

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:

  • (Boolean)


26
27
28
# File 'lib/jmespath/nodes/field.rb', line 26

def chains_with?(other)
  other.is_a?(Field)
end

#visit(value) ⇒ 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.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/jmespath/nodes/field.rb', line 11

def visit(value)
  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 @key_sym && !(v = value[@key_sym]).nil?
      v
    end
  elsif value.is_a?(Struct) && value.respond_to?(@key)
    value[@key]
  end
end