class RspecEditor::Editor

Attributes

editor[RW]
output_file[RW]

Public Instance Methods

check_enabled!() click to toggle source
# File lib/rspec_editor/editor.rb, line 7
def check_enabled!
  unless enabled?
    log "set $RSPEC_EDITOR to enable."
  end
  self
end
close(*args) click to toggle source
# File lib/rspec_editor/editor.rb, line 38
def close *args
  return self unless @out
  @out.close rescue nil
  @out = nil
  File.rename(@tmp_file, output_file)
  if @log_already_exists
    log "# Refresh #{output_file} in your editor."
  else
    editor! output_file
  end
  self
ensure
  @out = @log_already_exists = nil
end
current_directory() click to toggle source
# File lib/rspec_editor/editor.rb, line 26
def current_directory
  @current_directory ||= Dir.pwd
end
editor!(file, line = nil) click to toggle source
# File lib/rspec_editor/editor.rb, line 66
def editor! file, line = nil
  cmd = editor_cmd(file, line)
  log "Using editor: #{cmd}"
  begin
    system! cmd if cmd
  rescue
    $stderr.puts "  #{self}: ERROR: #{cmd}: #{$!.inspect}"
  end
end
editor_cmd(file, line = nil) click to toggle source
# File lib/rspec_editor/editor.rb, line 76
def editor_cmd file, line = nil
  file = File.expand_path(file)
  line = nil if line && line.empty?
  editor = self.editor
  case editor
  when ""
    cmd = nil
  when /emacs/
    line &&= "+#{line}"
    unless File.exist?(editor)
      editor = emacsclient
    end
    cmd = "#{editor} -q -n #{line} #{file}"
  when /vi/
    line &&= "+#{line}"
    cmd = "#{editor} --remote #{line} #{file}"
  else
    line &&= "--line #{line}"
    cmd = "#{editor} #{line} #{file}"
  end
  cmd
end
emacsclient() click to toggle source
# File lib/rspec_editor/editor.rb, line 109
def emacsclient
  @emacsclient ||=
  [
    '/Applications/Aquamacs.app/Contents/MacOS/bin/emacsclient',
    '/opt/local/bin/emacsclient',
    '/usr/bin/emacsclient',
  ].find{|f| File.executable? f} || 'emacsclient'
end
enabled?() click to toggle source
# File lib/rspec_editor/editor.rb, line 14
def enabled?
  ! ! editor
end
log(msg) click to toggle source
# File lib/rspec_editor/editor.rb, line 105
def log msg
  $stderr.puts "  #{self.class}: #{msg}"
end
open() click to toggle source
# File lib/rspec_editor/editor.rb, line 30
def open
  return if @out
  @log_already_exists = File.exist?(output_file)
  @out = File.open(@tmp_file = "#{output_file}.tmp", "w")
  puts "-*- mode: grep; mode: auto-revert; default-directory: \"#{current_directory}/\" -*-"
  puts ""
end
open_example!(location) click to toggle source
# File lib/rspec_editor/editor.rb, line 57
def open_example! location
  if location.to_s =~ /^([^:]+)(:(\d+))?$/
    file, line = $1, $3
    editor! file, line
  else
    raise ArgumentError, "Invalid location"
  end
end
puts(msg) click to toggle source
# File lib/rspec_editor/editor.rb, line 53
def puts msg
  @out.puts msg
end
system!(cmd) click to toggle source
# File lib/rspec_editor/editor.rb, line 99
def system! cmd
  Process.fork do
    system(cmd)
  end
end