Module: Utils::IRB::Shell

Includes:
FileUtils, Tins::Find
Included in:
Object
Defined in:
lib/utils/irb.rb

Defined Under Namespace

Classes: ConstantWrapper, MethodWrapper, WrapperBase

Constant Summary collapse

Infinity =

I like to define the infinite.

1.0 / 0

Instance Method Summary collapse

Instance Method Details

#capture_output(with_stderr = false) ⇒ Object



228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/utils/irb.rb', line 228

def capture_output(with_stderr = false)
  require 'tempfile'
  begin
    old_stdout, $stdout = $stdout, Tempfile.new('irb')
    if with_stderr
      old_stderr, $stderr = $stderr, $stdout
    end
    yield
  ensure
    $stdout, temp = old_stdout, $stdout
    with_stderr and $stderr = old_stderr
  end
  temp.rewind
  temp.read
end

#editObject



337
338
339
# File 'lib/utils/irb.rb', line 337

def edit
  $editor.full?(:edit, self)
end

#irb_all_class_instance_methods(obj = self) ⇒ Object

Return all instance methods of obj’s class.



87
88
89
90
# File 'lib/utils/irb.rb', line 87

def irb_all_class_instance_methods(obj = self)
  methods = obj.class.instance_methods
  irb_wrap_methods obj, methods
end

#irb_all_instance_methods(modul = self) ⇒ Object

Return all instance methods defined in module modul.



100
101
102
103
# File 'lib/utils/irb.rb', line 100

def irb_all_instance_methods(modul = self)
  methods = modul.instance_methods
  irb_wrap_methods modul, methods, true
end

#irb_all_methods(obj = self) ⇒ Object

Return all methods of obj (including obj’s eigenmethods.)



113
114
115
116
# File 'lib/utils/irb.rb', line 113

def irb_all_methods(obj = self)
  methods = obj.methods
  irb_wrap_methods obj, methods
end

#irb_class_instance_methods(obj = self) ⇒ Object

Return instance methods of obj’s class without the inherited/mixed in methods.



94
95
96
97
# File 'lib/utils/irb.rb', line 94

def irb_class_instance_methods(obj = self)
  methods = obj.class.instance_methods(false)
  irb_wrap_methods obj, methods
end

#irb_connect(uri = nil) ⇒ Object

Connect to an irb server.



80
81
82
# File 'lib/utils/irb.rb', line 80

def irb_connect(uri = nil)
  Utils::IRB::Service.connect(uri)
end

#irb_constants(modul = self) ⇒ Object

Return all the constants defined in modul.



215
216
217
# File 'lib/utils/irb.rb', line 215

def irb_constants(modul = self)
  modul.constants.map { |c| ConstantWrapper.new(modul.const_get(c), c) }.sort
end

#irb_edit(*files) ⇒ Object



333
334
335
# File 'lib/utils/irb.rb', line 333

def irb_edit(*files)
  $editor.full?(:edit, *files)
end

#irb_eigen_methods(obj = self) ⇒ Object

Return all eigen methods of obj.



128
129
130
# File 'lib/utils/irb.rb', line 128

def irb_eigen_methods(obj = self)
  irb_wrap_methods obj, obj.methods(false)
end

#irb_instance_methods(modul = self) ⇒ Object

Return instance methods defined in module modul without the inherited/mixed in methods.



107
108
109
110
# File 'lib/utils/irb.rb', line 107

def irb_instance_methods(modul = self)
  methods = modul.instance_methods(false)
  irb_wrap_methods modul, methods, true
end

#irb_load!(*files) ⇒ Object



305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
# File 'lib/utils/irb.rb', line 305

def irb_load!(*files)
  files = files.map { |f| f.gsub(/(\.rb)?\Z/, '.rb') }
  loaded = {}
  for file in files
    catch :found do
      Find.find('.') do |f|
        File.directory?(f) and next
        md5_f = Utils::MD5.md5(f)
        if f.end_with?(file) and !loaded[md5_f]
          Kernel.load f
          loaded[md5_f] = true
          STDERR.puts "Loaded '#{f}'."
        end
      end
      Find.find('.') do |f|
        File.directory?(f) and next
        md5_f = Utils::MD5.md5(f)
        if f.end_with?(file) and !loaded[md5_f]
          Kernel.load f
          loaded[md5_f] = true
          STDERR.puts "Loaded '#{f}'."
        end
      end
    end
  end
  nil
end

#irb_methods(obj = self) ⇒ Object

Return instance methods of obj’s class without the inherited/mixed in methods, but including obj’s eigenmethods.



120
121
122
123
124
125
# File 'lib/utils/irb.rb', line 120

def irb_methods(obj = self)
  methods = obj.class.ancestors[1..-1].inject(obj.methods) do |all, a|
    all -= a.instance_methods
  end
  irb_wrap_methods obj, methods
end

#irb_open(url = nil, &block) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/utils/irb.rb', line 57

