Class: JSONP3::Path::IndexSelector
- Defined in:
- lib/json_p3/path/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.
97 98 99 100 |
# File 'lib/json_p3/path/selector.rb', line 97 def initialize(env, token, index) super(env, token) @index = index end |
Instance Attribute Details
#index ⇒ Object (readonly)
Returns the value of attribute index.
95 96 97 |
# File 'lib/json_p3/path/selector.rb', line 95 def index @index end |
Instance Method Details
#==(other) ⇒ Object Also known as: eql?
121 122 123 124 125 |
# File 'lib/json_p3/path/selector.rb', line 121 def ==(other) self.class == other.class && @index == other.index && @token == other.token end |
#hash ⇒ Object
129 130 131 |
# File 'lib/json_p3/path/selector.rb', line 129 def hash [@index, @token].hash end |
#resolve(node) ⇒ Object
102 103 104 105 106 107 108 109 110 111 |
# File 'lib/json_p3/path/selector.rb', line 102 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
113 114 115 |
# File 'lib/json_p3/path/selector.rb', line 113 def singular? true end |
#to_s ⇒ Object
117 118 119 |
# File 'lib/json_p3/path/selector.rb', line 117 def to_s @index.to_s end |