Class: JSONP3::IndexSelector
- Defined in:
- lib/json_p3/selector.rb
Overview
The index selector selects values from arrays given an index.
Instance Attribute Summary collapse
-
#index ⇒ Object
readonly
Returns the value of attribute index.
Attributes inherited from Selector
Instance Method Summary collapse
- #==(other) ⇒ Object (also: #eql?)
- #hash ⇒ Object
-
#initialize(env, token, index) ⇒ IndexSelector
constructor
A new instance of IndexSelector.
- #resolve(node) ⇒ Object
- #singular? ⇒ Boolean
- #to_s ⇒ Object
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
#index ⇒ Object (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 |
#hash ⇒ Object
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
109 110 111 |
# File 'lib/json_p3/selector.rb', line 109 def singular? true end |
#to_s ⇒ Object
113 114 115 |
# File 'lib/json_p3/selector.rb', line 113 def to_s @index.to_s end |