Class: Cora::Plugin
- Inherits:
-
Object
- Object
- Cora::Plugin
- 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
-
#current_state ⇒ Object
readonly
Returns the value of attribute current_state.
-
#manager ⇒ Object
Returns the value of attribute manager.
-
#match_data ⇒ Object
Returns the value of attribute match_data.
Class Method Summary collapse
Instance Method Summary collapse
- #ask(question, options = {}) ⇒ Object
- #confirm(question, options = {unmatched_message: "I'm sorry, I didn't understand that."}, &block) ⇒ Object
- #listeners ⇒ Object
- #process(text) ⇒ Object
- #say(text, options = {}) ⇒ Object
- #set_state(state) ⇒ Object
Instance Attribute Details
#current_state ⇒ Object (readonly)
Returns the value of attribute current_state.
15 16 17 |
# File 'lib/cora/plugin.rb', line 15 def current_state @current_state end |
#manager ⇒ Object
Returns the value of attribute manager.
14 15 16 |
# File 'lib/cora/plugin.rb', line 14 def manager @manager end |
#match_data ⇒ Object
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, = {}, &block) listeners[regex] = { block: block, within_state: ([[:within_state]].flatten) } end |
.listeners ⇒ Object
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, ={}) log "Ask: #{question}" f = Fiber.current [:prompt_for_response] = true manager.respond(question, ) 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, = {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 [:unmatched_message] end end end |
#listeners ⇒ Object
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, ={}) log "Say: #{text}" manager.respond(text, ) 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 |