class Alchemist::PrefixParser

Attributes

exponent[R]
unit_name[R]

Public Class Methods

new(unit) click to toggle source
# File lib/alchemist/prefix_parser.rb, line 5
def initialize(unit)
  @unit = unit
  matches = unit.to_s.match(prefix_matcher)
  prefix, parsed_unit = matches.captures

  if prefix && library.si_units.include?(parsed_unit)
    @exponent = library.exponent_for(parsed_unit, prefix.to_sym)
    @unit_name = parsed_unit.to_sym
  else
    @exponent = 1
    @unit_name = unit
  end
end

Public Instance Methods

guess_type(from) click to toggle source
# File lib/alchemist/prefix_parser.rb, line 27
def guess_type(from)
  shared_types(from).first
end
prefix_matcher() click to toggle source
# File lib/alchemist/prefix_parser.rb, line 19
def prefix_matcher
  @prefix_matcher ||= generate_prefix_matcher
end
shares_type?(from) click to toggle source
# File lib/alchemist/prefix_parser.rb, line 23
def shares_type?(from)
  guess_type(from)
end

Private Instance Methods

generate_prefix_matcher() click to toggle source
# File lib/alchemist/prefix_parser.rb, line 42
def generate_prefix_matcher
  prefix_keys = library.unit_prefixes.keys.map(&:to_s).sort{ |a,b| b.length <=> a.length }
  %r{^(#{prefix_keys.join('|')})?(.+)}
end
library() click to toggle source
# File lib/alchemist/prefix_parser.rb, line 38
def library
  Alchemist.library
end
shared_types(from) click to toggle source
# File lib/alchemist/prefix_parser.rb, line 33
def shared_types from
  library.measurement_for(from.unprefixed_unit_name) & library.measurement_for(unit_name)
end