Class: Vedeu::Input::DSL

Inherits:
Object
  • Object
show all
Includes:
Common, 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

#client, #model

Class Method Summary collapse

Instance Method Summary collapse

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

Note:

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

Parameters:

  • name (NilClass|Symbol|String)

    The name of the model or target model to act upon. May default to ‘Vedeu.focus`.

  • block (Proc)

Returns:

Raises:



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.

Parameters:

  • keys (Array<String>|Array<Symbol>|String|Symbol)

    The key(s) pressed. Special keys can be found in Input#specials. When more than one key is defined, then the extras are treated as aliases.

  • block (Proc)

    The action to perform when this key is pressed. Can be a method call or event triggered.

Returns:

  • (Array)

    A collection containing the keys minus any invalid or nil keys.

Raises:

  • (Vedeu::Error::InvalidSyntax)

    When the value given for an argument or parameter cannot be used because it is not valid for the use case, unsupported or the method expects a different type.



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)

Parameters:

  • keys (Array<void>)

Returns:

  • (Array<String|Symbol>)


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