Sha256: d40bf320baa53db490bb6fdbb019d8107b8893ddad3516a7fb7251634aba9338

Contents?: true

Size: 1.65 KB

Versions: 2

Compression:

Stored size: 1.65 KB

Contents

require 'eymiha/units/numeric_with_units'
require 'eymiha/util/methodic_hash'

module Eymiha

  # 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

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
eymiha_units-1.0.1 lib/eymiha/units/units_unit.rb
eymiha_units-1.0.0 lib/eymiha/units/units_unit.rb