Class: Eymiha::UnitsUnit

Inherits:
MethodicHash
  • Object
show all
Defined in:
lib/eymiha/units/units_unit.rb

Overview

A unit in the context of a particular UnitsSystem. It’s a MethodicHash of its named qualities.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(units_system, attributes) ⇒ UnitsUnit

:nodoc:



13
14
15
16
17
# File 'lib/eymiha/units/units_unit.rb', line 13

def initialize(units_system,attributes) # :nodoc:
  @units_system = units_system
  merge! attributes
  normalize
end

Instance Attribute Details

#units_systemObject (readonly)

The UnitsSystem to which this instance is associated.



11
12
13
# File 'lib/eymiha/units/units_unit.rb', line 11

def units_system
  @units_system
end

Instance Method Details

#==(unit) ⇒ Object

:nodoc:



19
20
21
# File 'lib/eymiha/units/units_unit.rb', line 19

def ==(unit) # :nodoc:
  self.equal? unit
end

#add_abbrevs(attribute = nil) ⇒ Object

:nodoc:



40
41
42
43
44
45
46
47
48
49
# File 'lib/eymiha/units/units_unit.rb', line 40

def add_abbrevs(attribute = nil) # :nodoc:
  if attribute == nil
    self.abbrevs = add_abbrevs(:abbrev)+add_abbrevs(:abbrevs)
  elsif (value = self[attribute])
    self.delete(attribute)
    (value.kind_of? Array) ? value.collect {|e| e.to_s } : [ value.to_s ]
  else
    [ ]
  end
end

#add_equalsObject

:nodoc:



51
52
53
# File 'lib/eymiha/units/units_unit.rb', line 51

def add_equals # :nodoc:
  self.equals = NumericWithUnits.new(1,self) unless self.equals
end

#add_pluralObject

:nodoc:



34
35
36
37
38
# File 'lib/eymiha/units/units_unit.rb', line 34

def add_plural # :nodoc:
  self.plural =
    (self.no_plural == true) ? self.name :
    (plural = self.plural) ? plural.to_s : self.name+'s'
end

#normalizeObject

:nodoc:

Raises:



23
24
25
26
27
28
29
30
31
32
# File 'lib/eymiha/units/units_unit.rb', line 23

def normalize # :nodoc:
  raise UnitsException.new("UnitUnits must have a name attribute") unless
    self.name
  self.name = self.name.to_s
  add_plural
  add_abbrevs
  add_equals
  equals.each { |n| n.promote_original } if equals.kind_of? Array
  self
end

#units_measureObject

returns the UnitsMeasure containing the UnitsSystem to which this instance belongs.



57
58
59
# File 'lib/eymiha/units/units_unit.rb', line 57

def units_measure
  units_system.units_measure
end