Class: JSONP3::FunctionExpression

Inherits:
Expression show all
Defined in:
lib/json_p3/filter.rb

Overview

A filter function call.

Instance Attribute Summary collapse

Attributes inherited from Expression

#token

Instance Method Summary collapse

Constructor Details

#initialize(token, name, args) ⇒ FunctionExpression

Returns a new instance of FunctionExpression.

Parameters:



354
355
356
357
358
# File 'lib/json_p3/filter.rb', line 354

def initialize(token, name, args)
  super(token)
  @name = name
  @args = args
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



350
351
352
# File 'lib/json_p3/filter.rb', line 350

def args
  @args
end

#nameObject (readonly)

Returns the value of attribute name.



350
351
352
# File 'lib/json_p3/filter.rb', line 350

def name
  @name
end

Instance Method Details

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



374
375
376
377
378
379
# File 'lib/json_p3/filter.rb', line 374

def ==(other)
  self.class == other.class &&
    @name == other.name &&
    @args == other.args &&
    @token == other.token
end

#evaluate(context) ⇒ Object



360
361
362
363
364
365
366
367
# File 'lib/json_p3/filter.rb', line 360

def evaluate(context)
  func = context.env.function_extensions.fetch(@name)
  args = @args.map { |arg| arg.evaluate(context) }
  unpacked_args = unpack_node_lists(func, args)
  func.call(*unpacked_args)
rescue KeyError
  :nothing
end

#hashObject



383
384
385
# File 'lib/json_p3/filter.rb', line 383

def hash
  [@name, @args, @token].hash
end

#to_sObject



369
370
371
372
# File 'lib/json_p3/filter.rb', line 369

def to_s
  args = @args.map(&:to_s).join(", ")
  "#{@name}(#{args})"
end