Sha256: ff676c0ac82b308d8f44066f6d512486aaa53d42f84787952fec2ec6cb0b56ae

Contents?: true

Size: 855 Bytes

Versions: 5

Compression:

Stored size: 855 Bytes

Contents

module Unitwise
  module Expression
    class Decomposer

      PARSERS = [:primary_code, :secondary_code, :names, :slugs, :symbol].map do |t|
        Parser.new(t)
      end

      TRANSFORMER = Transformer.new

      attr_reader :expression

      def initialize(expression)
        @expression = expression.to_s
        if terms.nil? || terms.empty?
          raise ExpressionError, "Could not evaluate '#{@expression}'."
        end
      end

      def parse
        PARSERS.reduce(nil) do |m, p|
          if prs = p.parse(expression) rescue next
            return prs
          end
        end
      end

      def transform
        @transform ||= TRANSFORMER.apply(parse)
      end

      def terms
        if transform.respond_to?(:terms)
          transform.terms
        else
          Array(transform)
        end
      end

    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
unitwise-0.3.0 lib/unitwise/expression/decomposer.rb
unitwise-0.2.2 lib/unitwise/expression/decomposer.rb
unitwise-0.2.1 lib/unitwise/expression/decomposer.rb
unitwise-0.2.0 lib/unitwise/expression/decomposer.rb
unitwise-0.1.0 lib/unitwise/expression/decomposer.rb