Class: Vedeu::DSL::Keymap
- Inherits:
-
Object
- Object
- Vedeu::DSL::Keymap
- Includes:
- Common, Vedeu::DSL
- Defined in:
- lib/vedeu/dsl/keymap.rb
Overview
You can define keymaps by name which matches a defined interface. When that interface is in focus, keys pressed as part of this definition will affect that interface. This allows you to form context driven behaviour for your application.
Instance Attribute Summary
Attributes included from Vedeu::DSL
Class Method Summary collapse
-
.keymap(name, &block) ⇒ Vedeu::Input::Keymap
Define actions for keypresses for when specific interfaces are in focus.
Instance Method Summary collapse
-
#initialize(model, client = nil) ⇒ Vedeu::DSL::Keymap
constructor
Returns an instance of DSL::Keymap.
-
#key(*value_or_values, &block) ⇒ Array
(also: #key=)
Define keypress(es) to perform an action.
-
#name(value) ⇒ String
(also: #name=)
Define the name of the keymap.
Methods included from Vedeu::DSL
Methods included from Common
#demodulize, #present?, #snake_case
Constructor Details
#initialize(model, client = nil) ⇒ Vedeu::DSL::Keymap
Returns an instance of DSL::Keymap.
65 66 67 68 |
# File 'lib/vedeu/dsl/keymap.rb', line 65 def initialize(model, client = nil) @model = model @client = client end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Vedeu::DSL
Class Method Details
.keymap(name, &block) ⇒ Vedeu::Input::Keymap
Try to remember why we need to pre-create the keymap in the repository.
Define actions for keypresses for when specific interfaces are in focus. Unless an interface is specified, the key will be assumed to be global, meaning its action will happen regardless of the interface in focus.
Vedeu.keymap 'some_interface' do
key('s') { Vedeu.trigger(:save) }
key('h', :left) { Vedeu.trigger(:left) }
key('j', :down) { Vedeu.trigger(:down) }
key('p') do
# ... some code
end
# ... some code
end
# or...
Vedeu.keys 'some_interface' do
# ... some code
end
# or...
Vedeu.interface 'some_interface' do
keymap do
# ... some code
end # or...
keys do
# ... some code
end
end
54 55 56 57 58 |
# File 'lib/vedeu/dsl/keymap.rb', line 54 def self.keymap(name, &block) Vedeu::Input::Keymap.new(name: name).store Vedeu::Input::Keymap.build(name: name, &block).store end |
Instance Method Details
#key(*value_or_values, &block) ⇒ Array Also known as: key=
Define keypress(es) to perform an action.
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/vedeu/dsl/keymap.rb', line 82 def key(*value_or_values, &block) fail Vedeu::Error::InvalidSyntax, 'No action defined for `key`.' unless block_given? unless present?(value_or_values) fail Vedeu::Error::InvalidSyntax, 'No keypress(es) defined for `key`.' end value_or_values.each do |value| unless present?(value) fail Vedeu::Error::InvalidSyntax, 'An invalid value for `key` was encountered.' end model.add(model.member.new(value, &block)) end end |
#name(value) ⇒ String Also known as: name=
Define the name of the keymap.
To only allow certain keys to work with specific interfaces, use the same name as the interface.
When the name ‘global’ is used, all keys in the keymap block will be available to all interfaces. Once a key has been defined in the ‘global’ keymap, it cannot be used for a specific interface.
Vedeu.keymap do
name 'some_interface'
end
116 117 118 |
# File 'lib/vedeu/dsl/keymap.rb', line 116 def name(value) model.name = value end |