Skip to content

Commit

Permalink
Calling existing before_X and after_X methods, respecting existing de…
Browse files Browse the repository at this point in the history
…finitions.
  • Loading branch information
tadman committed May 15, 2012
1 parent 27bbadc commit 13c0547
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 55 deletions.
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ begin
require 'jeweler' require 'jeweler'
Jeweler::Tasks.new do |gem| Jeweler::Tasks.new do |gem|
gem.name = "sequel_simple_callbacks" 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.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.email = "github@tadman.ca"
gem.homepage = "http://github.com/tadman/sequel_simple_callbacks" gem.homepage = "http://github.com/tadman/sequel_simple_callbacks"
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Original file line Diff line number Diff line change
@@ -1 +1 @@
0.1.1 0.1.2
57 changes: 35 additions & 22 deletions lib/sequel_simple_callbacks.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -14,12 +14,43 @@ module SequelSimpleCallbacks
STANDARD_HOOKS = (Sequel::Model::HOOKS - SPECIAL_HOOKS).freeze STANDARD_HOOKS = (Sequel::Model::HOOKS - SPECIAL_HOOKS).freeze
INSTALLABLE_HOOKS = (Sequel::Model::HOOKS + ADDITIONAL_HOOKS).freeze INSTALLABLE_HOOKS = (Sequel::Model::HOOKS + ADDITIONAL_HOOKS).freeze


def apply(model_class) def self.apply(model_class)
end end


def configure(model_class, *arguments, &block) def self.configure(model_class, *arguments, &block)
self.define_callback_hooks(model_class)
end 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 module ClassMethods
# Add a callback hook to the model with parameters: # Add a callback hook to the model with parameters:
# * :if => One of [ Symbol, Proc ] (optional) # * :if => One of [ Symbol, Proc ] (optional)
Expand Down Expand Up @@ -98,7 +129,7 @@ def add_callback(chain, *args, &block)
end end
end end


break if (result === false) break if (false === result)
end end
end end


Expand Down Expand Up @@ -142,24 +173,6 @@ def #{hook}(*args, &block)
end end


module InstanceMethods 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 # This method is provided as a simple method to call arbitrary callback
# chains without having to run through the specific method # chains without having to run through the specific method
def run_callbacks(hook) def run_callbacks(hook)
Expand Down
52 changes: 21 additions & 31 deletions sequel_simple_callbacks.gemspec
Original file line number Original file line Diff line number Diff line change
@@ -1,51 +1,41 @@
# Generated by jeweler # Generated by jeweler
# DO NOT EDIT THIS FILE DIRECTLY # 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 -*- # -*- encoding: utf-8 -*-


Gem::Specification.new do |s| Gem::Specification.new do |s|
s.name = %q{sequel_simple_callbacks} s.name = "sequel_simple_callbacks"
s.version = "0.1.1" s.version = "0.1.2"


s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version= s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["tadman"] s.authors = ["tadman"]
s.date = %q{2010-08-25} s.date = "2012-05-15"
s.description = %q{This plugin makes it possible to declare simple before and after callbacks on the class level just like ActiveRecord} s.description = "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.email = "github@tadman.ca"
s.extra_rdoc_files = [ s.extra_rdoc_files = [
"LICENSE", "LICENSE",
"README.rdoc" "README.rdoc"
] ]
s.files = [ s.files = [
".document", ".document",
".gitignore", "LICENSE",
"LICENSE", "README.rdoc",
"README.rdoc", "Rakefile",
"Rakefile", "VERSION",
"VERSION", "lib/sequel_simple_callbacks.rb",
"lib/sequel_simple_callbacks.rb", "sequel_simple_callbacks.gemspec",
"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 = [
"test/helper.rb", "test/helper.rb",
"test/models/conditional.rb", "test/models/conditional.rb",
"test/models/example.rb", "test/models/example.rb",
"test/models/model_triggers.rb", "test/models/model_triggers.rb",
"test/test_sequel_simple_callbacks.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 if s.respond_to? :specification_version then
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
s.specification_version = 3 s.specification_version = 3


if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
Expand Down

0 comments on commit 13c0547

Please sign in to comment.