Sha256: 4d7e1bff3fc1736f5ff126d16a9465677d5a62b15a26b0c70a60c81f5cc50625

Contents?: true

Size: 1.2 KB

Versions: 8

Compression:

Stored size: 1.2 KB

Contents

module Danica
  class Operator
    class Chained < Operator
      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.content)
      end

      def to(format)
        extractor = string_extractor(format)
        variables.procedural_join(extractor, &join_proc(symbol(format)))
      end

      def variables
        @variables.map(&:content)
      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

8 entries across 8 versions & 1 rubygems

Version Path
danica-2.6.4 lib/danica/operator/chained.rb
danica-2.6.3 lib/danica/operator/chained.rb
danica-2.6.2 lib/danica/operator/chained.rb
danica-2.6.1 lib/danica/operator/chained.rb
danica-2.6.0 lib/danica/operator/chained.rb
danica-2.5.1 lib/danica/operator/chained.rb
danica-2.5.0 lib/danica/operator/chained.rb
danica-2.4.4 lib/danica/operator/chained.rb