Sha256: 229c7fb2994af8a79f52121ee36f2a4aa530c58c1d6d4b71baee62cf9afb1fd0

Contents?: true

Size: 1.16 KB

Versions: 1

Compression:

Stored size: 1.16 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(format)
        extractor = string_extractor(format)
        variables.procedural_join(extractor, &join_proc(symbol(format)))
      end

      private

      def symbol(format)
        case format.to_sym
        when :tex
          tex_symbol
        when :gnu
          gnu_symbol
        end
      end

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

      def string_extractor(method)
        proc do |parcel|
          parcel = wrap_as_group(parcel)
          parcel.to(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

1 entries across 1 versions & 1 rubygems

Version Path
danica-2.4.3 lib/danica/operator/chained.rb