lib/unitwise/unit.rb in unitwise-0.8.1 vs lib/unitwise/unit.rb in unitwise-0.9.0
- old
+ new
@@ -15,19 +15,29 @@
@expression = input.expression
when String, Symbol
@expression = input.to_s
else
@terms = input
- @expression = Expression.compose(input)
end
- freeze
end
def terms
- @terms || Expression.decompose(expression)
+ unless frozen?
+ unless @terms
+ decomposer = Expression::Decomposer.new(@expression)
+ @mode = decomposer.mode
+ @terms = decomposer.terms
+ end
+ freeze
+ end
+ @terms
end
+ def expression(mode = mode)
+ Expression.compose(terms, mode)
+ end
+
def atoms
terms.map(&:atom)
end
memoize :atoms
@@ -86,10 +96,16 @@
else
fail TypeError, "Can't raise #{self} to #{other}."
end
end
- def to_s
- expression
+ def to_s(mode = mode)
+ expression(mode || self.mode)
end
+
+ def mode
+ terms
+ @mode || :primary_code
+ end
+
end
end