Module: Quantify::Unit::Prefix

Defined in:
lib/quantify/unit/prefix/prefix.rb,
lib/quantify/unit/prefix/si_prefix.rb,
lib/quantify/unit/prefix/base_prefix.rb,
lib/quantify/unit/prefix/non_si_prefix.rb

Defined Under Namespace

Classes: Base, NonSI, SI

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#prefixesObject

Returns the value of attribute prefixes.



6
7
8
# File 'lib/quantify/unit/prefix/prefix.rb', line 6

def prefixes
  @prefixes
end

Class Method Details

.for(name_or_symbol) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# 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) ⇒ Object



10
11
12
# 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) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# 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_prefixesObject

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



51
52
53
# File 'lib/quantify/unit/prefix/prefix.rb', line 51

def self.non_si_prefixes
  @prefixes.select {|prefix| prefix.is_non_si_prefix? }
end

.prefixesObject



21
22
23
# File 'lib/quantify/unit/prefix/prefix.rb', line 21

def self.prefixes
  @prefixes
end

.si_prefixesObject

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



44
45
46
# File 'lib/quantify/unit/prefix/prefix.rb', line 44

def self.si_prefixes
  @prefixes.select {|prefix| prefix.is_si_prefix? }
end

.unload(*unloaded_prefixes) ⇒ Object



14
15
16
17
18
19
# 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