Class: Unitwise::Expression::Decomposer
- Inherits:
-
Object
- Object
- Unitwise::Expression::Decomposer
- Defined in:
- lib/unitwise/expression/decomposer.rb
Overview
The decomposer is used to turn string expressions into collections of terms. It is responsible for executing the parsing and transformation of a string, as well as caching the results.
Constant Summary collapse
- MODES =
[:primary_code, :secondary_code, :names, :slugs, :symbol].freeze
Instance Attribute Summary collapse
-
#expression ⇒ Object
readonly
Returns the value of attribute expression.
-
#mode ⇒ Object
readonly
Returns the value of attribute mode.
Class Method Summary collapse
-
.parse(expression) ⇒ Object
Parse an expression to an array of terms and cache the results.
- .parsers ⇒ Object
- .transformer ⇒ Object
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.
51 52 53 54 55 56 |
# File 'lib/unitwise/expression/decomposer.rb', line 51 def initialize(expression) @expression = expression.to_s if expression.empty? || terms.nil? || terms.empty? fail(ExpressionError, "Could not evaluate '#{ expression }'.") end end |
Instance Attribute Details
#expression ⇒ Object (readonly)
Returns the value of attribute expression.
49 50 51 |
# File 'lib/unitwise/expression/decomposer.rb', line 49 def expression @expression end |
#mode ⇒ Object (readonly)
Returns the value of attribute mode.
49 50 51 |
# File 'lib/unitwise/expression/decomposer.rb', line 49 def mode @mode end |
Class Method Details
.parse(expression) ⇒ Object
Parse an expression to an array of terms and cache the results
13 14 15 16 17 18 19 20 |
# File 'lib/unitwise/expression/decomposer.rb', line 13 def parse(expression) expression = expression.to_s if cache.key?(expression) cache[expression] elsif decomposer = new(expression) cache[expression] = decomposer end end |
.parsers ⇒ Object
22 23 24 25 26 |
# File 'lib/unitwise/expression/decomposer.rb', line 22 def parsers @parsers ||= MODES.reduce({}) do |hash, mode| hash[mode] = Parser.new(mode); hash end end |
.transformer ⇒ Object
28 29 30 |
# File 'lib/unitwise/expression/decomposer.rb', line 28 def transformer @transformer = Transformer.new end |
Instance Method Details
#parse ⇒ Object
58 59 60 61 62 63 64 |
# File 'lib/unitwise/expression/decomposer.rb', line 58 def parse self.class.parsers.reduce(nil) do |_, (mode, parser)| parsed = parser.parse(expression) rescue next @mode = mode break parsed end end |
#terms ⇒ Object
70 71 72 73 74 75 76 |
# File 'lib/unitwise/expression/decomposer.rb', line 70 def terms @terms ||= if transform.respond_to?(:terms) transform.terms else Array(transform) end end |
#transform ⇒ Object
66 67 68 |
# File 'lib/unitwise/expression/decomposer.rb', line 66 def transform @transform ||= self.class.transformer.apply(parse, :mode => mode) end |