require 'units/units_unit' require 'methodic_hash' # A UnitsSystem groups units that measure the same quality and are # conceptually organized together within a UnitsMeasure. Its a MethodicHash # of unit names to UnitsUnit instances. class UnitsSystem < MethodicHash # The UnitsMeasure to which this instance is associated. attr_reader :units_measure # The name of this UnitsSystem attr_reader :name def initialize(units_measure,name) # :nodoc: @units_measure = units_measure @name = name end # Defines a new UnitsUnit. If ten-based or two-based greeking is defined, # it is applied. def unit(attributes) unit = nil if !Units.holding_forward_reference? unit = UnitsUnit.new self, attributes self[unit.name] = unit Units.add_unit unit case unit.greek when :ten then greek_ten unit when :two then greek_two unit end else Units.hold_forward_reference nil end Units.continue_forward_reference_resolution unit end # Associates the name of a format with an output formatter in the instance's # UnitsMeasure. def format(options) @units_measure.format(options) end def greek_ten(base) # :nodoc: greek_unit base, "yocto", "y", 0.000000000000000000000001 greek_unit base, "zepto", "z", 0.000000000000000000001 greek_unit base, "atto", "a", 0.000000000000000001 greek_unit base, "femto", "f", 0.000000000000001 greek_unit base, "pico", "p", 0.000000000001 greek_unit base, "nano", "n", 0.000000001 greek_unit base, "micro", "u", 0.000001 greek_unit base, "milli", "m", 0.001 greek_unit base, "centi", "c", 0.01 greek_unit base, "deci", "d", 0.1 greek_unit base, "deca", "da", 10.0 greek_unit base, "hecto", "h", 100.0 greek_unit base, "kilo", "k", 1000.0 greek_unit base, "mega", "M", 1000000.0 greek_unit base, "giga", "G", 1000000000.0 greek_unit base, "tera", "T", 1000000000000.0 greek_unit base, "peta", "P", 1000000000000000.0 greek_unit base, "exa", "E", 1000000000000000000.0 greek_unit base, "zetta", "Z", 1000000000000000000000.0 greek_unit base, "yotta", "Y", 1000000000000000000000000.0 end def greek_two(base) # :nodoc: greek_unit base, "kilo", "k", 2**10 greek_unit base, "mega", "m", 2**20 greek_unit base, "giga", "g", 2**30 greek_unit base, "tera", "t", 2**40 greek_unit base, "peta", "p", 2**50 greek_unit base, "exa", "e", 2**60 greek_unit base, "zetta", "z", 2**70 greek_unit base, "yotta", "y", 2**80 end def greek_unit(base,prefix,abbrev_prefix,scalar) # :nodoc: unit :name => prefix+base.name, :plural => prefix+base.plural, :abbrevs => base.abbrevs.collect { |abbrev| abbrev_prefix+abbrev }, :equals => base.equals * scalar end end