Class: Vedeu::DSL::Keymap

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

Overview

Provides methods to be used to define keypress mapped to actions.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Vedeu::DSL

#method_missing

Methods included from Common

#defined_value?, #to_sentence

Constructor Details

#initialize(model, client = nil) ⇒ Vedeu::DSL::Keymap

Returns an instance of DSL::Keymap.



64
65
66
67
# File 'lib/vedeu/dsl/components/keymap.rb', line 64

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

Instance Attribute Details

#clientObject (readonly, private)



130
131
132
# File 'lib/vedeu/dsl/components/keymap.rb', line 130

def client
  @client
end

#modelKeymap (readonly, private)



133
134
135
# File 'lib/vedeu/dsl/components/keymap.rb', line 133

def model
  @model
end

Class Method Details

.keymap(name, &block) ⇒ Keymap

TODO:

Try to remember why we need to pre-create the keymap in the repository.

Note:

When defining an interface, there is no need to provide a name since this can be discerned from the interface itself, e.g:

Vedeu.interface ‘my_interface’ do

keymap do
  # ...

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.

Examples:

Vedeu.keymap 'my_interface' do
  # ...

Vedeu.keys 'my_interface' do
  # ...

Vedeu.interface 'my_interface' do
  keymap do
    # ...

Vedeu.interface 'my_interface' do
  keys do
    # ...

Raises:

  • (InvalidSyntax)

    The required block was not given.



53
54
55
56
57
# File 'lib/vedeu/dsl/components/keymap.rb', line 53

def self.keymap(name, &block)
  Vedeu::Keymap.new({ name: name }).store

  Vedeu::Keymap.build({ name: name }, &block).store
end

Instance Method Details

#key(*value_or_values, &block) ⇒ Array

Define keypress(es) to perform an action.

Examples:

Vedeu.keymap do
  key('s')        { Vedeu.trigger(:save) }
  key('h', :left) { Vedeu.trigger(:left) }
  key('j', :down) { Vedeu.trigger(:down) }
  key('p') do
    ...
  end
  ...

Raises:

  • (InvalidSyntax)

    When the required block is not given, the value_or_values parameter is undefined, or when processing the collection, a member is undefined.



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/vedeu/dsl/components/keymap.rb', line 92

def key(*value_or_values, &block)
  fail InvalidSyntax, 'No action defined for `key`.' unless block_given?

  unless defined_value?(value_or_values)
    fail InvalidSyntax, 'No keypress(es) defined for `key`.'
  end

  value_or_values.each do |value|
    unless defined_value?(value)
      fail InvalidSyntax, 'An invalid value for `key` was encountered.'
    end

    model.add(model.member.new(value, &block))
  end
end

#name(value) ⇒ String

Note:

Using the name of a keymap that already exists will overwrite that keymap. Do not use the name ‘system’ as unexpected behaviour may occur.

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.



123
124
125
# File 'lib/vedeu/dsl/components/keymap.rb', line 123

def name(value)
  model.name = value
end