Class: JSONP3::IndexSelector

Inherits:
Selector show all
Defined in:
lib/json_p3/selector.rb

Overview

The index selector selects values from arrays given an index.

Instance Attribute Summary collapse

Attributes inherited from Selector

#token

Instance Method Summary collapse

Constructor Details

#initialize(env, token, index) ⇒ IndexSelector

Returns a new instance of IndexSelector.



93
94
95
96
# File 'lib/json_p3/selector.rb', line 93

def initialize(env, token, index)
  super(env, token)
  @index = index
end

Instance Attribute Details

#indexObject (readonly)

Returns the value of attribute index.



91
92
93
# File 'lib/json_p3/selector.rb', line 91

def index
  @index
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



117
118
119
120
121
# File 'lib/json_p3/selector.rb', line 117

def ==(other)
  self.class == other.class &&
    @index == other.index &&
    @token == other.token
end

#hashObject



125
126
127
# File 'lib/json_p3/selector.rb', line 125

def hash
  [@index, @token].hash
end

#resolve(node) ⇒ Object



98
99
100
101
102
103
104
105
106
107
# File 'lib/json_p3/selector.rb', line 98

def resolve(node)
  if node.value.is_a?(Array)
    norm_index = normalize(@index, node.value.length)
    return [] if norm_index.negative? || norm_index >= node.value.length

    [node.new_child(node.value[@index], norm_index)]
  else
    []
  end
end

#singular?Boolean

Returns:

  • (Boolean)


109
110
111
# File 'lib/json_p3/selector.rb', line 109

def singular?
  true
end

#to_sObject



113
114
115
# File 'lib/json_p3/selector.rb', line 113

def to_s
  @index.to_s
end