Sha256: 16d3f43b3b4b8d4dd290da890bd6e50d18333a6758b6b79ec8880676a2fba141

Contents?: true

Size: 1.03 KB

Versions: 3

Compression:

Stored size: 1.03 KB

Contents

module Danica
  module DSL
    def self.register_operator(method, clazz=nil)
      register(method, clazz, 'Danica::Operator')
    end

    def self.register_wrapper(method, clazz=nil)
      register(method, clazz, 'Danica::Wrapper')
    end

    def self.register(method, clazz=nil, base=nil)
      define_method method do |*args|
        clazz = [base.to_s, method.to_s.camelize].compact.join('::').constantize unless clazz
        clazz = [base, clazz.to_s].compact.join('::').constantize unless clazz.is_a? Class
        clazz.new(*args)
      end
    end

    def function(*variables, &block)
      Function.build(*variables, &block).new
    end
  end

  %i(
    addition multiplication division sin cos power
    squared_root exponential
  ).each do |method|
    DSL.register_operator(method)
  end

  %i(number group negative plus_minus variable).each do |method|
    DSL.register_wrapper(method)
  end

  DSL.register_wrapper  :num,     :Number
  DSL.register_operator :sum,     :Addition
  DSL.register_operator :product, :Multiplication
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
danica-2.4.1 lib/danica/dsl.rb
danica-2.4.0 lib/danica/dsl.rb
danica-2.3.1 lib/danica/dsl.rb