public
Last active

Bang!

  • Download Gist
bang.gemspec
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
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
bang.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12
module Bang
 
def bang(attributes)
 
[*attributes].each do |attribute|
key, value = attribute
define_method("#{key}!") { update_attribute(value || key, send(key)) }
end
 
end
 
end
bang_spec.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.