Permalink
Browse files

Calling existing before_X and after_X methods, respecting existing de…

…finitions.
  • Loading branch information...
1 parent 27bbadc commit 13c0547fbf8559879b8b2836abc1387e041dd5c4 @tadman committed May 15, 2012
Showing with 58 additions and 55 deletions.
  1. +1 −1 Rakefile
  2. +1 −1 VERSION
  3. +35 −22 lib/sequel_simple_callbacks.rb
  4. +21 −31 sequel_simple_callbacks.gemspec
View
@@ -5,7 +5,7 @@ begin
require 'jeweler'
Jeweler::Tasks.new do |gem|
gem.name = "sequel_simple_callbacks"
- gem.summary = %Q[Sequel Plugin to add ActiveRecord style callback declarations]
+ gem.summary = %Q[Sequel Model plugin that enables ActiveRecord-compatible class-level before and after filter declarations for models with support for conditional execution]
gem.description = %Q[This plugin makes it possible to declare simple before and after callbacks on the class level just like ActiveRecord]
gem.email = "github@tadman.ca"
gem.homepage = "http://github.com/tadman/sequel_simple_callbacks"
View
@@ -1 +1 @@
-0.1.1
+0.1.2
@@ -14,12 +14,43 @@ module SequelSimpleCallbacks
STANDARD_HOOKS = (Sequel::Model::HOOKS - SPECIAL_HOOKS).freeze
INSTALLABLE_HOOKS = (Sequel::Model::HOOKS + ADDITIONAL_HOOKS).freeze
- def apply(model_class)
+ def self.apply(model_class)
end
- def configure(model_class, *arguments, &block)
+ def self.configure(model_class, *arguments, &block)
+ self.define_callback_hooks(model_class)
end
-
+
+ def self.define_callback_hooks(model_class)
+ STANDARD_HOOKS.each do |hook|
+ pre_hook = nil
+
+ if (model_class.instance_methods.include?(hook))
+ pre_hook = :"_internal_#{hook}"
+
+ model_class.send(:alias_method, pre_hook, hook)
+ end
+
+ model_class.send(:define_method, hook) do
+ self.class.run_callbacks(self, hook)
+
+ self.send(pre_hook) if (pre_hook)
+ end
+ end
+
+ SPECIAL_HOOKS.each do |hook|
+ model_class.send(:define_method, hook) do
+ self.class.run_callbacks(self, hook)
+
+ if (new?)
+ self.class.run_callbacks(self, :"#{hook}_on_create")
+ else
+ self.class.run_callbacks(self, :"#{hook}_on_update")
+ end
+ end
+ end
+ end
+
module ClassMethods
# Add a callback hook to the model with parameters:
# * :if => One of [ Symbol, Proc ] (optional)
@@ -98,7 +129,7 @@ def add_callback(chain, *args, &block)
end
end
- break if (result === false)
+ break if (false === result)
end
end
@@ -142,24 +173,6 @@ def #{hook}(*args, &block)
end
module InstanceMethods
- STANDARD_HOOKS.each do |hook|
- define_method(hook) do
- self.class.run_callbacks(self, hook)
- end
- end
-
- SPECIAL_HOOKS.each do |hook|
- define_method(hook) do
- self.class.run_callbacks(self, hook)
-
- if (new?)
- self.class.run_callbacks(self, :"#{hook}_on_create")
- else
- self.class.run_callbacks(self, :"#{hook}_on_update")
- end
- end
- end
-
# This method is provided as a simple method to call arbitrary callback
# chains without having to run through the specific method
def run_callbacks(hook)
@@ -1,51 +1,41 @@
# Generated by jeweler
# DO NOT EDIT THIS FILE DIRECTLY
-# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
+# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
# -*- encoding: utf-8 -*-
Gem::Specification.new do |s|
- s.name = %q{sequel_simple_callbacks}
- s.version = "0.1.1"
+ s.name = "sequel_simple_callbacks"
+ s.version = "0.1.2"
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["tadman"]
- s.date = %q{2010-08-25}
- s.description = %q{This plugin makes it possible to declare simple before and after callbacks on the class level just like ActiveRecord}
- s.email = %q{github@tadman.ca}
+ s.date = "2012-05-15"
+ s.description = "This plugin makes it possible to declare simple before and after callbacks on the class level just like ActiveRecord"
+ s.email = "github@tadman.ca"
s.extra_rdoc_files = [
"LICENSE",
- "README.rdoc"
+ "README.rdoc"
]
s.files = [
".document",
- ".gitignore",
- "LICENSE",
- "README.rdoc",
- "Rakefile",
- "VERSION",
- "lib/sequel_simple_callbacks.rb",
- "sequel_simple_callbacks.gemspec",
- "test/helper.rb",
- "test/models/conditional.rb",
- "test/models/example.rb",
- "test/models/model_triggers.rb",
- "test/test_sequel_simple_callbacks.rb"
- ]
- s.homepage = %q{http://github.com/tadman/sequel_simple_callbacks}
- s.rdoc_options = ["--charset=UTF-8"]
- s.require_paths = ["lib"]
- s.rubygems_version = %q{1.3.7}
- s.summary = %q{Sequel Plugin to add ActiveRecord style callback declarations}
- s.test_files = [
+ "LICENSE",
+ "README.rdoc",
+ "Rakefile",
+ "VERSION",
+ "lib/sequel_simple_callbacks.rb",
+ "sequel_simple_callbacks.gemspec",
"test/helper.rb",
- "test/models/conditional.rb",
- "test/models/example.rb",
- "test/models/model_triggers.rb",
- "test/test_sequel_simple_callbacks.rb"
+ "test/models/conditional.rb",
+ "test/models/example.rb",
+ "test/models/model_triggers.rb",
+ "test/test_sequel_simple_callbacks.rb"
]
+ s.homepage = "http://github.com/tadman/sequel_simple_callbacks"
+ s.require_paths = ["lib"]
+ s.rubygems_version = "1.8.24"
+ s.summary = "Sequel Model plugin that enables ActiveRecord-compatible class-level before and after filter declarations for models with support for conditional execution"
if s.respond_to? :specification_version then
- current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
s.specification_version = 3
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then

0 comments on commit 13c0547

Please sign in to comment.