Class: Cora::Plugin

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/cora/plugin.rb

Constant Summary collapse

CONFIRM_REGEX =

These could use some more work

/yes|yeah|yep|ok|confirm|affirmative|indeed|engage/i
DENY_REGEX =
/no|nope|nah|cancel|negative/i

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#current_stateObject (readonly)

Returns the value of attribute current_state.



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

def current_state
  @current_state
end

#managerObject

Returns the value of attribute manager.



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

def manager
  @manager
end

#match_dataObject

Returns the value of attribute match_data.



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

def match_data
  @match_data
end

Class Method Details

.listen_for(regex, options = {}, &block) ⇒ Object



19
20
21
22
23
24
# File 'lib/cora/plugin.rb', line 19

def listen_for(regex, options = {}, &block)
  listeners[regex] = {
    block: block,
    within_state: ([options[:within_state]].flatten)
  }
end

.listenersObject



26
27
28
# File 'lib/cora/plugin.rb', line 26

def listeners
  @listeners ||= {}
end

Instance Method Details

#ask(question, options = {}) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/cora/plugin.rb', line 67

def ask(question, options={})
  log "Ask: #{question}"

  f = Fiber.current
  options[:prompt_for_response] = true
  manager.respond(question, options)
  manager.set_callback do |text|
    f.resume(text)
  end

  Fiber.yield
end

#confirm(question, options = {unmatched_message: "I'm sorry, I didn't understand that."}, &block) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/cora/plugin.rb', line 80

def confirm(question, options = {unmatched_message: "I'm sorry, I didn't understand that."}, &block)
  while (response = ask(question))
    if response.match(CONFIRM_REGEX)
      return true
    elsif response.match(DENY_REGEX)
      return false
    else
      say options[:unmatched_message]
    end
  end
end

#listenersObject



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

def listeners
  self.class.listeners
end

#process(text) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/cora/plugin.rb', line 32

def process(text)
  listeners.each do |regex, entry|
    if match = text.match(regex)
      captures = match.captures
      log "Matches #{regex}"

      if entry[:within_state]
        log "Applicable states: #{entry[:within_state].join(', ')}"
        log "Current state: #{current_state}"

        if entry[:within_state].include?(current_state)
          log "Matches, executing block"

          self.match_data = match
          Fiber.new {
            instance_exec(*captures, &entry[:block])
          }.resume
          return true
        end
      end
    end

  end
  false
end

#say(text, options = {}) ⇒ Object



62
63
64
65
# File 'lib/cora/plugin.rb', line 62

def say(text, options={})
  log "Say: #{text}"
  manager.respond(text, options)
end

#set_state(state) ⇒ Object



92
93
94
95
# File 'lib/cora/plugin.rb', line 92

def set_state(state)
  @current_state = state
  manager.set_priority_plugin(self)
end