Class: JSONP3::Path::FilterSelector
- Defined in:
- lib/json_p3/path/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.
247 248 249 250 |
# File 'lib/json_p3/path/selector.rb', line 247 def initialize(env, token, expression) super(env, token) @expression = expression end |
Instance Attribute Details
#expression ⇒ Object (readonly)
Returns the value of attribute expression.
245 246 247 |
# File 'lib/json_p3/path/selector.rb', line 245 def expression @expression end |
Instance Method Details
#==(other) ⇒ Object Also known as: eql?
291 292 293 294 295 |
# File 'lib/json_p3/path/selector.rb', line 291 def ==(other) self.class == other.class && @expression == other.start && @token == other.token end |
#hash ⇒ Object
299 300 301 |
# File 'lib/json_p3/path/selector.rb', line 299 def hash [@expression, @token].hash end |
#resolve(node) ⇒ Object
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 |
# File 'lib/json_p3/path/selector.rb', line 252 def resolve(node) # @type var nodes: Array[Node] nodes = [] 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
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 |
# File 'lib/json_p3/path/selector.rb', line 271 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
287 288 289 |
# File 'lib/json_p3/path/selector.rb', line 287 def to_s "?#{@expression}" end |