class Quantify::Unit::CompoundBaseUnit

Attributes

index[RW]

Container class for compound unit base units. Each instance is represented by a unit and an index, i.e. a unit raised to some power. If no index is present, 1 is assumed.

Instances of this class can be used to initialize base units, and are the structures which hold base units within compound units

unit[RW]

Container class for compound unit base units. Each instance is represented by a unit and an index, i.e. a unit raised to some power. If no index is present, 1 is assumed.

Instances of this class can be used to initialize base units, and are the structures which hold base units within compound units

Public Class Methods

new(unit,index=1) click to toggle source
# File lib/quantify/unit/compound_base_unit.rb, line 16
def initialize(unit,index=1)
  @unit = Unit.match(unit) || raise(Exceptions::InvalidUnitError, "Base unit not known: #{unit}")
  raise Exceptions::InvalidUnitError, "Base unit cannot be compound unit" if @unit.is_a? Compound
  @index = index
end

Public Instance Methods

dimensions() click to toggle source
# File lib/quantify/unit/compound_base_unit.rb, line 22
def dimensions
  @unit.dimensions ** @index
end
factor() click to toggle source
# File lib/quantify/unit/compound_base_unit.rb, line 50
def factor
  @unit.factor ** @index
end
initialize_copy(source) click to toggle source
# File lib/quantify/unit/compound_base_unit.rb, line 94
def initialize_copy(source)
  instance_variable_set("@unit", @unit.clone)
end
is_base_quantity_si_unit?() click to toggle source
# File lib/quantify/unit/compound_base_unit.rb, line 66
def is_base_quantity_si_unit?
  @unit.is_base_quantity_si_unit?
end
is_base_unit?() click to toggle source
# File lib/quantify/unit/compound_base_unit.rb, line 70
def is_base_unit?
  @unit.is_base_unit?
end
is_benchmark_unit?() click to toggle source
# File lib/quantify/unit/compound_base_unit.rb, line 74
def is_benchmark_unit?
  @unit.is_benchmark_unit?
end
is_denominator?() click to toggle source
# File lib/quantify/unit/compound_base_unit.rb, line 58
def is_denominator?
  @index < 0
end
is_derived_unit?() click to toggle source
# File lib/quantify/unit/compound_base_unit.rb, line 90
def is_derived_unit?
  @unit.is_derived_unit?
end
is_dimensionless?() click to toggle source

Only refers to the unit index, rather than the dimensions configuration of the actual unit

# File lib/quantify/unit/compound_base_unit.rb, line 29
def is_dimensionless?
  @index == 0
end
is_non_si_unit?() click to toggle source
# File lib/quantify/unit/compound_base_unit.rb, line 82
def is_non_si_unit?
  @unit.is_non_si_unit?
end
is_numerator?() click to toggle source
# File lib/quantify/unit/compound_base_unit.rb, line 54
def is_numerator?
  @index > 0
end
is_prefixed_unit?() click to toggle source
# File lib/quantify/unit/compound_base_unit.rb, line 86
def is_prefixed_unit?
  @unit.is_prefixed_unit?
end
is_si_unit?() click to toggle source
# File lib/quantify/unit/compound_base_unit.rb, line 78
def is_si_unit?
  @unit.is_si_unit?
end
label(reciprocal=false) click to toggle source
# File lib/quantify/unit/compound_base_unit.rb, line 46
def label(reciprocal=false)
  @unit.label + index_as_string(reciprocal)
end
measures() click to toggle source
# File lib/quantify/unit/compound_base_unit.rb, line 62
def measures
  dimensions.describe
end
name(reciprocal=false) click to toggle source

Absolute index as names always contain 'per' before denominator units

# File lib/quantify/unit/compound_base_unit.rb, line 34
def name(reciprocal=false)
  name_to_power(@unit.name, @index.abs)
end
pluralized_name() click to toggle source
# File lib/quantify/unit/compound_base_unit.rb, line 38
def pluralized_name
  name_to_power(@unit.pluralized_name, @index.abs)
end
symbol(reciprocal=false) click to toggle source
# File lib/quantify/unit/compound_base_unit.rb, line 42
def symbol(reciprocal=false)
  @unit.symbol + index_as_string(reciprocal)
end

Private Instance Methods

formatted_index(index=nil) click to toggle source

Returns a string representation of the unit index, formatted according to the global superscript configuration.

The index value can be overridden by specifying as an argument.

# File lib/quantify/unit/compound_base_unit.rb, line 105
def formatted_index(index=nil)
  index = "^#{index.nil? ? @index : index}"
  Unit.use_superscript_characters? ? index.with_superscript_characters : index
end
index_as_string(reciprocal=false) click to toggle source
# File lib/quantify/unit/compound_base_unit.rb, line 110
def index_as_string(reciprocal=false)
  if reciprocal == true
    @index == -1 ? "" : formatted_index(@index * -1)
  else
    @index.nil? || @index == 1 ? "" : formatted_index
  end
end
name_to_power(string,index) click to toggle source
# File lib/quantify/unit/compound_base_unit.rb, line 118
def name_to_power(string,index)
  name = string.clone
  case index
  when 1 then name
  when 2 then "square #{name}"
  when 3 then "cubic #{name}"
  else
    ordinal = ActiveSupport::Inflector.ordinalize(index)
    name << " to the #{ordinal} power"
  end
end