Class: Dentaku::AST::Function

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

Direct Known Subclasses

All, Any, Count, Duration, Filter, If, Map, Pluck, RubyMath, StringFunctions::Base

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

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Node

arity, peek, precedence, #type

Constructor Details

#initialize(*args) ⇒ Function

Returns a new instance of Function.



10
11
12
# File 'lib/dentaku/ast/function.rb', line 10

def initialize(*args)
  @args = args
end

Class Method Details

.get(name) ⇒ Object



27
28
29
# File 'lib/dentaku/ast/function.rb', line 27

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:



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

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



31
32
33
# File 'lib/dentaku/ast/function.rb', line 31

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

.register_class(name, function_class) ⇒ Object



35
36
37
# File 'lib/dentaku/ast/function.rb', line 35

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

.registryObject



39
40
41
# File 'lib/dentaku/ast/function.rb', line 39

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

Instance Method Details

#deferred_argsObject

override if your function implementation needs to defer evaluation of any arguments



23
24
25
# File 'lib/dentaku/ast/function.rb', line 23

def deferred_args
  []
end

#dependencies(context = {}) ⇒ Object



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

def dependencies(context = {})
  deferred = deferred_args
  @args.each_with_index
       .reject { |_, i| deferred.include? i }
       .flat_map { |a, _| a.dependencies(context) }
end