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

Methods inherited from Selector

#resolve_enum

Constructor Details

#initialize(env, token, index) ⇒ IndexSelector

Returns a new instance of IndexSelector.


99
100
101
102
# File 'lib/json_p3/selector.rb', line 99

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

Instance Attribute Details

#indexObject (readonly)

Returns the value of attribute index.


97
98
99
# File 'lib/json_p3/selector.rb', line 97

def index
  @index
end

Instance Method Details

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


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

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

#hashObject


131
132
133
# File 'lib/json_p3/selector.rb', line 131

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

#resolve(node) ⇒ Object


104
105
106
107
108
109
110
111
112
113
# File 'lib/json_p3/selector.rb', line 104

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)

115
116
117
# File 'lib/json_p3/selector.rb', line 115

def singular?
  true
end

#to_sObject


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

def to_s
  @index.to_s
end