Class: Vedeu::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

#demodulize, #deputy, #dsl_class, included, #store

Constructor Details

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

Parameters:

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

Options Hash (attributes):



24
25
26
27
28
29
30
# File 'lib/vedeu/input/keymap.rb', line 24

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

  @name       = @attributes[:name]
  @keys       = @attributes[:keys]
  @repository = @attributes[:repository]
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



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.

Parameters:



34
35
36
37
38
# File 'lib/vedeu/input/keymap.rb', line 34

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

  @keys = keys.add(key)
end

#defaultsHash (private)

Returns:

  • (Hash)


70
71
72
73
74
75
76
# File 'lib/vedeu/input/keymap.rb', line 70

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

#key_defined?(input) ⇒ Boolean

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

Parameters:

  • input (String|Symbol)

Returns:

  • (Boolean)

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



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

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

#keysVedeu::Keys

Returns:



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

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

#use(input) ⇒ Array|FalseClass

Triggers the action associated with the key, if it is registered with this keymap.

Parameters:

  • input (String|Symbol)

Returns:

  • (Array|FalseClass)


57
58
59
60
61
62
63
64
65
# File 'lib/vedeu/input/keymap.rb', line 57

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)


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

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

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

  false
end