class Keybreak::Controller
Controller
of key break processing
Public Class Methods
Generates an instance.
# File lib/keybreak/controller.rb, line 9 def initialize() clear @handlers = {} @handlers[:keystart] = DO_NOTHING @handlers[:keyend] = DO_NOTHING @handlers[:detection] = KEY_CHANGED end
Public Instance Methods
Clears internal data to the status before key feed starts.
# File lib/keybreak/controller.rb, line 53 def clear() @is_fed = false @values = [] end
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.
# File lib/keybreak/controller.rb, line 75 def execute(&block) instance_eval(&block) self.flush end
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.
# 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
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.
# File lib/keybreak/controller.rb, line 63 def flush() if @is_fed @handlers[:keyend].call(@key, *@values) end clear end
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.
# File lib/keybreak/controller.rb, line 24 def on(event, &block) @handlers[event] = block return self end