Class: Unitwise::Expression::Decomposer

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

Constant Summary collapse

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(expression) ⇒ Decomposer



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

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.



11
12
13
# File 'lib/unitwise/expression/decomposer.rb', line 11

def expression
  @expression
end

Instance Method Details

#parseObject



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

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

#termsObject



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

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

#transformObject



28
29
30
# File 'lib/unitwise/expression/decomposer.rb', line 28

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