Class: GeneralUnits::Base::Measurement
- Inherits:
-
Object
- Object
- GeneralUnits::Base::Measurement
- Defined in:
- lib/general_units/units/base/measurement.rb
Instance Attribute Summary collapse
-
#amount ⇒ Object
readonly
Returns the value of attribute amount.
-
#unit ⇒ Object
readonly
Returns the value of attribute unit.
Instance Method Summary collapse
- #%(val) ⇒ Object
- #*(other_object) ⇒ Object
- #+(other_object) ⇒ Object
- #-(other_object) ⇒ Object
- #-@ ⇒ Object
- #/(other_object) ⇒ Object
- #<(other_object) ⇒ Object
- #<=(other_object) ⇒ Object
- #<=>(other_object) ⇒ Object
- #==(other_object) ⇒ Object
- #>(other_object) ⇒ Object
- #>=(other_object) ⇒ Object
- #abs ⇒ Object
- #attributes ⇒ Object
- #coerce(other) ⇒ Object
- #div(value) ⇒ Object
- #divmod(val) ⇒ Object
- #eql?(other_object) ⇒ Boolean
- #formatted(round = nil, &block) ⇒ Object
-
#initialize(amount = 0, unit) ⇒ Measurement
constructor
A new instance of Measurement.
- #inspect ⇒ Object
- #measurement ⇒ Object
- #modulo(val) ⇒ Object
- #negative? ⇒ Boolean
- #nonzero? ⇒ Boolean
- #positive? ⇒ Boolean
- #remainder(val) ⇒ Object
- #to_s(round = nil) ⇒ Object
- #zero? ⇒ Boolean
Constructor Details
#initialize(amount = 0, unit) ⇒ Measurement
Returns a new instance of Measurement.
9 10 11 12 13 14 |
# File 'lib/general_units/units/base/measurement.rb', line 9 def initialize(amount = 0, unit) if unit = valid_unit?(unit) @amount = amount.try(:to_d)||0.to_d @unit = unit end end |
Instance Attribute Details
#amount ⇒ Object (readonly)
Returns the value of attribute amount.
6 7 8 |
# File 'lib/general_units/units/base/measurement.rb', line 6 def amount @amount end |
#unit ⇒ Object (readonly)
Returns the value of attribute unit.
6 7 8 |
# File 'lib/general_units/units/base/measurement.rb', line 6 def unit @unit end |
Instance Method Details
#%(val) ⇒ Object
132 133 134 |
# File 'lib/general_units/units/base/measurement.rb', line 132 def %(val) modulo(val) end |
#*(other_object) ⇒ Object
95 96 97 |
# File 'lib/general_units/units/base/measurement.rb', line 95 def *(other_object) self.class.new(amount * valid_amount(other_object), unit) end |
#+(other_object) ⇒ Object
87 88 89 |
# File 'lib/general_units/units/base/measurement.rb', line 87 def +(other_object) self.class.new(amount + valid_amount(other_object), unit) end |
#-(other_object) ⇒ Object
91 92 93 |
# File 'lib/general_units/units/base/measurement.rb', line 91 def -(other_object) self.class.new(amount - valid_amount(other_object), unit) end |
#-@ ⇒ Object
43 44 45 |
# File 'lib/general_units/units/base/measurement.rb', line 43 def -@ self.class.new(-amount, unit) end |
#/(other_object) ⇒ Object
99 100 101 |
# File 'lib/general_units/units/base/measurement.rb', line 99 def /(other_object) self.class.new(amount / valid_amount(other_object), unit) end |
#<(other_object) ⇒ Object
67 68 69 |
# File 'lib/general_units/units/base/measurement.rb', line 67 def <(other_object) amount < valid_amount(other_object) end |
#<=(other_object) ⇒ Object
75 76 77 |
# File 'lib/general_units/units/base/measurement.rb', line 75 def <=(other_object) amount <= valid_amount(other_object) end |
#<=>(other_object) ⇒ Object
57 58 59 60 61 |
# File 'lib/general_units/units/base/measurement.rb', line 57 def <=>(other_object) amount <=> valid_amount(other_object) rescue NoMethodError raise ArgumentError, "Comparison of #{self.class} with #{val.inspect} failed" end |
#==(other_object) ⇒ Object
47 48 49 50 51 |
# File 'lib/general_units/units/base/measurement.rb', line 47 def ==(other_object) amount == valid_amount(other_object) rescue NoMethodError false end |
#>(other_object) ⇒ Object
63 64 65 |
# File 'lib/general_units/units/base/measurement.rb', line 63 def >(other_object) amount > valid_amount(other_object) end |
#>=(other_object) ⇒ Object
71 72 73 |
# File 'lib/general_units/units/base/measurement.rb', line 71 def >=(other_object) amount >= valid_amount(other_object) end |
#abs ⇒ Object
148 149 150 |
# File 'lib/general_units/units/base/measurement.rb', line 148 def abs self.class.new(amount.abs, unit) end |
#attributes ⇒ Object
16 17 18 |
# File 'lib/general_units/units/base/measurement.rb', line 16 def attributes {:amount => amount, :unit => unit} end |
#coerce(other) ⇒ Object
160 161 162 |
# File 'lib/general_units/units/base/measurement.rb', line 160 def coerce(other) [self, other] end |
#div(value) ⇒ Object
103 104 105 |
# File 'lib/general_units/units/base/measurement.rb', line 103 def div(value) amount.div(value) end |
#divmod(val) ⇒ Object
107 108 109 110 111 112 113 |
# File 'lib/general_units/units/base/measurement.rb', line 107 def divmod(val) if val.is_a?(self.class) divmod_object(val) else divmod_other(val) end end |
#eql?(other_object) ⇒ Boolean
53 54 55 |
# File 'lib/general_units/units/base/measurement.rb', line 53 def eql?(other_object) self == other_object end |
#formatted(round = nil, &block) ⇒ Object
28 29 30 31 32 33 34 |
# File 'lib/general_units/units/base/measurement.rb', line 28 def formatted(round = nil, &block) if block_given? yield to_s(round), unit else "#{to_s(round)} #{unit.short}" end end |
#inspect ⇒ Object
24 25 26 |
# File 'lib/general_units/units/base/measurement.rb', line 24 def inspect "<#{self.class.name} amount=#{amount} unit=#{unit}>" end |
#measurement ⇒ Object
39 40 41 |
# File 'lib/general_units/units/base/measurement.rb', line 39 def measurement self.class.name.split("::").last.underscore end |
#modulo(val) ⇒ Object
128 129 130 |
# File 'lib/general_units/units/base/measurement.rb', line 128 def modulo(val) divmod(val)[1] end |
#negative? ⇒ Boolean
83 84 85 |
# File 'lib/general_units/units/base/measurement.rb', line 83 def negative? self < 0 end |
#nonzero? ⇒ Boolean
156 157 158 |
# File 'lib/general_units/units/base/measurement.rb', line 156 def nonzero? amount != 0 ? self : nil end |
#positive? ⇒ Boolean
79 80 81 |
# File 'lib/general_units/units/base/measurement.rb', line 79 def positive? self > 0 end |
#remainder(val) ⇒ Object
136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/general_units/units/base/measurement.rb', line 136 def remainder(val) if val.is_a?(self.class) && unit != val.unit val = val.convert_to(unit) end if (amount < 0 && val < 0) || (amount > 0 && val > 0) self.modulo(val) else self.modulo(val) - (val.is_a?(self.class) ? val : self.class.new(val, unit)) end end |
#to_s(round = nil) ⇒ Object
20 21 22 |
# File 'lib/general_units/units/base/measurement.rb', line 20 def to_s(round = nil) "#{to_f.divmod(1).last == 0 ? to_f.round(0) : to_f.round(round||2)}" end |
#zero? ⇒ Boolean
152 153 154 |
# File 'lib/general_units/units/base/measurement.rb', line 152 def zero? amount == 0 end |