Class: Beerxml::Unit

Inherits:
Object
  • Object
show all
Includes:
Temperature, Time, Volume, Weight
Defined in:
lib/beerxml/unit.rb

Overview

a work in progress.

Defined Under Namespace

Modules: Temperature, Time, Volume, Weight

Constant Summary collapse

Units =
{}
BaseUnits =
{}
UnitToType =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Temperature

included

Methods included from Time

included

Methods included from Volume

included

Methods included from Weight

included

Constructor Details

#initialize(amount, unit = nil) ⇒ Unit

Returns a new instance of Unit.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/beerxml/unit.rb', line 37

def initialize(amount, unit = nil)
  if amount.is_a?(Unit)
    if unit
      amount = amount.to(unit).to_f
    else
      amount, unit = amount.to_f, amount.unit
    end
  elsif !unit
    amount, unit = amount.to_s.split(/\s+/, 2)
  end
  unit = unit.to_s

  @type = UnitToType[unit]
  self.unit = unit
  @value = Float(amount) / Units[type][unit]
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *a, &b) ⇒ Object



91
92
93
94
95
96
97
# File 'lib/beerxml/unit.rb', line 91

def method_missing(meth, *a, &b)
  if meth.to_s =~ %r{\A(to|in)_([^!]+)(!)?\z}
    send($3 ? :in! : :in, $2)
  else
    super
  end
end

Instance Attribute Details

#typeObject (readonly)

Returns the value of attribute type.



5
6
7
# File 'lib/beerxml/unit.rb', line 5

def type
  @type
end

#unitObject (readonly)

Returns the value of attribute unit.



5
6
7
# File 'lib/beerxml/unit.rb', line 5

def unit
  @unit
end

Class Method Details

.add(type, factor, name, *aliases) ⇒ Object



67
68
69
70
71
# File 'lib/beerxml/unit.rb', line 67

def self.add(type, factor, name, *aliases)
  Units[type][name] = factor.to_f
  aliases.each { |a| add_alias(type, name, a) }
  UnitToType[name] = type
end

.add_alias(type, name, new_alias) ⇒ Object

Raises:

  • (ArgumentError)


72
73
74
75
76
77
# File 'lib/beerxml/unit.rb', line 72

def self.add_alias(type, name, new_alias)
  base = Units[type][name]
  raise(ArgumentError, "Unknown base: #{name}") unless base
  Units[type][new_alias] = base
  UnitToType[new_alias] = type
end

.add_type(type, base_unit_name, *aliases) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/beerxml/unit.rb', line 60

def self.add_type(type, base_unit_name, *aliases)
  Units[type] = {}
  Units[type][base_unit_name] = 1.0
  UnitToType[base_unit_name] = type
  BaseUnits[type] = base_unit_name
  aliases.each { |a| add_alias(type, base_unit_name, a) }
end

.apply_to_numeric!Object



139
140
141
142
143
144
145
146
147
# File 'lib/beerxml/unit.rb', line 139

def self.apply_to_numeric!
  Beerxml::Unit::UnitToType.each do |unit, v|
    Numeric.class_eval(<<-METHOD, __FILE__, __LINE__+1)
    def #{unit}
      Beerxml::Unit.new(self, #{unit.inspect})
    end
    METHOD
  end
end

Instance Method Details

#==(rhs) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/beerxml/unit.rb', line 79

def ==(rhs)
  # TODO: hard-coding this precision stinks...
  if rhs.is_a?(self.class)
    rhs = rhs.in(base_unit)
    @value.between?(rhs.to_f - 0.01, rhs.to_f + 0.01)
  elsif rhs.is_a?(Numeric)
    to_f.between?(rhs.to_f - 0.01, rhs.to_f + 0.01)
  else
    raise ArgumentError, "#{rhs.inspect} is not a #{self.class.name}"
  end
end

#base_unitObject



29
30
31
# File 'lib/beerxml/unit.rb', line 29

def base_unit
  BaseUnits[type]
end

#in(unit, *args) ⇒ Object Also known as: to



11
12
13
# File 'lib/beerxml/unit.rb', line 11

def in(unit, *args)
  self.clone.in!(unit, *args)
end

#in!(new_unit, *args) ⇒ Object Also known as: to!, unit=

Raises:

  • (ArgumentError)


16
17
18
19
20
21
22
23
24
25
# File 'lib/beerxml/unit.rb', line 16

def in!(new_unit, *args)
  new_unit = new_unit.to_s
  new_unit_type = UnitToType[new_unit]
  raise(ArgumentError, "Unknown unit: #{new_unit}") if new_unit_type.nil?
  if new_unit_type != type
    raise(ArgumentError, "New unit: #{new_unit} not compatible with current unit: #{unit}")
  end
  @unit = new_unit
  self
end

#inspectObject



99
100
101
# File 'lib/beerxml/unit.rb', line 99

def inspect
  "[#{to_f} #{unit}]"
end

#is?(unit) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
57
58
# File 'lib/beerxml/unit.rb', line 54

def is?(unit)
  self.unit == unit ||
    (UnitToType[self.unit] == UnitToType[unit] &&
     Units[type][self.unit] == Units[type][unit])
end

#to_fObject



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

def to_f
  @value * Units[type][unit]
end