Class: Dentaku::AST::RubyMath

Inherits:
Function show all
Defined in:
lib/dentaku/ast/functions/ruby_math.rb

Constant Summary collapse

ARRAY_RETURN_TYPES =
[:frexp, :lgamma].freeze

Constants inherited from Function

Function::DIG

Instance Attribute Summary

Attributes inherited from Function

#args

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Function

#accept, #dependencies, get, #initialize, numeric, register, register_class, registry

Methods inherited from Node

#dependencies, #name, precedence, resolve_class

Constructor Details

This class inherits a constructor from Dentaku::AST::Function

Class Method Details

.[](method) ⇒ Object



7
8
9
10
11
12
# File 'lib/dentaku/ast/functions/ruby_math.rb', line 7

def self.[](method)
  klass_name = method.to_s.capitalize
  klass = const_set(klass_name , Class.new(self))
  klass.implement(method)
  const_get(klass_name)
end

.arityObject



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

def self.arity
  @implementation.arity < 0 ? nil : @implementation.arity
end

.call(*args) ⇒ Object



35
36
37
38
39
# File 'lib/dentaku/ast/functions/ruby_math.rb', line 35

def self.call(*args)
  @implementation.call(*args)
rescue Math::DomainError => _e
  raise Dentaku::MathDomainError.new(name, args)
end

.implement(method) ⇒ Object



14
15
16
17
# File 'lib/dentaku/ast/functions/ruby_math.rb', line 14

def self.implement(method)
  @name = method
  @implementation = Math.method(method)
end

.max_param_countObject



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

def self.max_param_count
  @implementation.parameters.select { |type, _name| type == :rest }.any? ? Float::INFINITY : @implementation.parameters.count
end

.min_param_countObject



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

def self.min_param_count
  @implementation.parameters.select { |type, _name| type == :req }.count
end

.nameObject



19
20
21
# File 'lib/dentaku/ast/functions/ruby_math.rb', line 19

def self.name
  @name
end

Instance Method Details

#typeObject



48
49
50
# File 'lib/dentaku/ast/functions/ruby_math.rb', line 48

def type
  ARRAY_RETURN_TYPES.include?(@name) ? :array : :numeric
end

#value(context = {}) ⇒ Object



41
42
43
44
# File 'lib/dentaku/ast/functions/ruby_math.rb', line 41

def value(context = {})
  args = @args.flatten.map { |a| Dentaku::AST::Function.numeric(a.value(context)) }
  self.class.call(*args)
end