def irb_open(url = nil, &block)
  case
  when url
    system 'open', url
  when block
    Tempfile.open('wb') do |t|
      t.write capture_output(&block)
      t.rewind
      system 'open', t.path
    end
  when url = receiver_unless_main(method(__method__))
    irb_open url
  else
    raise ArgumentError, 'need an url or block'
  end
end

#irb_read(filename, chunk_size = 8_192) ⇒ Object



293
294
295
296
297
298
299
300
301
302
303
# File 'lib/utils/irb.rb', line 293

def irb_read(filename, chunk_size = 8_192)
  if block_given?
    File.open(filename) do |file|
      until file.eof?
        yield file.read(chunk_size)
      end
    end
  else
    IO.read filename
  end
end

#irb_server(uri = nil) ⇒ Object

Start an irb server.



75
76
77
# File 'lib/utils/irb.rb', line 75

def irb_server(uri = nil)
  Utils::IRB::Service.start(uri) {}
end

#irb_subclasses(klass = self) ⇒ Object

Return all the subclasses of klass. TODO implement subclasses w/out rails



220
221
222
# File 'lib/utils/irb.rb', line 220

def irb_subclasses(klass = self)
  klass.subclasses.map { |c| ConstantWrapper.new(eval(c), c) }.sort
end

#irb_timeObject



253
254
255
256
257
258
259
# File 'lib/utils/irb.rb', line 253

def irb_time
  s = Time.now
  yield
  d = Time.now - s
  warn "Took %.3fs seconds." % d
  d
end

#irb_time_tapObject



261
262
263
264
265
# File 'lib/utils/irb.rb', line 261

def irb_time_tap
  r = nil
  irb_time { r = yield }
  r
end

#irb_time_watch(duration = 1) ⇒ Object



267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/utils/irb.rb', line 267

def irb_time_watch(duration = 1)
  start = Time.now
  pre = nil
  loop do
    cur = [ yield ].flatten
    unless pre
      pre = cur.map(&:to_f)
      cur = [ yield ].flatten
    end
    expired = Time.now - start
    diffs = cur.zip(pre).map { |c, p| c - p }
    rates = diffs.map { |d| d / duration }
    warn "#{expired} #{cur.zip(rates, diffs).map(&:inspect) * ' '} # / per sec."
    pre = cur.map(&:to_f)
    sleep duration
  end
end

#irb_toggle_loggingObject



348
349
350
351
352
353
354
355
356
357
358
# File 'lib/utils/irb.rb', line 348

def irb_toggle_logging
  require 'logger'
  if ActiveRecord::Base.logger != $logger
    $old_logger = ActiveRecord::Base.logger
    ActiveRecord::Base.logger = $logger
    true
  else
    ActiveRecord::Base.logger = $old_logger
    false
  end
end

#irb_wrap_methods(obj = self, methods = methods(), modul = false) ⇒ Object



132
133
134
135
136
# File 'lib/utils/irb.rb', line 132

def irb_wrap_methods(obj = self, methods = methods(), modul = false)
  methods.map do |name|
    MethodWrapper.new(obj, name, modul) rescue nil
  end.compact.sort!
end

#irb_write(filename, text = nil, &block) ⇒ Object



285
286
287
288
289
290
291
# File 'lib/utils/irb.rb', line 285

def irb_write(filename, text = nil, &block)
  if text.nil? && block
    File.secure_write filename, nil, 'wb', &block
  else
    File.secure_write filename, text, 'wb'
  end
end

#less(with_stderr = false, &block) ⇒ Object

Use pager on the output of the commands given in the block.



245
246
247
248
249
250
251
# File 'lib/utils/irb.rb', line 245

def less(with_stderr = false, &block)
  IO.popen($pager, 'w') do |f|
    f.write capture_output(with_stderr, &block)
    f.close_write
  end
  nil
end

#ls(*args) ⇒ Object

List contents of directory



342
343
344
# File 'lib/utils/irb.rb', line 342

def ls(*args)
  puts `ls #{args.map { |x| "'#{x}'" } * ' '}`
end

#ri(*patterns, doc: 'ri') ⇒ Object

Start ri for pattern. If pattern is not string like, call it with pattern.class.name as argument.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/utils/irb.rb', line 35

def ri(*patterns, doc: 'ri')
  patterns.empty? and
    receiver_unless_main(method(__method__)) do |pattern|
      return ri(pattern, doc: doc)
    end
  patterns.map! { |p|
    case
    when Module === p
      p.name
    when p.respond_to?(:to_str)
      p.to_str
    else
      p.class.name
    end
  }
  system "#{doc} #{patterns.map { |p| "'#{p}'" } * ' ' } | #$pager"
end

#yri(*patterns) ⇒ Object



53
54
55
# File 'lib/utils/irb.rb', line 53

def yri(*patterns)
  ri *patterns, doc: 'yri'
end