Module: ConsoleExtensions

Included in:
Fertilizer
Defined in:
lib/fertilizer/console_extensions.rb

Overview

Following module allows us to call following IRB commands:

  • reload_lib - reloads whole lib folder meaning you don’t have to restart irb

  • cls - clears IRB console

  • code - displays source code for method with given instance/class

Instance Method Summary collapse

Instance Method Details

#clsObject



46
47
48
# File 'lib/fertilizer/console_extensions.rb', line 46

def cls
  system('cls')
end

#code(ints_or_clazz, method) ⇒ Object

Takes instance/class, method and displays source code and comments



51
52
53
54
55
56
57
58
# File 'lib/fertilizer/console_extensions.rb', line 51

def code(ints_or_clazz, method)
  method = method.to_sym
  clazz = ints_or_clazz.is_a?(Class) ? ints_or_clazz : ints_or_clazz.class
  puts "** Comments: "
  clazz.instance_method(method).comment.display
  puts "** Source:"
  clazz.instance_method(method).source.display
end

#reload_libObject

Reload lib configuration from console



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/fertilizer/console_extensions.rb', line 9

def reload_lib
  path = File.join( Rails.root, 'lib') 
  failures = []
  Dir.glob("#{path}/**/*.rb").each {  |file|
    puts "loading: #{file.inspect} ... "
    begin
      if !(file.include? 'reload.rb')
        load file
      end
    rescue => ex
      failures << file 
    end
  }
   
  # this second pass is here to try to catch anything that 
  # is dependent on something else
  # could be improved, but is working fine for my needs
  double_failures = []
  for file in failures 
    begin
      # don't reload yourself
      if !(file.include? 'reload.rb')
        load file
      end
    rescue => ex1
      double_failures << file 
    end
  end 

  if double_failures.size > 0 
    puts "these files failed twice"
    for file in double_failures
      puts file
    end
  end
end