module Quantify::Unit::Prefix

Attributes

prefixes[RW]

Public Class Methods

for(name_or_symbol) click to toggle source
# File lib/quantify/unit/prefix/prefix.rb, line 25
def self.for(name_or_symbol)
  return name_or_symbol.clone if name_or_symbol.is_a? Quantify::Unit::Prefix::Base
  if name_or_symbol.is_a?(String) || name_or_symbol.is_a?(Symbol)
    if prefix = @prefixes.find do |prefix|
       prefix.name == name_or_symbol.remove_underscores.downcase ||
       prefix.symbol == name_or_symbol.remove_underscores
      end
      return prefix.clone
    else
      return nil
    end
  else
    raise Exceptions::InvalidArgumentError, "Argument must be a Symbol or String"
  end
end
load(prefix) click to toggle source
# File lib/quantify/unit/prefix/prefix.rb, line 10
def self.load(prefix)
  @prefixes << prefix if prefix.is_a? Quantify::Unit::Prefix::Base
end
method_missing(method, *args, &block) click to toggle source
Calls superclass method
# File lib/quantify/unit/prefix/prefix.rb, line 55
def self.method_missing(method, *args, &block)
  if method.to_s =~ /((si|non_si)_)?prefixes(_by_(name|symbol|label))?/
    if $2
      prefixes = Prefix.prefixes.select { |prefix| instance_eval("prefix.is_#{$2}_prefix?") }
    else
      prefixes = Prefix.prefixes
    end
    return_format = ( $4 ? $4.to_sym : nil )
    prefixes.map(&return_format).to_a
  elsif prefix = self.for(method)
    return prefix
  else
    super
  end
end
non_si_prefixes() click to toggle source

This can be replicated by method missing approach, but explicit method provided given importance in Unit match (and for) methods regexen

# File lib/quantify/unit/prefix/prefix.rb, line 51
def self.non_si_prefixes
  @prefixes.select {|prefix| prefix.is_non_si_prefix? }
end
prefixes() click to toggle source
# File lib/quantify/unit/prefix/prefix.rb, line 21
def self.prefixes
  @prefixes
end
si_prefixes() click to toggle source

This can be replicated by method missing approach, but explicit method provided given importance in Unit match (and for) methods regexen

# File lib/quantify/unit/prefix/prefix.rb, line 44
def self.si_prefixes
  @prefixes.select {|prefix| prefix.is_si_prefix? }
end
unload(*unloaded_prefixes) click to toggle source
# File lib/quantify/unit/prefix/prefix.rb, line 14
def self.unload(*unloaded_prefixes)
  [unloaded_prefixes].flatten.each do |unloaded_prefix|
    unloaded_prefix = Prefix.for(unloaded_prefix)
    @prefixes.delete_if { |prefix| prefix.label == unloaded_prefix.label }
  end
end