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
Methods inherited from Selector
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
#index ⇒ Object (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 |
#hash ⇒ Object
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
115 116 117 |
# File 'lib/json_p3/selector.rb', line 115 def singular? true end |
#to_s ⇒ Object
119 120 121 |
# File 'lib/json_p3/selector.rb', line 119 def to_s @index.to_s end |