Class: Vedeu::Input::DSL Private

Inherits:
Object
  • Object
show all
Includes:
Common, DSL
Defined in:
lib/vedeu/input/dsl.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

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

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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:



67
68
69
70
71
72
73
74
75
76
# File 'lib/vedeu/input/dsl.rb', line 67

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=

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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)

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.



88
89
90
91
92
93
94
95
96
97
98
# File 'lib/vedeu/input/dsl.rb', line 88

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)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • keys (Array<void>)

Returns:

  • (Array<String|Symbol>)


105
106
107
108
109
# File 'lib/vedeu/input/dsl.rb', line 105

def valid_keys(keys)
  keys.compact.keep_if do |key|
    symbol?(key) || (string?(key) && present?(key))
  end
end