Class: Dentaku::AST::Function

Inherits:
Node
  • Object
show all
Defined in:
lib/dentaku/ast/function.rb

Direct Known Subclasses

Count, Duration, Enum, If, Reduce, RubyMath, StringFunctions::Base, Xor

Constant Summary collapse

DIG =

Returns with the number of significant decimal digits to use.

Returns:

  • (Integer)

    with the number of significant decimal digits to use.

Float::DIG + 1

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Node

arity, #name, precedence, resolve_class, #type

Constructor Details

#initialize(*args) ⇒ Function

Returns a new instance of Function.



12
13
14
# File 'lib/dentaku/ast/function.rb', line 12

def initialize(*args)
  @args = args
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



7
8
9
# File 'lib/dentaku/ast/function.rb', line 7

def args
  @args
end

Class Method Details

.get(name) ⇒ Object



25
26
27
# File 'lib/dentaku/ast/function.rb', line 25

def self.get(name)
  registry.get(name)
end

.numeric(value) ⇒ Numeric

An Exception will be raised if a value is passed that cannot be cast to a Number.

Returns:

  • (Numeric)

    where possible it returns an Integer otherwise a BigDecimal.

Raises:



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/dentaku/ast/function.rb', line 43

def self.numeric(value)
  return value if value.is_a?(::Numeric)

  if value.is_a?(::String)
    number = value[/\A-?\d*\.?\d+\z/]
    return number.include?('.') ? BigDecimal(number, DIG) : number.to_i if number
  end

  raise Dentaku::ArgumentError.for(:incompatible_type, value: value, for: Numeric),
    "'#{value || value.class}' is not coercible to numeric"
end

.register(name, type, implementation) ⇒ Object



29
30
31
# File 'lib/dentaku/ast/function.rb', line 29

def self.register(name, type, implementation)
  registry.register(name, type, implementation)
end

.register_class(name, function_class) ⇒ Object



33
34
35
# File 'lib/dentaku/ast/function.rb', line 33

def self.register_class(name, function_class)
  registry.register_class(name, function_class)
end

.registryObject



37
38
39
# File 'lib/dentaku/ast/function.rb', line 37

def self.registry
  @registry ||= FunctionRegistry.new
end

Instance Method Details

#accept(visitor) ⇒ Object



16
17
18
# File 'lib/dentaku/ast/function.rb', line 16

def accept(visitor)
  visitor.visit_function(self)
end

#dependencies(context = {}) ⇒ Object



20
21
22
23
# File 'lib/dentaku/ast/function.rb', line 20

def dependencies(context = {})
  @args.each_with_index
       .flat_map { |a, _| a.dependencies(context) }
end