Skip to content

Instantly share code, notes, and snippets.

@razielgn
Created August 8, 2011 12:27
Show Gist options
  • Save razielgn/1131665 to your computer and use it in GitHub Desktop.
Save razielgn/1131665 to your computer and use it in GitHub Desktop.
require 'tempfile'
module Arnold
def edit
YAML.load(change).each { |key, value| write_attribute(key, value) }
end
def edit!
edit
save!
end
private
def change
path = tempfile
system [editor, path].join(' ')
File.read(path)
end
def tempfile
tmp = Tempfile.new(id.to_s)
File.open(tmp.path, 'w') { |file| file.write(attributes.to_yaml) }
tmp.path
end
def editor
exec = ENV['EDITOR'] || 'vi'
EDITORS.each_pair{|cmd, args| return "#{cmd} #{args}" if cmd == exec}
exec
end
EDITORS = {"mate" => "-w",
"subl" => "-w"}
end
Mongoid::Document.send(:include, Arnold) if defined? Mongoid::Document
ActiveRecord::Base.send(:include, Arnold) if defined? ActiveRecord::Base
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment