Class: Vedeu::Input::Keymap

Inherits:
Object
  • Object
show all
Includes:
Model
Defined in:
lib/vedeu/input/keymap.rb

Overview

A container class for keys associated with a particular interface.

Instance Attribute Summary collapse

Attributes included from Model

#repository

Instance Method Summary collapse

Methods included from Model

#deputy, #dsl_class, included, #store

Methods included from Common

#demodulize, #present?, #snake_case

Constructor Details

#initialize(attributes = {}) ⇒ Vedeu::Input::Keymap

Returns a new instance of Vedeu::Input::Keymap.

Parameters:

  • attributes (Hash) (defaults to: {})

Options Hash (attributes):



27
28
29
30
31
32
33
# File 'lib/vedeu/input/keymap.rb', line 27

def initialize(attributes = {})
  @attributes = defaults.merge!(attributes)

  @attributes.each do |key, value|
    instance_variable_set("@#{key}", value)
  end
end

Instance Attribute Details

#nameString

Returns:

  • (String)


16
17
18
# File 'lib/vedeu/input/keymap.rb', line 16

def name
  @name
end

Instance Method Details

#add(key) ⇒ void

This method returns an undefined value.

Add a key to the keymap.

Parameters:



39
40
41
42
43
# File 'lib/vedeu/input/keymap.rb', line 39

def add(key)
  return false unless valid?(key)

  @keys = keys.add(key)
end

#defaultsHash (private)

Returns the default options/attributes for this class.

Returns:

  • (Hash)


81
82
83
84
85
86
87
# File 'lib/vedeu/input/keymap.rb', line 81

def defaults
  {
    name:       '',
    keys:       [],
    repository: Vedeu::Input::Keymaps.keymaps,
  }
end

#key_defined?(input) ⇒ Boolean

Check whether the key is already defined for this keymap.

Parameters:

  • input (String|Symbol)

Returns:

  • (Boolean)

    A boolean indicating the input provided is already in use for this keymap.



57
58
59
# File 'lib/vedeu/input/keymap.rb', line 57

def key_defined?(input)
  keys.any? { |key| key.input == input }
end

#keysVedeu::Input::Keys

Returns the collection of keys defined for this keymap.

Returns:



48
49
50
# File 'lib/vedeu/input/keymap.rb', line 48

def keys
  collection.coerce(@keys, self)
end

#use(input) ⇒ Array|FalseClass

When the given input is registered with this keymap, this method triggers the action associated with the key.

Parameters:

  • input (String|Symbol)

Returns:

  • (Array|FalseClass)


66
67
68
69
70
71
72
73
74
# File 'lib/vedeu/input/keymap.rb', line 66

def use(input)
  return false unless key_defined?(input)

  Vedeu.log(type: :input, message: "Key pressed: '#{input}'")

  Vedeu.trigger(:key, input)

  keys.select { |key| key.input == input }.map(&:press)
end

#valid?(key) ⇒ Boolean (private)

Checks that the provided key is not already registered with this keymap.

Parameters:

Returns:

  • (Boolean)


94
95
96
97
98
99
100
101
# File 'lib/vedeu/input/keymap.rb', line 94

def valid?(key)
  return true unless key_defined?(key.input)

  Vedeu.log(type:    :input,
            message: "Keymap '#{name}' already defines '#{key.input}'.")

  false
end