Class: Unitwise::Expression::Decomposer
- Inherits:
-
Object
- Object
- Unitwise::Expression::Decomposer
- 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
-
#expression ⇒ Object
readonly
Returns the value of attribute expression.
Instance Method Summary collapse
-
#initialize(expression) ⇒ Decomposer
constructor
A new instance of Decomposer.
- #parse ⇒ Object
- #terms ⇒ Object
- #transform ⇒ Object
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
#expression ⇒ Object (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
#parse ⇒ Object
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 |
#terms ⇒ Object
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 |
#transform ⇒ Object
28 29 30 |
# File 'lib/unitwise/expression/decomposer.rb', line 28 def transform @transform ||= TRANSFORMER.apply(parse) end |