Class: PryKeybind

Inherits:
Object
  • Object
show all
Defined in:
lib/version.rb,
lib/pry-keybind.rb

Defined Under Namespace

Classes: InputState, KeySequence

Constant Summary collapse

VERSION =
"0.0.1"

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, options = {}, &block) ⇒ PryKeybind

Returns a new instance of PryKeybind.

Raises:

  • (ArgumentError)


39
40
41
42
43
44
45
46
# File 'lib/pry-keybind.rb', line 39

def initialize(key, options = {}, &block)
  raise ArgumentError, "block required" unless block_given?

  @options = options
  @key = KeySequence.new(key)
  @block = block
  @bound = false
end

Class Attribute Details

.layersObject

Returns the value of attribute layers.



5
6
7
# File 'lib/pry-keybind.rb', line 5

def layers
  @layers
end

.registryObject

Returns the value of attribute registry.



4
5
6
# File 'lib/pry-keybind.rb', line 4

def registry
  @registry
end

Instance Attribute Details

#pry_instanceObject

Returns the value of attribute pry_instance.



8
9
10
# File 'lib/pry-keybind.rb', line 8

def pry_instance
  @pry_instance
end

Class Method Details

.bind_all!(pry, source: nil) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/pry-keybind.rb', line 20

def self.bind_all!(pry, source: nil)
  # puts "binding / layer size?: #{layers.size} / source: #{source}"
  layers << [*registry.values].map do |key_binding|
    key_binding.pry_instance = pry
    key_binding.bind!
  end.compact
end

.register(constant, key, options = {}, &block) ⇒ Object



13
14
15
16
17
# File 'lib/pry-keybind.rb', line 13

def self.register(constant, key, options = {}, &block)
  self.registry ||= {}
  self.layers ||= []
  self.registry[constant] = new(key, options, &block)
end

.unbind_all!(pry_instance, source: nil) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/pry-keybind.rb', line 28

def self.unbind_all!(pry_instance, source: nil)
  # puts "unbinding / layer size?: #{layers.size} / source: #{source}"
  return unless layer = layers.pop

  layer.each do |key_binding|
    key_binding.pry_instance = pry_instance
    key_binding.unbind!
  end
end

Instance Method Details

#bind!Object



48
49
50
51
52
53
54
55
56
# File 'lib/pry-keybind.rb', line 48

def bind!
  return nil if bound?

  Pryline.public_send(bind_method, key.for_readline, &block_caller)

  @bound = true

  self
end

#unbind!Object



58
59
60
61
62
63
64
65
66
# File 'lib/pry-keybind.rb', line 58

def unbind!
  return self unless bound?

  Pryline.public_send(unbind_method, key.for_readline)

  @bound = false

  self
end