Module: Measurements::Unit::BaseUnit
- Included in:
- Chain, Cup, Foot, Furlong, Gallon, Inch, League, Mile, Ounce, Pint, Pound, Quart, Tablespoon, Teaspoon, Thou, Yard
- Defined in:
- lib/measurements/unit/baseunit.rb
Instance Attribute Summary collapse
-
#quantity ⇒ Object
The quantity of a unit.
-
#type ⇒ Object
writeonly
Set the unit type of a unit manually.
Instance Method Summary collapse
-
#convert_to(type) ⇒ BaseUnit
Convert the current unit into a new unit of the type given.
- #initialize(quantity, type = nil) ⇒ Object
-
#inspect ⇒ Object
When you look at a unit object the quantity will be displayed.
-
#smart_convert ⇒ BaseUnit
Convert the current unit into a new unit of highest bestfit unit that would make sense while measuring something.
- #type ⇒ Object writeonly
- #unit ⇒ Object
- #unit_abbr ⇒ Object
- #unit_system ⇒ Object
- #unit_type ⇒ Object
Instance Attribute Details
#quantity ⇒ Object
The quantity of a unit
7 8 9 |
# File 'lib/measurements/unit/baseunit.rb', line 7 def quantity @quantity end |
#type=(value) ⇒ Object (writeonly)
Set the unit type of a unit manually. A type can only be set for units with the unit type of "neutral", any other unit type will raise and error.
23 24 25 26 27 28 29 |
# File 'lib/measurements/unit/baseunit.rb', line 23 def type=(type) if self.unit_type.eql? "neutral" @type = type else raise Measurements::Exception::InvalidTypeSettingError, "Types can only be set on neutral units." end end |
Instance Method Details
#convert_to(type) ⇒ BaseUnit
Convert the current unit into a new unit of the type given
76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/measurements/unit/baseunit.rb', line 76 def convert_to(type) type = type.to_s if !validate_system(type) raise Measurements::Exception::InvalidConversionError, "A conversion must be from the same system type." end if validate_conversion(type) base = convert_to_base(self, type) return convert_to_type(base, type) else raise Measurements::Exception::InvalidConversionError, "A conversion must be from the same type or neutral." end end |
#initialize(quantity, type = nil) ⇒ Object
9 10 11 12 13 14 15 16 17 |
# File 'lib/measurements/unit/baseunit.rb', line 9 def initialize(quantity, type = nil) @quantity = quantity.to_f if !type.nil? self.type = type else @type = type end end |
#inspect ⇒ Object
When you look at a unit object the quantity will be displayed. It seemed like a nicer way to display the unit, kind of like a [Float]
68 69 70 |
# File 'lib/measurements/unit/baseunit.rb', line 68 def inspect @quantity end |
#smart_convert ⇒ BaseUnit
Convert the current unit into a new unit of highest bestfit unit that would make sense while measuring something. An example would be that a better way to display 6 tsps would be 2 tbsps.
95 96 97 |
# File 'lib/measurements/unit/baseunit.rb', line 95 def smart_convert #TODO end |
#type ⇒ Object (writeonly)
33 34 35 |
# File 'lib/measurements/unit/baseunit.rb', line 33 def type @type end |
#unit ⇒ Object
40 41 42 |
# File 'lib/measurements/unit/baseunit.rb', line 40 def unit self.class.name.split('::').last.downcase.to_s end |
#unit_abbr ⇒ Object
58 59 60 61 62 63 64 |
# File 'lib/measurements/unit/baseunit.rb', line 58 def unit_abbr if @quantity <= 1 Measurements::Unit::ABBREVIATIONS["abbreviations"][Measurements::LOCALE]["singular"][self.unit] else Measurements::Unit::ABBREVIATIONS["abbreviations"][Measurements::LOCALE]["plural"][self.unit] end end |
#unit_system ⇒ Object
46 47 48 |
# File 'lib/measurements/unit/baseunit.rb', line 46 def unit_system eval(self.class.to_s + "::UNIT_SYSTEM") end |
#unit_type ⇒ Object
52 53 54 |
# File 'lib/measurements/unit/baseunit.rb', line 52 def unit_type eval(self.class.to_s + "::UNIT_TYPE") end |