Class: OCRunner::ParseMachine
- Inherits:
-
Object
- Object
- OCRunner::ParseMachine
- Includes:
- Oniguruma
- Defined in:
- lib/ocrunner/parse_machine.rb
Direct Known Subclasses
Class Attribute Summary collapse
-
.events ⇒ Object
Returns the value of attribute events.
-
.states ⇒ Object
Returns the value of attribute states.
Class Method Summary collapse
- .default_state(default_state) ⇒ Object
- .event(name, options = {}, &block) ⇒ Object
- .match(regex) ⇒ Object
- .state(name, transitions = {}) ⇒ Object
Instance Method Summary collapse
- #default_state ⇒ Object
- #event(name) ⇒ Object
- #initialize_state ⇒ Object
- #process_input(line) ⇒ Object
Class Attribute Details
.events ⇒ Object
Returns the value of attribute events.
9 10 11 |
# File 'lib/ocrunner/parse_machine.rb', line 9 def events @events end |
.states ⇒ Object
Returns the value of attribute states.
9 10 11 |
# File 'lib/ocrunner/parse_machine.rb', line 9 def states @states end |
Class Method Details
.default_state(default_state) ⇒ Object
29 30 31 32 33 |
# File 'lib/ocrunner/parse_machine.rb', line 29 def default_state(default_state) @states ||= {} @states[default_state] ||= {} @states[default_state][:default] = true end |
.event(name, options = {}, &block) ⇒ Object
15 16 17 18 19 20 21 22 23 |
# File 'lib/ocrunner/parse_machine.rb', line 15 def event(name, ={}, &block) @events ||= [] @events << @next_event.merge( :name => name, :options => , :callback => block ) @next_event = nil end |
.match(regex) ⇒ Object
10 11 12 13 14 |
# File 'lib/ocrunner/parse_machine.rb', line 10 def match(regex) @next_event ||= {} @next_event[:regexes] ||= [] @next_event[:regexes] << regex end |
.state(name, transitions = {}) ⇒ Object
24 25 26 27 28 |
# File 'lib/ocrunner/parse_machine.rb', line 24 def state(name, transitions={}) @states ||= {} @states[name] ||= {} @states[name][:transitions] = transitions end |
Instance Method Details
#default_state ⇒ Object
41 42 43 44 45 |
# File 'lib/ocrunner/parse_machine.rb', line 41 def default_state self.class.states.each_pair do |state_name, state_definition| return state_name if state_definition[:default] == true end end |
#event(name) ⇒ Object
47 48 49 50 51 |
# File 'lib/ocrunner/parse_machine.rb', line 47 def event(name) self.class.events.find do |event| event[:name] == name end end |
#initialize_state ⇒ Object
36 37 38 39 |
# File 'lib/ocrunner/parse_machine.rb', line 36 def initialize_state @state = default_state raise "Default state not defined" if @state.nil? end |
#process_input(line) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/ocrunner/parse_machine.rb', line 53 def process_input(line) self.class.events.each do |event| if self.class.states[@state][:transitions].has_key?(event[:name]) event[:regexes].each do |regex| if regex.is_a?(String) regex = ORegexp.new(regex) end if (match = regex.match(line)) args = [line] + match[1..-1] self.instance_exec(*args, &event[:callback]) if event[:callback] @state = self.class.states[@state][:transitions][event[:name]] return end end end end end |