Class: Unitwise::Scale

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

Overview

A Unitwise::Scale represents a value and a unit, sort of like a vector, it has two components. In this case, it’s a value and unit rather than a magnitude and direction. This class should be considered mostly privateish.

Direct Known Subclasses

Functional, Measurement

Instance Method Summary collapse

Methods included from Compatible

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

Instance Method Details

#atomsArray

List the atoms associated with this scale’s unit.

Returns:

  • (Array)


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

def atoms
  unit.atoms
end

#depthInteger

How far away is this instances unit from the deepest leve atom.

Returns:

  • (Integer)


77
78
79
# File 'lib/unitwise/scale.rb', line 77

def depth
  unit.depth + 1
end

#eql?(other) ⇒ Boolean

Redefine hash equality to match the hashes

Returns:

  • (Boolean)


120
121
122
# File 'lib/unitwise/scale.rb', line 120

def eql?(other)
  hash == other.hash
end

#expressionObject



97
98
99
# File 'lib/unitwise/scale.rb', line 97

def expression
  unit.expression
end

#hashObject

Redefine hash for apropriate hash/key lookup



113
114
115
# File 'lib/unitwise/scale.rb', line 113

def hash
  [value, unit.to_s, self.class].hash
end

#inspectObject



107
108
109
# File 'lib/unitwise/scale.rb', line 107

def inspect
  "#<#{self.class} value=#{simplified_value} unit=#{unit}>"
end

#magnitude(scalar = scalar) ⇒ Numeric

Get a magnitude based on a linear scale value. Only used by scales with special atoms in it’s hierarchy.

Parameters:

  • scalar (Numeric) (defaults to: scalar)

    A linear scalar value

Returns:

  • (Numeric)

    The equivalent magnitude on this scale



58
59
60
61
62
63
64
# File 'lib/unitwise/scale.rb', line 58

def magnitude(scalar = scalar)
  if special?
    unit.magnitude(scalar)
  else
    value * unit.magnitude
  end
end

#root_termsArray

The base terms this scale’s unit is derived from

Returns:

  • (Array)

    An array of Unitwise::Term



69
70
71
# File 'lib/unitwise/scale.rb', line 69

def root_terms
  unit.root_terms
end

#scalar(magnitude = value) ⇒ Numeric

Get a scalar value for this scale.

Parameters:

  • magnitude (Numeric) (defaults to: value)

    An optional magnitude on this scale.

Returns:

  • (Numeric)

    A scalar value on a linear scale



45
46
47
48
49
50
51
# File 'lib/unitwise/scale.rb', line 45

def scalar(magnitude = value)
  if special?
    unit.scalar(magnitude)
  else
    value * unit.scalar
  end
end

#simplified_valueObject



82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/unitwise/scale.rb', line 82

def simplified_value
  if value.is_a?(Integer)
    value
  elsif (i = Integer(value)) == value
    i
  elsif value.is_a?(Float)
    value
  elsif (f = Float(value)) == value
    f
  else
    value
  end
end

#special?true, false

Is this scale’s unit special?

Returns:

  • (true, false)


37
38
39
# File 'lib/unitwise/scale.rb', line 37

def special?
  unit.special?
end

#termsArray

List the terms associated with this scale’s unit.

Returns:

  • (Array)


30
31
32
# File 'lib/unitwise/scale.rb', line 30

def terms
  unit.terms
end

#to_s(mode = nil) ⇒ Object

Convert to a simple string representing the scale.



103
104
105
# File 'lib/unitwise/scale.rb', line 103

def to_s(mode = nil)
  "#{simplified_value} #{unit.to_s(mode)}"
end

#unit=(value) ⇒ Object

Set the unit vector.

Parameters:



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

def unit=(value)
  @unit = value.is_a?(Unit) ? value : Unit.new(value)
end

#value=(value) ⇒ Object



23
24
25
# File 'lib/unitwise/scale.rb', line 23

def value=(value)
  @value = BigDecimal(value.to_s)
end