lib/unitwise/unit.rb in unitwise-0.6.1 vs lib/unitwise/unit.rb in unitwise-0.6.2

- old
+ new

@@ -2,11 +2,11 @@ # A Unit is essentially a collection of Unitwise::Term. Terms can be combined # through multiplication or division to create a unit. A unit does not have # a magnitude, but it does have a scale. class Unit liner :expression, :terms - include Unitwise::Compatible + include Compatible # Create a new unit. You can send an expression or a collection of terms # @param input [String, Unit, [Term]] A string expression, a unit, or a # collection of tems. def initialize(input) @@ -15,35 +15,37 @@ @expression = input.expression when String, Symbol @expression = input.to_s else @terms = input + @expression = Expression.compose(input) end + freeze end - def expression - @expression ||= (Expression.compose(@terms) if @terms) - end - def terms - @terms ||= (Expression.decompose(@expression) if @expression) + @terms || Expression.decompose(expression) end def atoms terms.map(&:atom) end + memoize :atoms def special? terms.count == 1 && terms.all?(&:special?) end + memoize :special? def depth terms.map(&:depth).max + 1 end + memoize :depth def root_terms terms.map(&:root_terms).flatten end + memoize :root_terms def scalar(magnitude = 1) terms.reduce(1) do |prod, term| prod * term.scalar(magnitude) end