Skip to content

Instantly share code, notes, and snippets.

@jeffkreeftmeijer
Created September 21, 2011 18:26
Show Gist options
  • Star 23 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save jeffkreeftmeijer/1232884 to your computer and use it in GitHub Desktop.
Save jeffkreeftmeijer/1232884 to your computer and use it in GitHub Desktop.
Bang!
Gem::Specification.new do |s|
s.name = 'bang'
s.version = '0.1.0'
s.platform = Gem::Platform::RUBY
s.author = 'Jeff Kreeftmeijer'
s.email = 'jeff@kreeftmeijer.nl'
s.summary = 'Bang!'
s.description = 'Bangs existing model methods'
s.files = ['bang.rb']
s.test_file = 'bang_spec.rb'
s.require_path = '.'
s.add_development_dependency('rspec', ["~> 2.0"])
end
module Bang
def bang(attributes)
[*attributes].each do |attribute|
key, value = attribute
define_method("#{key}!") { update_attribute(value || key, send(key)) }
end
end
end
require File.expand_path('bang')
class ObjectWithBang
extend Bang
end
describe Bang do
let(:object) do
object = ObjectWithBang.new
object.stub(:attribute).and_return(1)
object.stub(:get_attribute).and_return(2)
object
end
context 'with one banged attribute' do
before { ObjectWithBang.send(:bang, :attribute) }
it 'should save the attribute value' do
object.should_receive(:update_attribute).with(:attribute, 1)
object.attribute!
end
end
context 'when the attribute and method names differ' do
before { ObjectWithBang.send(:bang, :get_attribute => :attribute) }
it 'should save the attribute value' do
object.should_receive(:update_attribute).with(:attribute, 2)
object.get_attribute!
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment