Class: CommerceUnits::Unit

Inherits:
Object
  • Object
show all
Defined in:
lib/commerce_units/unit.rb

Defined Under Namespace

Classes: RequireOrdInstance

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeUnit

Returns a new instance of Unit.



22
23
24
25
# File 'lib/commerce_units/unit.rb', line 22

def initialize
  @numerator = []
  @denominator = []
end

Instance Attribute Details

#denominatorObject

Returns the value of attribute denominator.



21
22
23
# File 'lib/commerce_units/unit.rb', line 21

def denominator
  @denominator
end

#numeratorObject

Returns the value of attribute numerator.



21
22
23
# File 'lib/commerce_units/unit.rb', line 21

def numerator
  @numerator
end

Class Method Details

.divide(ua, ub) ⇒ Object



10
11
12
# File 'lib/commerce_units/unit.rb', line 10

def divide(ua, ub)
  multiply ua, ub.flip
end

.equals?(ua, ub) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/commerce_units/unit.rb', line 13

def equals?(ua, ub)
  divide(ua, ub).unitless?
end

.multiply(ua, ub) ⇒ Object



4
5
6
7
8
9
# File 'lib/commerce_units/unit.rb', line 4

def multiply(ua, ub)
  new.tap do |u|
    u.numerator = ua.numerator + ub.numerator
    u.denominator = ua.denominator + ub.denominator
  end.simplify
end

.parse(string) ⇒ Object



16
17
18
# File 'lib/commerce_units/unit.rb', line 16

def parse(string)
  CommerceUnits::UnitParser.parse(string)
end

Instance Method Details

#*(other) ⇒ Object



27
28
29
# File 'lib/commerce_units/unit.rb', line 27

def *(other)
  self.class.multiply self, other
end

#/(other) ⇒ Object



31
32
33
# File 'lib/commerce_units/unit.rb', line 31

def /(other)
  self.class.divide self, other
end

#==(unit) ⇒ Object



70
71
72
73
74
# File 'lib/commerce_units/unit.rb', line 70

def ==(unit)
  s = self.simplify
  u = unit.simplify
  s.numerator == u.numerator && s.denominator == u.denominator
end

#flipObject



46
47
48
49
50
51
# File 'lib/commerce_units/unit.rb', line 46

def flip
  CommerceUnits::Unit.new.tap do |u|
    u.numerator = denominator
    u.denominator = numerator
  end
end

#pretty_inspectObject



80
81
82
83
84
85
# File 'lib/commerce_units/unit.rb', line 80

def pretty_inspect
  n = numerator.join(" * ")
  d = denominator.join(" * ")
  n = n.blank? ? "(1)" : n
  [n,d].reject(&:blank?).join(" / ")
end

#simplifyObject



35
36
37
38
39
40
# File 'lib/commerce_units/unit.rb', line 35

def simplify
  CommerceUnits::Unit.new.tap do |u|
    u.numerator = _simplifier.numerator
    u.denominator = _simplifier.denominator
  end
end

#to_root_dimensionObject



53
54
55
56
57
58
# File 'lib/commerce_units/unit.rb', line 53

def to_root_dimension
  CommerceUnits::Unit.new.tap do |u|
    u.numerator = CommerceUnits.dimensional_database.from_array_of_unit_names!(numerator).map(&:root_dimension)
    u.denominator = CommerceUnits.dimensional_database.from_array_of_unit_names!(denominator).map(&:root_dimension)
  end
end

#to_sObject



76
77
78
# File 'lib/commerce_units/unit.rb', line 76

def to_s
  '#<CommerceUnits::Unit:' + self.object_id.to_s + " @numerator=#{numerator}, @denominator=#{denominator}>"
end

#unitless?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/commerce_units/unit.rb', line 42

def unitless?
  numerator.blank? and denominator.blank?
end