lib/unitwise/unit.rb in unitwise-1.0.1 vs lib/unitwise/unit.rb in unitwise-1.0.2

- old
+ new

@@ -25,11 +25,11 @@ # @return [Array] # @api public def terms unless frozen? unless @terms - decomposer = Expression::Decomposer.new(@expression) + decomposer = Expression.decompose(@expression) @mode = decomposer.mode @terms = decomposer.terms end freeze end @@ -39,15 +39,18 @@ # Build a string representation of this unit by it's terms. # @param mode [Symbol] The mode to use to stringify the atoms # (:primary_code, :names, :secondary_code). # @return [String] # @api public - def expression(mode = mode) - Expression.compose(terms, mode) + def expression(mode=nil) + if @expression && (mode.nil? || mode == self.mode) + @expression + else + Expression.compose(terms, mode || self.mode) + end end - # The collection of atoms that compose this unit. Essentially delegated to # terms. # @return [Array] # @api public def atoms @@ -103,37 +106,24 @@ # Multiply this unit by another unit, term, or number. # @param other [Unitwise::Unit, Unitwise::Term, Numeric] # @return [Unitwise::Unit] # @api public def *(other) - if other.respond_to?(:terms) - self.class.new(terms + other.terms) - elsif other.respond_to?(:atom) - self.class.new(terms << other) - elsif other.is_a?(Numeric) - self.class.new(terms.map { |t| t * other }) - else - fail TypeError, "Can't multiply #{self} by #{other}." - end + operate('*', other) || + fail(TypeError, "Can't multiply #{ self } by #{ other }.") end # Divide this unit by another unit,term, or number. # @param other [Unitwise::Unit, Unitwise::Term, Numeric] # @return [Unitwise::Unit] # @api public def /(other) - if other.respond_to?(:terms) - self.class.new(terms + other.terms.map { |t| t ** -1 }) - elsif other.respond_to?(:atom) - self.class.new(terms << other ** -1) - elsif other.is_a?(Numeric) - self.class.new(terms.map { |t| t / other }) - else - fail TypeError, "Can't divide #{self} by #{other}." - end + operate('/', other) || + fail(TypeError, "Can't divide #{ self } by #{ other }.") end + # Raise this unit to a numeric power. # @param other [Numeric] # @return [Unitwise::Unit] # @api public def **(other) @@ -147,12 +137,12 @@ # A string representation of this unit. # @param mode [:symbol] The mode used to represent the unit # (:primary_code, :names, :secondary_code) # @return [String] # @api public - def to_s(mode = mode) - expression(mode || self.mode) + def to_s(mode = nil) + expression(mode) end # A collection of the possible string representations of this unit. # Primarily used by Unitwise::Search. # @return [Array] @@ -168,9 +158,24 @@ # @return [Symbol] # @api semipublic def mode terms @mode || :primary_code + end + + private + + # Multiply or divide units + # @api private + def operate(operator, other) + exp = operator == '/' ? -1 : 1 + if other.respond_to?(:terms) + self.class.new(terms + other.terms.map { |t| t ** exp }) + elsif other.respond_to?(:atom) + self.class.new(terms << other ** exp) + elsif other.is_a?(Numeric) + self.class.new(terms.map { |t| t.send(operator, other) }) + end end end end