Sha256: d5f4e12347ec33cbc1b619b273cb5e89c9903b3b1226a47c62356a806ac15785

Contents?: true

Size: 1.18 KB

Versions: 3

Compression:

Stored size: 1.18 KB

Contents

module Danica
  class Operator
    class Chained < Operator
      attr_reader :variables

      def variables=(vars)
        @variables = vars.map { |v| wrap_value(v) }
      end

      def to_f
        chain(variables.map(&:to_f))
      end

      def include?(value)
        value = wrap_value(value)
        variables.include?(value)
      end

      def to_tex
        to_string(:tex)
      end

      def to_gnu
        to_string(:gnu)
      end

      private

      def to_string(type)
        extractor = string_extractor("to_#{type}")
        symbol = { tex: tex_symbol, gnu: gnu_symbol }[type]
        variables.procedural_join(extractor, &join_proc(symbol))
      end

      def join_proc(symbol)
        proc { " #{symbol} " }
      end

      def string_extractor(method)
        proc do |parcel|
          parcel = wrap_as_group(parcel)
          parcel.public_send(method)
        end
      end

      def repack(other)
        other_variables = other.is_a?(self.class) ? other.variables : [ other ]
        self.class.new(variables + other_variables)
      end

      def chain(numbers)
        numbers.inject do |a,b|
          chain_operation(a,b)
        end.to_f
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
danica-2.4.2 lib/danica/operator/chained.rb
danica-2.4.1 lib/danica/operator/chained.rb
danica-2.4.0 lib/danica/operator/chained.rb