Sha256: 631550a228465ff5ecf77bdf45834d6ab199d0b05434ebb2de5b3812a4ccb5d4

Contents?: true

Size: 1.35 KB

Versions: 4

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(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

4 entries across 4 versions & 1 rubygems

Version Path
dentaku-3.3.4 lib/dentaku/ast/function.rb
dentaku-3.3.3 lib/dentaku/ast/function.rb
dentaku-3.3.2 lib/dentaku/ast/function.rb
dentaku-3.3.1 lib/dentaku/ast/function.rb