Class: JSONP3::FilterSelector
- Defined in:
- lib/json_p3/selector.rb
Overview
Select array elements or hash values according to a filter expression.
Instance Attribute Summary collapse
-
#expression ⇒ Object
readonly
Returns the value of attribute expression.
Attributes inherited from Selector
Instance Method Summary collapse
- #==(other) ⇒ Object (also: #eql?)
- #hash ⇒ Object
-
#initialize(env, token, expression) ⇒ FilterSelector
constructor
A new instance of FilterSelector.
- #resolve(node) ⇒ Object
- #resolve_enum(node) ⇒ Object
- #to_s ⇒ Object
Methods inherited from Selector
Constructor Details
#initialize(env, token, expression) ⇒ FilterSelector
Returns a new instance of FilterSelector.
251 252 253 254 |
# File 'lib/json_p3/selector.rb', line 251 def initialize(env, token, expression) super(env, token) @expression = expression end |
Instance Attribute Details
#expression ⇒ Object (readonly)
Returns the value of attribute expression.
249 250 251 |
# File 'lib/json_p3/selector.rb', line 249 def expression @expression end |
Instance Method Details
#==(other) ⇒ Object Also known as: eql?
294 295 296 297 298 |
# File 'lib/json_p3/selector.rb', line 294 def ==(other) self.class == other.class && @expression == other.start && @token == other.token end |
#hash ⇒ Object
302 303 304 |
# File 'lib/json_p3/selector.rb', line 302 def hash [@expression, @token].hash end |
#resolve(node) ⇒ Object
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 |
# File 'lib/json_p3/selector.rb', line 256 def resolve(node) nodes = [] # : Array[JSONPathNode] if node.value.is_a?(Array) node.value.each_with_index do |e, i| context = FilterContext.new(@env, e, node.root) nodes << node.new_child(e, i) if @expression.evaluate(context) end elsif node.value.is_a?(Hash) node.value.each_pair do |k, v| context = FilterContext.new(@env, v, node.root) nodes << node.new_child(v, k) if @expression.evaluate(context) end end nodes end |
#resolve_enum(node) ⇒ Object
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 |
# File 'lib/json_p3/selector.rb', line 274 def resolve_enum(node) Enumerator.new do |yielder| if node.value.is_a?(Array) node.value.each_with_index do |e, i| context = FilterContext.new(@env, e, node.root) yielder << node.new_child(e, i) if @expression.evaluate(context) end elsif node.value.is_a?(Hash) node.value.each_pair do |k, v| context = FilterContext.new(@env, v, node.root) yielder << node.new_child(v, k) if @expression.evaluate(context) end end end end |
#to_s ⇒ Object
290 291 292 |
# File 'lib/json_p3/selector.rb', line 290 def to_s "?#{@expression}" end |