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

Instance Method Summary collapse

Instance Attribute Details

#quantityObject

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.

Parameters:

  • type (String)

    the type of unit the unit should be.



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

Parameters:

  • type (Symbol)

    the type of unit to convert to

Returns:

  • (BaseUnit)

    the new unit, it will be a subclass of [BaseUnit].

Raises:

  • (InvalidConversionError)

    gets raised if the type of conversion is not valid.



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

#inspectObject

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_convertBaseUnit

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.

Returns:

  • (BaseUnit)

    the new unit, it will be the unit that is the bestfit conversion.



95
96
97
# File 'lib/measurements/unit/baseunit.rb', line 95

def smart_convert
    #TODO
end

#typeObject (writeonly)



33
34
35
# File 'lib/measurements/unit/baseunit.rb', line 33

def type
    @type
end

#unitObject



40
41
42
# File 'lib/measurements/unit/baseunit.rb', line 40

def unit
    self.class.name.split('::').last.downcase.to_s
end

#unit_abbrObject



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_systemObject



46
47
48
# File 'lib/measurements/unit/baseunit.rb', line 46

def unit_system
    eval(self.class.to_s + "::UNIT_SYSTEM")
end

#unit_typeObject



52
53
54
# File 'lib/measurements/unit/baseunit.rb', line 52

def unit_type
    eval(self.class.to_s + "::UNIT_TYPE")
end