Class: Vedeu::Keymap
- Inherits:
-
Object
- Object
- Vedeu::Keymap
- Includes:
- Model
- Defined in:
- lib/vedeu/input/keymap.rb
Overview
A container class for keys associated with a particular interface.
Instance Attribute Summary collapse
Attributes included from Model
Instance Method Summary collapse
-
#add(key) ⇒ void
Add a key to the keymap.
- #defaults ⇒ Hash private
-
#initialize(attributes = {}) ⇒ Vedeu::Keymap
constructor
Returns a new instance of Vedeu::Keymap.
-
#key_defined?(input) ⇒ Boolean
Check whether the key is already defined for this keymap.
-
#keys ⇒ Vedeu::Keys
Returns the collection of keys defined for this keymap.
-
#use(input) ⇒ Array|FalseClass
Triggers the action associated with the key, if it is registered with this keymap.
-
#valid?(key) ⇒ Boolean
private
Checks that the provided key is not already registered with this keymap.
Methods included from Model
#demodulize, #deputy, #dsl_class, included, #store
Constructor Details
#initialize(attributes = {}) ⇒ Vedeu::Keymap
Returns a new instance of Vedeu::Keymap.
24 25 26 27 28 29 30 |
# File 'lib/vedeu/input/keymap.rb', line 24 def initialize(attributes = {}) @attributes = defaults.merge!(attributes) @attributes.each do |key, value| instance_variable_set("@#{key}", value) end end |
Instance Attribute Details
#name ⇒ String
15 16 17 |
# File 'lib/vedeu/input/keymap.rb', line 15 def name @name end |
Instance Method Details
#add(key) ⇒ void
This method returns an undefined value.
Add a key to the keymap.
36 37 38 39 40 |
# File 'lib/vedeu/input/keymap.rb', line 36 def add(key) return false unless valid?(key) @keys = keys.add(key) end |
#defaults ⇒ Hash (private)
76 77 78 79 80 81 82 |
# File 'lib/vedeu/input/keymap.rb', line 76 def defaults { name: '', keys: [], repository: Vedeu::Keymaps.keymaps, } end |
#key_defined?(input) ⇒ Boolean
Check whether the key is already defined for this keymap.
54 55 56 |
# File 'lib/vedeu/input/keymap.rb', line 54 def key_defined?(input) keys.any? { |key| key.input == input } end |
#keys ⇒ Vedeu::Keys
Returns the collection of keys defined for this keymap.
45 46 47 |
# File 'lib/vedeu/input/keymap.rb', line 45 def keys collection.coerce(@keys, self) end |
#use(input) ⇒ Array|FalseClass
Triggers the action associated with the key, if it is registered with this keymap.
63 64 65 66 67 68 69 70 71 |
# File 'lib/vedeu/input/keymap.rb', line 63 def use(input) return false unless key_defined?(input) Vedeu.log(type: :input, message: "Key pressed: '#{input}'") Vedeu.trigger(:key, input) keys.select { |key| key.input == input }.map(&:press) end |
#valid?(key) ⇒ Boolean (private)
Checks that the provided key is not already registered with this keymap.
88 89 90 91 92 93 94 95 |
# File 'lib/vedeu/input/keymap.rb', line 88 def valid?(key) return true unless key_defined?(key.input) Vedeu.log(type: :debug, message: "Keymap '#{name}' already defines '#{key.input}'.") false end |