Class: Alchemist::PrefixParser

Inherits:
Object
  • Object
show all
Defined in:
lib/alchemist/prefix_parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(unit) ⇒ PrefixParser

Returns a new instance of PrefixParser.



5
6
7
8
9
10
11
12
13
14
15
16
17
# 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

Instance Attribute Details

#exponentObject (readonly)

Returns the value of attribute exponent.



3
4
5
# File 'lib/alchemist/prefix_parser.rb', line 3

def exponent
  @exponent
end

#unit_nameObject (readonly)

Returns the value of attribute unit_name.



3
4
5
# File 'lib/alchemist/prefix_parser.rb', line 3

def unit_name
  @unit_name
end

Instance Method Details

#guess_type(from) ⇒ Object



27
28
29
# File 'lib/alchemist/prefix_parser.rb', line 27

def guess_type(from)
  shared_types(from).first
end

#prefix_matcherObject



19
20
21
# File 'lib/alchemist/prefix_parser.rb', line 19

def prefix_matcher
  @prefix_matcher ||= generate_prefix_matcher
end

#shares_type?(from) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/alchemist/prefix_parser.rb', line 23

def shares_type?(from)
  guess_type(from)
end