class Unitwise::Term

A Term is the combination of an atom, prefix, factor and annotation. Not all properties have to be present. Examples: 'g', 'mm', 'mi2', '4', 'kJ{Electric Potential}'

Public Instance Methods

*(other) click to toggle source

Term multiplication. Multiply by a Unit, another Term, or a Numeric. params other [Unit, Term, Numeric] @return [Term]

# File lib/unitwise/term.rb, line 91
def *(other)
  operate('*', other) ||
    fail(TypeError, "Can't multiply #{ self } by #{ other }.")
end
**(other) click to toggle source

Term exponentiation. Raise a term to a numeric power. params other [Numeric] @return [Term]

# File lib/unitwise/term.rb, line 108
def **(other)
  if other.is_a?(Numeric)
    self.class.new(to_hash.merge(:exponent => exponent * other))
  else
    fail TypeError, "Can't raise #{self} to #{other}."
  end
end
/(other) click to toggle source

Term division. Divide by a Unit, another Term, or a Numeric. params other [Unit, Term, Numeric] @return [Term]

# File lib/unitwise/term.rb, line 99
def /(other)
  operate('/', other) ||
    fail(TypeError, "Can't divide #{ self } by #{ other }.")
end
atom=(value) click to toggle source

Set the atom. @param value [String, Atom] Either a string representing an Atom, or an Atom @api public

Calls superclass method
# File lib/unitwise/term.rb, line 12
def atom=(value)
  value.is_a?(Atom) ? super(value) : super(Atom.find(value.to_s))
end
depth() click to toggle source

Determine how far away a unit is from a base unit. @return [Integer] @api public

# File lib/unitwise/term.rb, line 32
def depth
  atom ? atom.depth + 1 : 0
end
exponent() click to toggle source

The exponent for this term. The default value is 1. @return [Numeric] @api public

Calls superclass method
# File lib/unitwise/term.rb, line 54
def exponent
  super || 1
end
factor() click to toggle source

The multiplication factor for this term. The default value is 1. @return [Numeric] @api public

Calls superclass method
# File lib/unitwise/term.rb, line 47
def factor
  super || 1
end
magnitude(scalar = scalar()) click to toggle source

Calculate the magnitude for this term @param scalar [Numeric] The scalar for which you want the magnitude @return [Numeric] The magnitude on this scale. @api public

# File lib/unitwise/term.rb, line 70
def magnitude(scalar = scalar())
  calculate(atom ? atom.magnitude(scalar) : 1)
end
prefix=(value) click to toggle source

Set the prefix. @param value [String, Prefix] Either a string representing a Prefix, or a Prefix

Calls superclass method
# File lib/unitwise/term.rb, line 19
def prefix=(value)
  value.is_a?(Prefix) ? super(value) : super(Prefix.find(value.to_s))
end
root_terms() click to toggle source

The base units this term is derived from @return [Array] An array of Unitwise::Term @api public

# File lib/unitwise/term.rb, line 77
def root_terms
  if terminal?
    [self]
  else
    atom.scale.root_terms.map do |t|
      self.class.new(:atom => t.atom, :exponent => t.exponent * exponent)
    end
  end
end
scalar(magnitude = 1) click to toggle source

The unitless scalar value for this term. @param magnitude [Numeric] The magnitude to calculate the scalar for. @return [Numeric] The unitless linear scalar value. @api public

# File lib/unitwise/term.rb, line 62
def scalar(magnitude = 1)
  calculate(atom ? atom.scalar(magnitude) : magnitude)
end
special?() click to toggle source

Is this term special? @return [true, false]

# File lib/unitwise/term.rb, line 25
def special?
  atom.special? rescue false
end
terminal?() click to toggle source

Determine if this is the last term in the scale chain @return [true, false] @api public

# File lib/unitwise/term.rb, line 40
def terminal?
  depth <= 3
end
to_s(mode = :primary_code) click to toggle source
# File lib/unitwise/term.rb, line 116
def to_s(mode = :primary_code)
  [
    (factor if factor != 1),
    (prefix.send(mode) if prefix),
    (atom.send(mode) if atom),
    (exponent if exponent != 1)
  ].compact.join('')
end

Private Instance Methods

calculate(value) click to toggle source

@api private

# File lib/unitwise/term.rb, line 128
def calculate(value)
  (factor * (prefix ? prefix.scalar : 1) * value) ** exponent
end
operate(operator, other) click to toggle source

Multiply or divide a term @api private

# File lib/unitwise/term.rb, line 134
def operate(operator, other)
  exp = operator == '/' ? -1 : 1
  if other.respond_to?(:terms)
    Unit.new(other.terms.map { |t| t ** exp } << self)
  elsif other.respond_to?(:atom)
    Unit.new([self, other ** exp])
  elsif other.is_a?(Numeric)
    self.class.new(to_hash.merge(:factor => factor.send(operator, other)))
  end
end