Class: Unitwise::Expression::Decomposer
- Inherits:
-
Object
- Object
- Unitwise::Expression::Decomposer
- Defined in:
- lib/unitwise/expression/decomposer.rb
Constant Summary collapse
- MODES =
[:primary_code, :secondary_code, :names, :slugs, :symbol]
- PARSERS =
MODES.reduce({}) do |hash, mode| hash[mode] = Parser.new(mode); hash end
- TRANSFORMER =
Transformer.new
Instance Attribute Summary collapse
-
#expression ⇒ Object
readonly
Returns the value of attribute expression.
-
#mode ⇒ Object
readonly
Returns the value of attribute mode.
Instance Method Summary collapse
-
#initialize(expression) ⇒ Decomposer
constructor
A new instance of Decomposer.
- #parse ⇒ Object
- #terms ⇒ Object
- #transform ⇒ Object
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? fail ExpressionError, "Could not evaluate '#{@expression}'." end end |
Instance Attribute Details
#expression ⇒ Object (readonly)
Returns the value of attribute expression.
13 14 15 |
# File 'lib/unitwise/expression/decomposer.rb', line 13 def expression @expression end |
#mode ⇒ Object (readonly)
Returns the value of attribute mode.
13 14 15 |
# File 'lib/unitwise/expression/decomposer.rb', line 13 def mode @mode end |
Instance Method Details
#parse ⇒ Object
22 23 24 25 26 27 28 |
# File 'lib/unitwise/expression/decomposer.rb', line 22 def parse @parse ||= PARSERS.reduce(nil) do |null,(mode, parser)| parsed = parser.parse(expression) rescue next @mode = mode break parsed end end |
#terms ⇒ Object
34 35 36 37 38 39 40 |
# File 'lib/unitwise/expression/decomposer.rb', line 34 def terms @terms ||= if transform.respond_to?(:terms) transform.terms else Array(transform) end end |
#transform ⇒ Object
30 31 32 |
# File 'lib/unitwise/expression/decomposer.rb', line 30 def transform @transform ||= TRANSFORMER.apply(parse, :mode => mode) end |