Class: PryMoves::Watch

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/pry-moves/watch.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeWatch

Returns a new instance of Watch.



10
11
12
# File 'lib/pry-moves/watch.rb', line 10

def initialize
  @list = Set.new
end

Instance Attribute Details

#listObject (readonly)

Returns the value of attribute list.



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

def list
  @list
end

Instance Method Details

#add(cmd, binding_) ⇒ Object



29
30
31
32
# File 'lib/pry-moves/watch.rb', line 29

def add(cmd, binding_)
  @list << cmd
  puts eval_cmd(cmd, binding_)
end

#empty?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/pry-moves/watch.rb', line 56

def empty?
  @list.empty?
end

#eval_cmd(cmd, binding_) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/pry-moves/watch.rb', line 44

def eval_cmd(cmd, binding_)
  "\033[1m#{cmd}\033[0m: #{format binding_.eval(cmd)}"
rescue NameError
  "\033[1m#{cmd}\033[0m: <undefined>"
rescue => e
  "\033[1m#{cmd}\033[0m: <#{e}>"
end

#format(text) ⇒ Object



52
53
54
# File 'lib/pry-moves/watch.rb', line 52

def format(text)
  Pry::ColorPrinter.pp(text, "").strip
end

#output(binding_) ⇒ Object



38
39
40
41
42
# File 'lib/pry-moves/watch.rb', line 38

def output(binding_)
  @list.map do |cmd|
    eval_cmd(cmd, binding_)
  end.join "; "
end


34
35
36
# File 'lib/pry-moves/watch.rb', line 34

def print(binding_)
  puts output(binding_) if @list.count > 0
end

#process_cmd(cmd, binding_) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/pry-moves/watch.rb', line 14

def process_cmd(cmd, binding_)
  case cmd
    when nil, ''
      if @list.count > 0
        print binding_
      else
        puts "Watch list is empty"
      end
    when '-clear', '-c'
      @list.clear
    else
      add cmd, binding_
  end
end