Class: Unit
- Inherits:
-
Object
- Object
- Unit
- Defined in:
- lib/amee_rails_layer/unit.rb
Overview
Encapsulates the Units used by the AMEE API. Possible types are currently :km, :miles, :kg, :tonnes, :kwh, :litres and :uk_gallons Convience class methods are provided to construct an object of each of these types
Constant Summary collapse
- NAME =
{ :km => "km", :miles => "miles", :kg => "kg", :tonnes => "tonnes", :kwh => "kWh", :litres => "litres", :uk_gallons => "UK Gallons" }
- AMEE_API_UNITS =
{ :km => "km", :miles => "mi", :kg => "kg", :tonnes => "t", :kwh => "kWh", :litres => "L", :uk_gallons => "gal_uk" }
Class Method Summary collapse
-
.from_amee_unit(unit) ⇒ Object
Creates a new Unit class from the string used by AMEE to represent the unit.
- .kg ⇒ Object
- .km ⇒ Object
- .kwh ⇒ Object
- .litres ⇒ Object
- .miles ⇒ Object
- .tonnes ⇒ Object
- .uk_gallons ⇒ Object
Instance Method Summary collapse
-
#amee_api_unit ⇒ Object
The string used by the AMEE API to represent the unit.
-
#initialize(type, *args) ⇒ Unit
constructor
Creates a new Unit object from the symbol representing the unit (see class doc).
-
#name ⇒ Object
A human readable form of the unit.
Constructor Details
#initialize(type, *args) ⇒ Unit
Creates a new Unit object from the symbol representing the unit (see class doc)
27 28 29 |
# File 'lib/amee_rails_layer/unit.rb', line 27 def initialize(type, *args) @type = type end |
Class Method Details
.from_amee_unit(unit) ⇒ Object
Creates a new Unit class from the string used by AMEE to represent the unit. For example pass in âtâ to initialize an Unit object for tonnes
33 34 35 36 37 38 |
# File 'lib/amee_rails_layer/unit.rb', line 33 def self.from_amee_unit(unit) AMEE_API_UNITS.each do |key, value| return new(key) if value == unit end return nil end |
.kg ⇒ Object
58 59 60 |
# File 'lib/amee_rails_layer/unit.rb', line 58 def self.kg new(:kg) end |
.km ⇒ Object
50 51 52 |
# File 'lib/amee_rails_layer/unit.rb', line 50 def self.km new(:km) end |
.kwh ⇒ Object
66 67 68 |
# File 'lib/amee_rails_layer/unit.rb', line 66 def self.kwh new(:kwh) end |
.litres ⇒ Object
70 71 72 |
# File 'lib/amee_rails_layer/unit.rb', line 70 def self.litres new(:litres) end |
.miles ⇒ Object
54 55 56 |
# File 'lib/amee_rails_layer/unit.rb', line 54 def self.miles new(:miles) end |
.tonnes ⇒ Object
62 63 64 |
# File 'lib/amee_rails_layer/unit.rb', line 62 def self.tonnes new(:tonnes) end |
.uk_gallons ⇒ Object
74 75 76 |
# File 'lib/amee_rails_layer/unit.rb', line 74 def self.uk_gallons new(:uk_gallons) end |
Instance Method Details
#amee_api_unit ⇒ Object
The string used by the AMEE API to represent the unit
46 47 48 |
# File 'lib/amee_rails_layer/unit.rb', line 46 def amee_api_unit AMEE_API_UNITS[@type] end |
#name ⇒ Object
A human readable form of the unit
41 42 43 |
# File 'lib/amee_rails_layer/unit.rb', line 41 def name NAME[@type] end |