Class: Fir::KeyCommand

Inherits:
Object
  • Object
show all
Defined in:
lib/fir/key_command/key_command.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(character, state) ⇒ KeyCommand

Returns a new instance of KeyCommand.



39
40
41
42
# File 'lib/fir/key_command/key_command.rb', line 39

def initialize(character, state)
  @character = character
  @state = state
end

Instance Attribute Details

#characterObject (readonly)

Returns the value of attribute character.



6
7
8
# File 'lib/fir/key_command/key_command.rb', line 6

def character
  @character
end

#stateObject (readonly)

Returns the value of attribute state.



7
8
9
# File 'lib/fir/key_command/key_command.rb', line 7

def state
  @state
end

Class Method Details

.build(character, state) ⇒ Object



9
10
11
# File 'lib/fir/key_command/key_command.rb', line 9

def self.build(character, state)
  find(character).new(character, state)
end

.character_regexObject



35
36
37
# File 'lib/fir/key_command/key_command.rb', line 35

def self.character_regex
  /.*/
end

.for(character, state) ⇒ Object



13
14
15
16
17
# File 'lib/fir/key_command/key_command.rb', line 13

def self.for(character, state)
  registry.find do |candidate|
    candidate.handles?(character)
  end.new(character, state)
end

.handles?(character) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/fir/key_command/key_command.rb', line 31

def self.handles?(character)
  Array(character_regex).any? { |re| re.match(character) }
end

.inherited(candidate) ⇒ Object



27
28
29
# File 'lib/fir/key_command/key_command.rb', line 27

def self.inherited(candidate)
  register(candidate)
end

.register(candidate) ⇒ Object



23
24
25
# File 'lib/fir/key_command/key_command.rb', line 23

def self.register(candidate)
  registry.unshift(candidate)
end

.registryObject



19
20
21
# File 'lib/fir/key_command/key_command.rb', line 19

def self.registry
  @registry ||= [KeyCommand]
end

Instance Method Details

#executeObject



44
45
46
# File 'lib/fir/key_command/key_command.rb', line 44

def execute
  execute_hook(state.clone)
end

#execute_hook(new_state) ⇒ Object



48
49
50
# File 'lib/fir/key_command/key_command.rb', line 48

def execute_hook(new_state)
  new_state
end