Class: Vedeu::Input::DSL
- Inherits:
-
Object
- Object
- Vedeu::Input::DSL
- Defined in:
- lib/vedeu/input/dsl.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 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
-
#key(*keys, &block) ⇒ Array
(also: #key=)
Define keypress(es) to perform an action.
- #valid_keys(keys) ⇒ Array<String|Symbol> private
Methods included from DSL
#attributes, #initialize, #method_missing, #name
Methods included from Common
#absent?, #array?, #boolean, #boolean?, #empty_value?, #escape?, #falsy?, #hash?, #line_model?, #numeric?, #positionable?, #present?, #snake_case, #stream_model?, #string?, #symbol?, #truthy?, #view_model?
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
If a keymap with this name does not already exist, pre-register it, otherwise we have no way of knowing if a key defined in the DSL for this keymap has already been registered. This protects the client application from attempting to define the same key more than once for the same keymap.
This is also used when defining the ‘global’ keymap.
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 :my_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 :my_interface do
# ... some code
end
# or...
Vedeu.interface :my_interface do
keymap do
# ... some code
end # or...
keys do
# ... some code
end
end
65 66 67 68 69 70 71 72 73 74 |
# File 'lib/vedeu/input/dsl.rb', line 65 def self.keymap(name, &block) raise Vedeu::Error::MissingRequired unless name raise Vedeu::Error::RequiresBlock unless block_given? unless Vedeu.keymaps.registered?(name) Vedeu::Input::Keymap.new(name: name).store end Vedeu::Input::Keymap.build(name: name, &block).store end |
Instance Method Details
#key(*keys, &block) ⇒ Array Also known as: key=
Define keypress(es) to perform an action.
87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/vedeu/input/dsl.rb', line 87 def key(*keys, &block) raise Vedeu::Error::InvalidSyntax, 'No action defined for `key`.' unless block_given? raise Vedeu::Error::InvalidSyntax, 'No keypresses defined for `key`.' unless present?(keys) valid_keys(keys).each do |key| model.add(Vedeu::Input::Key.new(key, &block)) end end |
#valid_keys(keys) ⇒ Array<String|Symbol> (private)
104 105 106 107 108 |
# File 'lib/vedeu/input/dsl.rb', line 104 def valid_keys(keys) keys.compact.keep_if do |key| symbol?(key) || (string?(key) && present?(key)) end end |