Class: CommerceUnits::Unit
- Inherits:
-
Object
- Object
- CommerceUnits::Unit
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
#initialize ⇒ Unit
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
#denominator ⇒ Object
Returns the value of attribute denominator.
21
22
23
|
# File 'lib/commerce_units/unit.rb', line 21
def denominator
@denominator
end
|
#numerator ⇒ Object
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
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
|
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
|
#flip ⇒ Object
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_inspect ⇒ Object
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
|
#simplify ⇒ Object
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_dimension ⇒ Object
#to_s ⇒ Object
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
42
43
44
|
# File 'lib/commerce_units/unit.rb', line 42
def unitless?
numerator.blank? and denominator.blank?
end
|