Class: State

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

Overview

© 2008 Los Angeles Times

Constant Summary collapse

@@id_sequence =
1

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(char, depth = 0, next_state = {}) ⇒ State

Returns a new instance of State.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/state.rb', line 17

def initialize(char, depth=0, next_state={})
  if next_state.is_a?(Array)
    class << next_state
      def values
        self.select{|value| value}
      end

      def keys
        retval = []
        self.each_index{|i| retval << i if self[i]}

        retval
      end
    end
  end

  @char = char
  @id = @@id_sequence
  @next_state = next_state
  @@id_sequence += 1
  self.depth = depth
end

Instance Attribute Details

#charObject (readonly)

Returns the value of attribute char.



9
10
11
# File 'lib/state.rb', line 9

def char
  @char
end

#depthObject

Returns the value of attribute depth.



14
15
16
# File 'lib/state.rb', line 14

def depth
  @depth
end

#failObject

Returns the value of attribute fail.



11
12
13
# File 'lib/state.rb', line 11

def fail
  @fail
end

#fail_incrementObject

Returns the value of attribute fail_increment.



12
13
14
# File 'lib/state.rb', line 12

def fail_increment
  @fail_increment
end

#idObject (readonly)

Returns the value of attribute id.



8
9
10
# File 'lib/state.rb', line 8

def id
  @id
end

#keywordObject

Returns the value of attribute keyword.



15
16
17
# File 'lib/state.rb', line 15

def keyword
  @keyword
end

#outputObject

Returns the value of attribute output.



13
14
15
# File 'lib/state.rb', line 13

def output
  @output
end

Instance Method Details

#[](char) ⇒ Object



50
51
52
# File 'lib/state.rb', line 50

def [](char)
  @next_state[char]
end

#[]=(char, value) ⇒ Object



54
55
56
# File 'lib/state.rb', line 54

def []=(char, value)
  @next_state[char] = value
end

#insert_next_state(char) ⇒ Object



40
41
42
43
44
45
46
47
48
# File 'lib/state.rb', line 40

def insert_next_state(char)
  cur_state = @next_state[char]

  if cur_state
    cur_state
  else
    @next_state[char] = State.new(char, depth + 1)
  end
end

#keysObject



62
63
64
# File 'lib/state.rb', line 62

def keys
  @next_state.keys
end

#transition(char, position = nil) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/state.rb', line 66

def transition(char, position=nil)
  next_state = @next_state[char]

  if next_state == self
    if position
      position.end = position.begin += 1
    end
  elsif next_state
    position.end += 1 if position
  else
    next_state = fail.transition(char)
    if position
      position.begin += depth - (next_state.depth - 1)
      position.end += 1
    end
  end

  next_state
end

#valuesObject



58
59
60
# File 'lib/state.rb', line 58

def values
  @next_state.values
end

#walk(array, &block) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
# File 'lib/state.rb', line 86

def walk(array, &block)
  block.call(array.join(''), output) if output

  self.keys.each do |key|
    array << key.chr

    self[key].walk(array, &block)

    array.pop
  end
end