Class: Context::KeyAssignment
Overview
Represents the assignment of an action to a key press
Instance Attribute Summary collapse
-
#action ⇒ Object
readonly
Returns the value of attribute action.
Instance Method Summary collapse
-
#handles?(key) ⇒ Boolean
Returns true if this KeyAssignment has an action assigned for key.
-
#initialize(key, &action) ⇒ KeyAssignment
constructor
Creates an assignment where action will be called when key is pressed.
-
#try(key) ⇒ Object
Run the action assigned to key if there is one.
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
#action ⇒ Object (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
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 |