Class: NormalizeCountry::Scanner

Inherits:
Object
  • Object
show all
Defined in:
lib/normalize_country/scanner.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = nil) ⇒ Scanner

Returns a new instance of Scanner.



28
29
30
31
32
33
# File 'lib/normalize_country/scanner.rb', line 28

def initialize(options = nil)
  options ||= {}

  @to = options[:to] || NormalizeCountry.to
  @table = lookup_table(options[:from] || NormalizeCountry.formats)   # need aliases!!!
end

Instance Method Details

#convert(text) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/normalize_country/scanner.rb', line 35

def convert(text)
  s = Tokenizer.new(text)

  matches = []
  stack = []
  match_position = @table

  while !s.end?
    word = s.peek.downcase
    if !match_position[word]
      s.scan
      next
    end

    stack << s.scan
    alternatives = match_position[stack[-1].downcase]

    peek = s.peek
    if alternatives && peek && alternatives[peek.downcase]
      match_position = alternatives
      next
    end

    if match_position[stack[-1].downcase][:match]
      text = stack.join(" ")
      matches << { :matched => text, :converted => NormalizeCountry(text, :to => @to) }

    end

    stack.clear
    match_position = @table
  end

  matches
end