Sha256: ceeda9a4473195774a6256edf80df381d04ae286467de30fc197a676b557963a

Contents?: true

Size: 1.5 KB

Versions: 1

Compression:

Stored size: 1.5 KB

Contents

module Danica
  class Function
    include Common
    include VariablesHolder
    include DSL

    attr_accessor :name

    default_value :priority, 3
    default_value :is_grouped?, false
    delegate :to_f, :to, to: :function_block

    def self.build(*vars, &block)
      Class.new(self) do
        variables(*vars)

        private

        define_method :function_block do
          @function_block ||= instance_eval(&block) if block
        end
      end
    end

    def self.create(*vars, &block)
      build(*vars, &block).new
    end

    def initialize(*args)
      options = args.extract_options!

      attributes = { variables: args.flatten }.merge(options)

      attributes.each do |key, value|
        self.public_send("#{key}=", value)
      end
    end

    def calculate(*args)
      vars_map = args.extract_options!
      vars_map = variables_value_hash.merge(vars_map)
      vars_map.each do |k, v|
        unless v && (v.is_a?(Integer) || v.valued?)
          vars_map[k] = args.shift
        end
      end

      self.class.new(vars_map).to_f
    end

    def describe(format)
      "#{name}(#{description_variables(format)}) = #{to(format)}"
    end

    def describe_tex
      describe(:tex)
    end

    def describe_gnu
      describe(:gnu)
    end

    autoload :Gauss,    'danica/function/gauss'

    private

    def description_variables(format)
      non_constant_variables.map { |v| v.to(format) }.join(', ')
    end

    def non_constant_variables
      variables.reject(&:constant?)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
danica-2.4.3 lib/danica/function.rb