Class: Unitwise::Expression::Decomposer

Inherits:
Object
  • Object
show all
Defined in:
lib/unitwise/expression/decomposer.rb

Constant Summary collapse

METHODS =
[:primary_code, :secondary_code, :names, :slugs, :symbol]
PARSERS =
METHODS.reduce({}) do |hash, method|
  hash[method] = Parser.new(method); hash
end
TRANSFORMER =
Transformer.new

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(expression) ⇒ Decomposer

Returns a new instance of Decomposer.



15
16
17
18
19
20
# File 'lib/unitwise/expression/decomposer.rb', line 15

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

Instance Attribute Details

#expressionObject (readonly)

Returns the value of attribute expression.



13
14
15
# File 'lib/unitwise/expression/decomposer.rb', line 13

def expression
  @expression
end

Instance Method Details

#termsObject



30
31
32
33
34
35
36
37
38
39
# File 'lib/unitwise/expression/decomposer.rb', line 30

def terms
  @terms ||= begin
    transformation = transform
    if transformation.respond_to?(:terms)
      transformation.terms
    else
      Array(transformation)
    end
  end
end

#transformObject



22
23
24
25
26
27
28
# File 'lib/unitwise/expression/decomposer.rb', line 22

def transform
  PARSERS.reduce(nil) do |foo, (method, parser)|
    if parsed = parser.parse(expression) rescue next
      return TRANSFORMER.apply(parsed, :key => method)
    end
  end
end