Class: Medidas::Measure
- Inherits:
-
Object
- Object
- Medidas::Measure
- Includes:
- Comparable
- Defined in:
- lib/medidas/measure.rb
Instance Attribute Summary collapse
-
#amount ⇒ Object
readonly
Returns the value of attribute amount.
-
#unit ⇒ Object
readonly
Returns the value of attribute unit.
Class Method Summary collapse
Instance Method Summary collapse
- #*(a_number) ⇒ Object
- #+(other_measure) ⇒ Object
- #-(other_measure) ⇒ Object
- #-@ ⇒ Object
- #/(a_divisor) ⇒ Object
- #<=>(other_measure) ⇒ Object
- #base_unit ⇒ Object
- #base_unit?(a_unit) ⇒ Boolean
- #convert_to(unit) ⇒ Object
-
#initialize(amount:, unit:) ⇒ Measure
constructor
A new instance of Measure.
- #inspect ⇒ Object
- #to_base_unit ⇒ Object
- #to_s ⇒ Object
- #unit?(potential_unit) ⇒ Boolean
Constructor Details
#initialize(amount:, unit:) ⇒ Measure
Returns a new instance of Measure.
16 17 18 19 |
# File 'lib/medidas/measure.rb', line 16 def initialize(amount:, unit:) @amount = BigDecimal(amount.to_s) @unit = unit end |
Instance Attribute Details
#amount ⇒ Object (readonly)
Returns the value of attribute amount.
7 8 9 |
# File 'lib/medidas/measure.rb', line 7 def amount @amount end |
#unit ⇒ Object (readonly)
Returns the value of attribute unit.
8 9 10 |
# File 'lib/medidas/measure.rb', line 8 def unit @unit end |
Class Method Details
.for(amount:, unit:) ⇒ Object
12 13 14 |
# File 'lib/medidas/measure.rb', line 12 def self.for(amount:, unit:) @registered_measures[[amount, unit]] ||= new(amount: amount, unit: unit) end |
Instance Method Details
#*(a_number) ⇒ Object
33 34 35 36 37 |
# File 'lib/medidas/measure.rb', line 33 def *(a_number) raise CANT_APPLY_OPERATION unless a_number.is_a?(Numeric) (a_number * amount) * unit end |
#+(other_measure) ⇒ Object
21 22 23 24 25 |
# File 'lib/medidas/measure.rb', line 21 def +(other_measure) assert_same_unit!(other_measure) unit * (amount + other_measure.amount) end |
#-(other_measure) ⇒ Object
27 28 29 30 31 |
# File 'lib/medidas/measure.rb', line 27 def -(other_measure) assert_same_unit!(other_measure) unit * (amount - other_measure.amount) end |
#-@ ⇒ Object
49 50 51 |
# File 'lib/medidas/measure.rb', line 49 def -@ unit * (amount * -1) end |
#/(a_divisor) ⇒ Object
39 40 41 42 43 44 45 46 47 |
# File 'lib/medidas/measure.rb', line 39 def /(a_divisor) raise CANT_APPLY_OPERATION if a_divisor.is_a?(Measure) && !a_divisor.unit?(unit) if a_divisor.is_a?(Numeric) (amount / a_divisor) * unit else amount / a_divisor.amount end end |
#<=>(other_measure) ⇒ Object
69 70 71 72 73 |
# File 'lib/medidas/measure.rb', line 69 def <=>(other_measure) raise CANT_APPLY_OPERATION unless other_measure.is_a?(Measure) && other_measure.base_unit?(unit.base_unit) to_base_unit.amount <=> other_measure.to_base_unit.amount end |
#base_unit ⇒ Object
61 62 63 |
# File 'lib/medidas/measure.rb', line 61 def base_unit unit.base_unit end |
#base_unit?(a_unit) ⇒ Boolean
57 58 59 |
# File 'lib/medidas/measure.rb', line 57 def base_unit?(a_unit) unit.base_unit?(a_unit) end |
#convert_to(unit) ⇒ Object
65 66 67 |
# File 'lib/medidas/measure.rb', line 65 def convert_to(unit) (to_base_unit.amount / unit.factor) * unit end |
#inspect ⇒ Object
79 80 81 |
# File 'lib/medidas/measure.rb', line 79 def inspect "#{self.class}(#{text_for_amount} #{unit.to_s})" end |
#to_base_unit ⇒ Object
53 54 55 |
# File 'lib/medidas/measure.rb', line 53 def to_base_unit amount * unit.factor * unit.base_unit end |
#to_s ⇒ Object
75 76 77 |
# File 'lib/medidas/measure.rb', line 75 def to_s "#{text_for_amount} #{unit.name_for(amount)}" end |
#unit?(potential_unit) ⇒ Boolean
83 84 85 |
# File 'lib/medidas/measure.rb', line 83 def unit?(potential_unit) unit == potential_unit end |