Class: Context::KeyMap
Overview
Represents a map of key strokes to actions
Instance Method Summary collapse
-
#add(assignment) ⇒ Object
Add a KeyAssignment to the map.
-
#findKey(key) ⇒ Object
Returns the assignment that handles this key or nil if it doesn’t exist.
-
#handles?(key) ⇒ Boolean
Returns true if this map can handle the key.
-
#initialize ⇒ KeyMap
constructor
A new instance of KeyMap.
-
#press(key) ⇒ Object
Press the key and run the associated action, if it exits.
Constructor Details
#initialize ⇒ KeyMap
Returns a new instance of KeyMap.
9 10 11 |
# File 'lib/Context/KeyMap.rb', line 9 def initialize @list = [] end |
Instance Method Details
#add(assignment) ⇒ Object
Add a KeyAssignment to the map
14 15 16 |
# File 'lib/Context/KeyMap.rb', line 14 def add(assignment) @list.push(assignment) end |
#findKey(key) ⇒ Object
Returns the assignment that handles this key or nil if it doesn’t exist
20 21 22 23 24 |
# File 'lib/Context/KeyMap.rb', line 20 def findKey(key) @list.find do |a| a.handles?(key) end end |
#handles?(key) ⇒ Boolean
Returns true if this map can handle the key. Otherwise false.
27 28 29 |
# File 'lib/Context/KeyMap.rb', line 27 def handles?(key) !findKey(key).nil? end |
#press(key) ⇒ Object
Press the key and run the associated action, if it exits. Returns true if an action was run, false otherwise.
33 34 35 36 37 38 39 40 |
# File 'lib/Context/KeyMap.rb', line 33 def press(key) assignment = findKey(key) if !assignment.nil? assignment.try(key) else false end end |