Class: Vedeu::Input::Mapper

Inherits:
Object
  • Object
show all
Extended by:
Common
Defined in:
lib/vedeu/input/mapper.rb

Overview

Maps keys to keymaps.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key = nil, name = nil, repository = nil) ⇒ Vedeu::Input::Mapper

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

Parameters:

  • key (NilClass|String|Symbol) (defaults to: nil)
  • name (NilClass|String) (defaults to: nil)
  • repository (NilClass|Vedeu::Repositories::Repository) (defaults to: nil)


61
62
63
64
65
# File 'lib/vedeu/input/mapper.rb', line 61

def initialize(key = nil, name = nil, repository = nil)
  @key        = key
  @name       = name
  @repository = repository || Vedeu.keymaps
end

Instance Attribute Details

#keyString|Symbol (readonly, protected)

Returns:

  • (String|Symbol)


106
107
108
# File 'lib/vedeu/input/mapper.rb', line 106

def key
  @key
end

#repositoryVedeu::Repositories::Repository (readonly, protected)



110
111
112
# File 'lib/vedeu/input/mapper.rb', line 110

def repository
  @repository
end

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

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

.keypress(key = nil, name = nil) ⇒ Boolean

Takes a key as a keypress and sends it to registered keymaps. If found, the associated action is fired, otherwise, we move to the next keymap or return false.

Examples:

Vedeu.keypress(key_name, keymap_name)

Parameters:

Returns:

  • (Boolean)


22
23
24
25
26
27
28
# File 'lib/vedeu/input/mapper.rb', line 22

def keypress(key = nil, name = nil)
  Vedeu.trigger(:key, key)

  return false unless key

  new(key, name).keypress
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)

.registered?(key = nil, name = nil) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
33
34
35
36
37
38
39
# File 'lib/vedeu/input/mapper.rb', line 30

def registered?(key = nil, name = nil)
  fail Vedeu::Error::MissingRequired,
       'Cannot check whether a key is registered to a keymap without ' \
       'the key.' if absent?(key)
  fail Vedeu::Error::MissingRequired,
       'Cannot check whether a key is registered to a keymap without ' \
       'the keymap name.' if absent?(name)

  new(key, name).registered?
end

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

.valid?(key = nil, name = nil) ⇒ Boolean

Checks a key is valid; i.e. not already registered to a keymap. When the key is registered, then the key is invalid and cannot be used again.

Parameters:

Returns:

  • (Boolean)


47
48
49
50
51
# File 'lib/vedeu/input/mapper.rb', line 47

def valid?(key = nil, name = nil)
  return false unless key

  new(key, name).valid?
end

Instance Method Details

#global_key?Boolean (private)

Is the key a global key?

Returns:

  • (Boolean)


117
118
119
# File 'lib/vedeu/input/mapper.rb', line 117

def global_key?
  key_defined?('_global_')
end

#key_defined?(named = name) ⇒ Boolean Also known as: registered?

Is the key defined in the named keymap?

Parameters:

  • named (NilClass|String) (defaults to: name)

Returns:

  • (Boolean)


87
88
89
# File 'lib/vedeu/input/mapper.rb', line 87

def key_defined?(named = name)
  keymap?(named) && keymap(named).key_defined?(key)
end

#keymap(named = name) ⇒ Vedeu::Input::Keymap (private)

Fetch the named keymap from the repository.

Parameters:

  • named (NilClass|String) (defaults to: name)

Returns:



125
126
127
# File 'lib/vedeu/input/mapper.rb', line 125

def keymap(named = name)
  repository.find(named)
end

#keymap?(named = name) ⇒ Boolean (private)

Does the keymaps repository have the named keymap already registered?

Parameters:

  • named (NilClass|String) (defaults to: name)

Returns:

  • (Boolean)


134
135
136
# File 'lib/vedeu/input/mapper.rb', line 134

def keymap?(named = name)
  repository.registered?(named)
end

#keypressBoolean

Returns a boolean indicating that the key is registered to the current keymap, or the global keymap.

Returns:

  • (Boolean)


71
72
73
74
75
76
77
78
79
80
81
# File 'lib/vedeu/input/mapper.rb', line 71

def keypress
  return false unless key

  return true if key_defined? && keymap.use(key)

  return true if global_key? && keymap('_global_').use(key)

  Vedeu.log(type: :input, message: "Key detected: #{key.inspect}".freeze)

  false
end

#nameString|NilClass (private) Also known as: interface

With a name, we check the keymap with that name, otherwise we use the name of the interface currently in focus.

Returns:

  • (String|NilClass)


142
143
144
# File 'lib/vedeu/input/mapper.rb', line 142

def name
  @name || Vedeu.focus
end

#valid?Boolean

Returns a boolean indicating that the key is not registered to the current keymap, or the global keymap.

Returns:

  • (Boolean)


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

def valid?
  return false if !key || key_defined? || global_key?

  true
end