Class: Unitwise::Term

Inherits:
Object
  • Object
show all
Includes:
Compatible
Defined in:
lib/unitwise/term.rb

Overview

A Term is the combination of an atom, prefix, factor and annotation. Not all properties have to be present. Examples: ‘g’, ‘mm’, ‘mi2’, ‘4’, ‘kJPotential’

Instance Method Summary collapse

Methods included from Compatible

#<=>, #compatible_with?, #composition, #composition_string, #dim, included, #initialize

Instance Method Details

#*(other) ⇒ Term

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

Returns:



91
92
93
94
# File 'lib/unitwise/term.rb', line 91

def *(other)
  operate('*', other) ||
    fail(TypeError, "Can't multiply #{ self } by #{ other }.")
end

#**(other) ⇒ Term

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

Returns:



108
109
110
111
112
113
114
# 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) ⇒ Term

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

Returns:



99
100
101
102
# File 'lib/unitwise/term.rb', line 99

def /(other)
  operate('/', other) ||
    fail(TypeError, "Can't divide #{ self } by #{ other }.")
end

#atom=(value) ⇒ Object

Set the atom. Atom

Parameters:

  • value (String, Atom)

    Either a string representing an Atom, or an



12
13
14
# File 'lib/unitwise/term.rb', line 12

def atom=(value)
  value.is_a?(Atom) ? super(value) : super(Atom.find(value.to_s))
end

#depthInteger

Determine how far away a unit is from a base unit.

Returns:

  • (Integer)


32
33
34
# File 'lib/unitwise/term.rb', line 32

def depth
  atom ? atom.depth + 1 : 0
end

#exponentNumeric

The exponent for this term. The default value is 1.

Returns:

  • (Numeric)


54
55
56
# File 'lib/unitwise/term.rb', line 54

def exponent
  super || 1
end

#factorNumeric

The multiplication factor for this term. The default value is 1.

Returns:

  • (Numeric)


47
48
49
# File 'lib/unitwise/term.rb', line 47

def factor
  super || 1
end

#magnitude(scalar = scalar()) ⇒ Numeric

Calculate the magnitude for this term

Parameters:

  • scalar (Numeric) (defaults to: scalar())

    The scalar for which you want the magnitude

Returns:

  • (Numeric)

    The magnitude on this scale.



70
71
72
# File 'lib/unitwise/term.rb', line 70

def magnitude(scalar = scalar())
  calculate(atom ? atom.magnitude(scalar) : 1)
end

#prefix=(value) ⇒ Object

Set the prefix. a Prefix

Parameters:

  • value (String, Prefix)

    Either a string representing a Prefix, or



19
20
21
# File 'lib/unitwise/term.rb', line 19

def prefix=(value)
  value.is_a?(Prefix) ? super(value) : super(Prefix.find(value.to_s))
end

#root_termsArray

The base units this term is derived from

Returns:

  • (Array)

    An array of Unitwise::Term



77
78
79
80
81
82
83
84
85
# 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) ⇒ Numeric

The unitless scalar value for this term.

Parameters:

  • magnitude (Numeric) (defaults to: 1)

    The magnitude to calculate the scalar for.

Returns:

  • (Numeric)

    The unitless linear scalar value.



62
63
64
# File 'lib/unitwise/term.rb', line 62

def scalar(magnitude = 1)
  calculate(atom ? atom.scalar(magnitude) : magnitude)
end

#special?true, false

Is this term special?

Returns:

  • (true, false)


25
26
27
# File 'lib/unitwise/term.rb', line 25

def special?
  atom.special? rescue false
end

#terminal?true, false

Determine if this is the last term in the scale chain

Returns:

  • (true, false)


40
41
42
# File 'lib/unitwise/term.rb', line 40

def terminal?
  depth <= 3
end

#to_s(mode = :primary_code) ⇒ Object



116
117
118
119
120
121
122
123
# 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