Class: Vedeu::Input::Keymap
- Inherits:
-
Object
- Object
- Vedeu::Input::Keymap
- Includes:
- Repositories::Model
- Defined in:
- lib/vedeu/input/keymap.rb
Overview
A container class for keys associated with a particular interface.
Instance Attribute Summary collapse
Instance Method Summary collapse
-
#absent?(variable) ⇒ Boolean
included
from Common
Returns a boolean indicating whether a variable is nil or empty.
-
#add(key) ⇒ void
Add a key to the keymap.
-
#defaults ⇒ Hash
private
Returns the default options/attributes for this class.
-
#demodulize(klass) ⇒ String
included
from Common
Removes the module part from the expression in the string.
-
#deputy(client = nil) ⇒ void
included
from Repositories::Model
Returns a DSL instance responsible for defining the DSL methods of this model.
-
#dsl_class ⇒ String
included
from Repositories::Model
private
Returns the DSL class name responsible for this model.
-
#initialize(attributes = {}) ⇒ Vedeu::Input::Keymap
constructor
Returns a new instance of Vedeu::Input::Keymap.
-
#key_defined?(input) ⇒ Boolean
Check whether the key is already defined for this keymap.
-
#keys ⇒ Vedeu::Input::Keys
Returns the collection of keys defined for this keymap.
-
#present?(variable) ⇒ Boolean
included
from Common
Returns a boolean indicating whether a variable has a useful value.
-
#snake_case(name) ⇒ String
included
from Common
Converts a class name to a lowercase snake case string.
-
#store ⇒ void
included
from Repositories::Model
The model instance stored in the repository.
-
#use(input) ⇒ Array|FalseClass
When the given input is registered with this keymap, this method triggers the action associated with the key.
-
#valid?(key) ⇒ Boolean
private
Checks that the provided key is not already registered with this keymap.
Constructor Details
#initialize(attributes = {}) ⇒ Vedeu::Input::Keymap
Returns a new instance of Vedeu::Input::Keymap.
29 30 31 32 33 |
# File 'lib/vedeu/input/keymap.rb', line 29 def initialize(attributes = {}) defaults.merge!(attributes).each do |key, value| instance_variable_set("@#{key}", value) end end |
Instance Attribute Details
#name ⇒ String
17 18 19 |
# File 'lib/vedeu/input/keymap.rb', line 17 def name @name end |
#repository ⇒ Vedeu::Repositories::Repository Originally defined in module Repositories::Model
Instance Method Details
#absent?(variable) ⇒ Boolean Originally defined in module Common
Returns a boolean indicating whether a variable is nil or empty.
#add(key) ⇒ void
This method returns an undefined value.
Add a key to the keymap.
39 40 41 42 43 |
# File 'lib/vedeu/input/keymap.rb', line 39 def add(key) return false unless valid?(key) @keys = keys.add(key) end |
#defaults ⇒ Hash (private)
Returns the default options/attributes for this class.
81 82 83 84 85 86 87 |
# File 'lib/vedeu/input/keymap.rb', line 81 def defaults { name: '', keys: [], repository: Vedeu::Input::Keymaps.keymaps, } end |
#demodulize(klass) ⇒ String Originally defined in module Common
Removes the module part from the expression in the string.
#deputy(client = nil) ⇒ void Originally defined in module Repositories::Model
This method returns an undefined value.
Returns a DSL instance responsible for defining the DSL methods of this model.
#dsl_class ⇒ String (private) Originally defined in module Repositories::Model
Returns the DSL class name responsible for this model.
#key_defined?(input) ⇒ Boolean
Check whether the key is already defined for this keymap.
57 58 59 |
# File 'lib/vedeu/input/keymap.rb', line 57 def key_defined?(input) keys.any? { |key| key.input == input } end |
#keys ⇒ Vedeu::Input::Keys
Returns the collection of keys defined for this keymap.
48 49 50 |
# File 'lib/vedeu/input/keymap.rb', line 48 def keys collection.coerce(@keys, self) end |
#present?(variable) ⇒ Boolean Originally defined in module Common
Returns a boolean indicating whether a variable has a useful value.
#snake_case(name) ⇒ String Originally defined in module Common
Converts a class name to a lowercase snake case string.
#store ⇒ void Originally defined in module Repositories::Model
Perhaps some validation could be added here?
If a block is given, store the model, return the model after yielding.
This method returns an undefined value.
Returns The model instance stored in the repository.
#use(input) ⇒ Array|FalseClass
When the given input is registered with this keymap, this method triggers the action associated with the key.
66 67 68 69 70 71 72 73 74 |
# File 'lib/vedeu/input/keymap.rb', line 66 def use(input) return false unless key_defined?(input) Vedeu.log(type: :input, message: "Key pressed: '#{input}'".freeze) 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.
94 95 96 97 98 99 100 101 102 |
# File 'lib/vedeu/input/keymap.rb', line 94 def valid?(key) return true unless key_defined?(key.input) Vedeu.log(type: :input, message: "Keymap '#{name}' already " \ "defines '#{key.input}'.".freeze) false end |