Class: Vedeu::Input::Keymap

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

Overview

A container class for keys associated with a particular interface.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

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

Parameters:

Options Hash (attributes):

  • name (String|Symbol)

    The name of the keymap.

  • keys (Vedeu::Input::Keys|Array)

    A collection of keys.

  • repository (Object)
    Vedeu::Repositories::Repository

    This model’s storage.



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

def initialize(attributes = {})
  defaults.merge!(attributes).each do |key, value|
    instance_variable_set("@#{key}", value)
  end
end

Instance Attribute Details

#nameString

Returns:

  • (String)


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

def name
  @name
end

#repositoryVedeu::Repositories::Repository Originally defined in module Repositories::Model

Instance Method Details

#absent?(variable) ⇒ Boolean Originally defined in module Common

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.

Returns a boolean indicating whether a variable is nil or empty.

Parameters:

  • variable (String|Symbol|Array|Fixnum)

    The variable to check.

Returns:

  • (Boolean)

#add(key) ⇒ void

This method returns an undefined value.

Add a key to the keymap.

Parameters:



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

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

  @keys = keys.add(key)
end

#defaultsHash<Symbol => Array|String|Vedeu::Input::Repository] (private)

Returns the default options/attributes for this class.

Returns:



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

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

#demodulize(klass) ⇒ String Originally defined in module Common

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.

Removes the module part from the expression in the string.

Examples:

demodulize('Vedeu::SomeModule::SomeClass') # => "SomeClass"

Parameters:

  • klass (Class|String)

Returns:

  • (String)

#deputy(client = nil) ⇒ Vedeu::Input::DSL

Returns a DSL instance responsible for defining the DSL methods of this model.

Parameters:

  • client (Object|NilClass) (defaults to: nil)

    The client binding represents the client application object that is currently invoking a DSL method. It is required so that we can send messages to the client application object should we need to.

Returns:



54
55
56
# File 'lib/vedeu/input/keymap.rb', line 54

def deputy(client = nil)
  Vedeu::Input::DSL.new(self, client)
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.



70
71
72
# File 'lib/vedeu/input/keymap.rb', line 70

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

#keysVedeu::Input::Keys

Returns the collection of keys defined for this keymap.

Returns:



61
62
63
# File 'lib/vedeu/input/keymap.rb', line 61

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

#present?(variable) ⇒ Boolean Originally defined in module Common

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.

Returns a boolean indicating whether a variable has a useful value.

Parameters:

  • variable (String|Symbol|Array|Fixnum)

    The variable to check.

Returns:

  • (Boolean)

#snake_case(name) ⇒ String Originally defined in module Common

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.

Converts a class name to a lowercase snake case string.

Examples:

snake_case(MyClassName) # => "my_class_name"
snake_case(NameSpaced::ClassName)
# => "name_spaced/class_name"

Parameters:

  • name (String)

Returns:

  • (String)

#storevoid Originally defined in module Repositories::Model

TODO:

Perhaps some validation could be added here?

Note:

If a block is given, store the model, return the model after yielding.

This method returns an undefined value.

Returns The model instance stored in the repository.

#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)


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

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

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

  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)


107
108
109
110
111
112
113
114
115
# File 'lib/vedeu/input/keymap.rb', line 107

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

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

  false
end