Class: Context::KeyAssignment

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

Overview

Represents the assignment of an action to a key press

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, &action) ⇒ KeyAssignment

Creates an assignment where action will be called when key is pressed



10
11
12
13
# File 'lib/Context/KeyAssignment.rb', line 10

def initialize(key, &action)
    @key = key
    @action = action
end

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action.



7
8
9
# File 'lib/Context/KeyAssignment.rb', line 7

def action
  @action
end

Instance Method Details

#handles?(key) ⇒ Boolean

Returns true if this KeyAssignment has an action assigned for key

Returns:

  • (Boolean)


16
17
18
# File 'lib/Context/KeyAssignment.rb', line 16

def handles?(key)
    @key.eql?(key)
end

#try(key) ⇒ Object

Run the action assigned to key if there is one. Returns true if an action was run, false otherwise



22
23
24
25
26
27
28
29
# File 'lib/Context/KeyAssignment.rb', line 22

def try(key)
    if handles?(key)
        @action.call
        true
    else
        false
    end
end