dirty_callbacks 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
@@ -0,0 +1,5 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 James Golick
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,37 @@
1
+ = Dirty Callbacks
2
+
3
+ Dirty callbacks is an active record plugin that adds some callbacks for tracking changes to fields.
4
+
5
+ Currently, it adds two types of callbacks:
6
+
7
+ before_field_changes_on_update & after_field_changed_on_update
8
+
9
+ This allows you to hook a callback in to the save cycle that only gets called if a particular field is changed in the save.
10
+
11
+ This is especially useful in conjunction with my {observational}[http://github.com/giraffesoft/observational] plugin.
12
+
13
+ == Installation
14
+
15
+ sudo gem install giraffesoft-dirty_callbacks
16
+
17
+ == Usage
18
+
19
+ To get dirty callbacks, you need to include the module in the model you want to use it in.
20
+
21
+ Then, just declare the callbacks as normal:
22
+
23
+ class User < ActiveRecord::BAse
24
+ include DirtyCallbacks
25
+
26
+ before_email_changes_on_update :do_stuff
27
+
28
+ protected
29
+ def do_stuff
30
+ puts email_was
31
+ puts email
32
+ end
33
+ end
34
+
35
+ == Copyright
36
+
37
+ Copyright (c) 2009 James Golick. See LICENSE for details.
@@ -0,0 +1,48 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "dirty_callbacks"
8
+ gem.summary = %Q{TODO}
9
+ gem.email = "james@giraffesoft.ca"
10
+ gem.homepage = "http://github.com/giraffesoft/dirty_callbacks"
11
+ gem.authors = ["James Golick"]
12
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
13
+ end
14
+
15
+ rescue LoadError
16
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
17
+ end
18
+
19
+ require 'spec/rake/spectask'
20
+ Spec::Rake::SpecTask.new(:spec) do |spec|
21
+ spec.libs << 'lib' << 'spec'
22
+ spec.spec_files = FileList['spec/**/*_spec.rb']
23
+ end
24
+
25
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
26
+ spec.libs << 'lib' << 'spec'
27
+ spec.pattern = 'spec/**/*_spec.rb'
28
+ spec.rcov = true
29
+ end
30
+
31
+
32
+ task :default => :spec
33
+
34
+ require 'rake/rdoctask'
35
+ Rake::RDocTask.new do |rdoc|
36
+ if File.exist?('VERSION.yml')
37
+ config = YAML.load(File.read('VERSION.yml'))
38
+ version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
39
+ else
40
+ version = ""
41
+ end
42
+
43
+ rdoc.rdoc_dir = 'rdoc'
44
+ rdoc.title = "dirty_callbacks #{version}"
45
+ rdoc.rdoc_files.include('README*')
46
+ rdoc.rdoc_files.include('lib/**/*.rb')
47
+ end
48
+
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,19 @@
1
+ module DirtyCallbacks
2
+ def self.included(klass)
3
+ klass.class_eval do
4
+ columns.each do |f|
5
+ f = f.name
6
+ define_callbacks :"before_#{f}_changes_on_update", :"after_#{f}_changed_on_update"
7
+
8
+ before_update do |o|
9
+ o.send :callback, :"before_#{f}_changes_on_update" if o.send :"#{f}_changed?"
10
+ end
11
+
12
+ after_update do |o|
13
+ o.send :callback, :"after_#{f}_changed_on_update" if o.send :"#{f}_changed?"
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
19
+
@@ -0,0 +1,39 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "DirtyCallbacks" do
4
+ describe "callback before a changed field is saved - on update" do
5
+ before do
6
+ @user = User.create
7
+ User.send :before_email_changes_on_update, :do_stuff
8
+ end
9
+
10
+ it "should fire in the before_save_on_update callback if the field has chnaged" do
11
+ @user.email = "whatever"
12
+ @user.expects(:do_stuff)
13
+ @user.send :callback, :before_update
14
+ end
15
+
16
+ it "should not fire in the before_save_on_update callback if the field hasn't chnaged" do
17
+ @user.expects(:do_stuff).never
18
+ @user.send :callback, :before_update
19
+ end
20
+ end
21
+
22
+ describe "callback after a changed field is saved - on update" do
23
+ before do
24
+ @user = User.create
25
+ User.send :after_email_changed_on_update, :do_stuff
26
+ end
27
+
28
+ it "should fire in the after_update callback if the field has chnaged" do
29
+ @user.email = "whatever"
30
+ @user.expects(:do_stuff)
31
+ @user.send :callback, :after_update
32
+ end
33
+
34
+ it "should not fire in the after_update callback if the field hasn't chnaged" do
35
+ @user.expects(:do_stuff).never
36
+ @user.send :callback, :after_update
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,28 @@
1
+ require 'spec'
2
+ require 'rubygems'
3
+
4
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ require 'active_record'
7
+ require 'dirty_callbacks'
8
+
9
+ Spec::Runner.configure do |config|
10
+ config.mock_with :mocha
11
+ end
12
+
13
+ ActiveRecord::Base.configurations = {'sqlite3' => {:adapter => 'sqlite3', :database => ':memory:'}}
14
+ ActiveRecord::Base.establish_connection('sqlite3')
15
+
16
+ ActiveRecord::Base.logger = Logger.new(STDERR)
17
+ ActiveRecord::Base.logger.level = Logger::WARN
18
+
19
+ ActiveRecord::Schema.define(:version => 0) do
20
+ create_table :users do |t|
21
+ t.string :email, :default => ''
22
+ end
23
+ end
24
+
25
+ class User < ActiveRecord::Base
26
+ include DirtyCallbacks
27
+ end
28
+
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dirty_callbacks
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - James Golick
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-08-05 00:00:00 -04:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: james@giraffesoft.ca
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - LICENSE
24
+ - README.rdoc
25
+ files:
26
+ - .document
27
+ - .gitignore
28
+ - LICENSE
29
+ - README.rdoc
30
+ - Rakefile
31
+ - VERSION
32
+ - lib/dirty_callbacks.rb
33
+ - spec/dirty_callbacks_spec.rb
34
+ - spec/spec_helper.rb
35
+ has_rdoc: true
36
+ homepage: http://github.com/giraffesoft/dirty_callbacks
37
+ post_install_message:
38
+ rdoc_options:
39
+ - --charset=UTF-8
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: "0"
47
+ version:
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: "0"
53
+ version:
54
+ requirements: []
55
+
56
+ rubyforge_project:
57
+ rubygems_version: 1.3.1
58
+ signing_key:
59
+ specification_version: 2
60
+ summary: TODO
61
+ test_files:
62
+ - spec/dirty_callbacks_spec.rb
63
+ - spec/spec_helper.rb