Class: Keybreak::Controller
- Inherits:
-
Object
- Object
- Keybreak::Controller
- Defined in:
- lib/keybreak/controller.rb
Overview
Controller of key break processing
Instance Method Summary collapse
-
#clear ⇒ Object
Clears internal data to the status before key feed starts.
-
#execute(&block) ⇒ Object
Executes the given block and calls flush() finally.
-
#feed(key, *values) ⇒ Object
Detects a key break and calls the registered handlers.
-
#flush ⇒ Object
Calls the :keyend handler once with the last fed key and value, then clears internal data to the status before key feed starts.
-
#initialize ⇒ Controller
constructor
Generates an instance.
-
#on(event, &block) ⇒ Object
Registers the given block as a key break event handler.
Constructor Details
#initialize ⇒ Controller
Generates an instance.
9 10 11 12 13 14 15 |
# File 'lib/keybreak/controller.rb', line 9 def initialize() clear @handlers = {} @handlers[:keystart] = DO_NOTHING @handlers[:keyend] = DO_NOTHING @handlers[:detection] = KEY_CHANGED end |
Instance Method Details
#clear ⇒ Object
Clears internal data to the status before key feed starts.
53 54 55 56 |
# File 'lib/keybreak/controller.rb', line 53 def clear() @is_fed = false @values = [] end |
#execute(&block) ⇒ Object
Executes the given block and calls flush() finally. Place feed() within the block so that the key break handlers are called for all keys including the last key.
75 76 77 78 |
# File 'lib/keybreak/controller.rb', line 75 def execute(&block) instance_eval(&block) self.flush end |
#feed(key, *values) ⇒ Object
Detects a key break and calls the registered handlers. When a new key comes, calls the :keyend handler with the last key and value, then call the :keystart handler with the new key and value. For the first key feed, does not call the :keyend handler. Use flush() to call the :keyend handler for the last fed key.
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/keybreak/controller.rb', line 35 def feed(key, *values) if @is_fed if @handlers[:detection].call(key, @key) @handlers[:keyend].call(@key, *@values) @key = key @handlers[:keystart].call(key, *values) end else @is_fed = true @key = key @handlers[:keystart].call(key, *values) end @values = values end |
#flush ⇒ Object
Calls the :keyend handler once with the last fed key and value, then clears internal data to the status before key feed starts. Place this method after the last feed() to complete key break process. Does nothing when no key has been fed.
63 64 65 66 67 68 69 |
# File 'lib/keybreak/controller.rb', line 63 def flush() if @is_fed @handlers[:keyend].call(@key, *@values) end clear end |
#on(event, &block) ⇒ Object
Registers the given block as a key break event handler. Valid events are:
:keystart
:keyend
:detection
Returns self instance so that registrations can be chained.
24 25 26 27 |
# File 'lib/keybreak/controller.rb', line 24 def on(event, &block) @handlers[event] = block return self end |