Class: Medidas::Unit
- Inherits:
-
Object
- Object
- Medidas::Unit
- Defined in:
- lib/medidas/unit.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#base_unit ⇒ Object
readonly
Returns the value of attribute base_unit.
Class Method Summary collapse
Instance Method Summary collapse
- #*(number) ⇒ Object
- #base_unit?(a_unit) ⇒ Boolean
- #coerce(amount) ⇒ Object
- #inspect ⇒ Object
- #name_for(amount) ⇒ Object
- #to_s ⇒ Object
Instance Attribute Details
#base_unit ⇒ Object (readonly)
Returns the value of attribute base_unit.
3 4 5 |
# File 'lib/medidas/unit.rb', line 3 def base_unit @base_unit end |
Class Method Details
.registered_units ⇒ Object
5 6 7 |
# File 'lib/medidas/unit.rb', line 5 def self.registered_units @registered_units ||= {} end |
Instance Method Details
#*(number) ⇒ Object
9 10 11 |
# File 'lib/medidas/unit.rb', line 9 def *(number) Measure.for(amount: number, unit: self) end |
#base_unit?(a_unit) ⇒ Boolean
13 14 15 |
# File 'lib/medidas/unit.rb', line 13 def base_unit?(a_unit) a_unit == base_unit end |
#coerce(amount) ⇒ Object
25 26 27 28 29 |
# File 'lib/medidas/unit.rb', line 25 def coerce(amount) raise TypeError, "Unit can't be coerced into Integer except for multiplication" unless caller.first.include?("`*'") [self, amount] end |
#inspect ⇒ Object
21 22 23 |
# File 'lib/medidas/unit.rb', line 21 def inspect "#{self.class}(#{@name})" end |
#name_for(amount) ⇒ Object
31 32 33 34 35 36 37 |
# File 'lib/medidas/unit.rb', line 31 def name_for(amount) if amount == 1 @name else @plural end end |
#to_s ⇒ Object
17 18 19 |
# File 'lib/medidas/unit.rb', line 17 def to_s @name end |