Skip to content

Instantly share code, notes, and snippets.

@jeffkreeftmeijer
Forked from razielgn/arnold.rb
Created August 8, 2011 12:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeffkreeftmeijer/1131668 to your computer and use it in GitHub Desktop.
Save jeffkreeftmeijer/1131668 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
editor = ENV['EDITOR'] || 'vi'
[editor, extra_editor_options[editor]].compact.join(' ')
end
def extra_editor_options
{'mate' => '-w'}
end
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