Class: Context::KeyMap

Inherits:
Object show all
Defined in:
lib/Context/KeyMap.rb

Overview

Represents a map of key strokes to actions

Instance Method Summary collapse

Constructor Details

#initializeKeyMap

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.

Returns:

  • (Boolean)


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