Sha256: a34f97303b780b9ae550a409b39404624c29bd0d4eaad5f4b013b7303774055a

Contents?: true

Size: 1.5 KB

Versions: 1

Compression:

Stored size: 1.5 KB

Contents

require 'methodic_hash'

require 'units/numeric_with_units'

# A unit in the context of a particular UnitsSystem. It's a MethodicHash of
# its named qualities.
class UnitsUnit < MethodicHash
  
  # The UnitsSystem to which this instance is associated.
  attr_reader :units_system
  
  def initialize(units_system,attributes) # :nodoc:
    @units_system = units_system
    merge! attributes
    normalize
  end
  
  def ==(unit) # :nodoc:
    self.equal? unit
  end
  
  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
  
  def add_plural # :nodoc:
    self.plural =
      (self.no_plural == true) ? self.name :
      (plural = self.plural) ? plural.to_s : self.name+'s'
  end
  
  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
  
  def add_equals # :nodoc:
    self.equals = NumericWithUnits.new(1,self) unless self.equals
  end
  
  # returns the UnitsMeasure containing the UnitsSystem to which this instance
  # belongs.
  def units_measure
    units_system.units_measure
  end
  
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
eymiha_units-0.1.0 lib/units/units_unit.rb