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.
51 52 53 54 |
# File 'lib/keybreak/controller.rb', line 51 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.
73 74 75 76 |
# File 'lib/keybreak/controller.rb', line 73 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.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/keybreak/controller.rb', line 33 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.
61 62 63 64 65 66 67 |
# File 'lib/keybreak/controller.rb', line 61 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
23 24 25 |
# File 'lib/keybreak/controller.rb', line 23 def on(event, &block) @handlers[event] = block end |