Class: Unit

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

.kgObject



58
59
60
# File 'lib/amee_rails_layer/unit.rb', line 58

def self.kg
  new(:kg)
end

.kmObject



50
51
52
# File 'lib/amee_rails_layer/unit.rb', line 50

def self.km
  new(:km)
end

.kwhObject



66
67
68
# File 'lib/amee_rails_layer/unit.rb', line 66

def self.kwh
  new(:kwh)
end

.litresObject



70
71
72
# File 'lib/amee_rails_layer/unit.rb', line 70

def self.litres
  new(:litres)
end

.milesObject



54
55
56
# File 'lib/amee_rails_layer/unit.rb', line 54

def self.miles
  new(:miles)
end

.tonnesObject



62
63
64
# File 'lib/amee_rails_layer/unit.rb', line 62

def self.tonnes
  new(:tonnes)
end

.uk_gallonsObject



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_unitObject

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

#nameObject

A human readable form of the unit



41
42
43
# File 'lib/amee_rails_layer/unit.rb', line 41

def name
  NAME[@type]
end