Sha256: a590b41767135a4f479500ed07ffd7412c806158d7a1416dae46e97e6ebde365

Contents?: true

Size: 1.35 KB

Versions: 2

Compression:

Stored size: 1.35 KB

Contents

require_relative 'node'
require_relative 'function_registry'

module Dentaku
  module AST
    class Function < Node
      # @return [Integer] with the number of significant decimal digits to use.
      DIG = Float::DIG + 1

      def initialize(*args)
        @args = args
      end

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

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

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

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

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

      # @return [Numeric] where possible it returns an Integer otherwise a BigDecimal.
      # An Exception will be raised if a value is passed that cannot be cast to a Number.
      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.new(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
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
dentaku-3.3.0 lib/dentaku/ast/function.rb
dentaku-3.2.1 lib/dentaku/ast/function.rb