Class: JSONP3::FilterSelector

Inherits:
Selector
  • Object
show all
Defined in:
lib/json_p3/selector.rb

Overview

Select array elements or hash values according to a filter expression.

Instance Attribute Summary collapse

Attributes inherited from Selector

#token

Instance Method Summary collapse

Methods inherited from Selector

#singular?

Constructor Details

#initialize(env, token, expression) ⇒ FilterSelector

Returns a new instance of FilterSelector.



246
247
248
249
# File 'lib/json_p3/selector.rb', line 246

def initialize(env, token, expression)
  super(env, token)
  @expression = expression
end

Instance Attribute Details

#expressionObject (readonly)

Returns the value of attribute expression.



244
245
246
# File 'lib/json_p3/selector.rb', line 244

def expression
  @expression
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



273
274
275
276
277
# File 'lib/json_p3/selector.rb', line 273

def ==(other)
  self.class == other.class &&
    @expression == other.start &&
    @token == other.token
end

#hashObject



281
282
283
# File 'lib/json_p3/selector.rb', line 281

def hash
  [@expression, @token].hash
end

#resolve(node) ⇒ Object



251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/json_p3/selector.rb', line 251

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

#to_sObject



269
270
271
# File 'lib/json_p3/selector.rb', line 269

def to_s
  "?#{@expression}"
end