-
Notifications
You must be signed in to change notification settings - Fork 530
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #65 from cerebris/callback_rework
Callback rework
- Loading branch information
Showing
7 changed files
with
326 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
require 'active_support/callbacks' | ||
|
||
module JSONAPI | ||
module Callbacks | ||
|
||
def self.included(base) | ||
base.class_eval do | ||
include ActiveSupport::Callbacks | ||
base.extend ClassMethods | ||
end | ||
end | ||
|
||
module ClassMethods | ||
def define_jsonapi_resources_callbacks(*callbacks) | ||
options = callbacks.extract_options! | ||
options = { | ||
only: [:before, :around, :after] | ||
}.merge!(options) | ||
|
||
types = Array(options.delete(:only)) | ||
|
||
callbacks.each do |callback| | ||
define_callbacks(callback, options) | ||
|
||
types.each do |type| | ||
send("_define_#{type}_callback", self, callback) | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
def _define_before_callback(klass, callback) #:nodoc: | ||
klass.define_singleton_method("before_#{callback}") do |*args, &block| | ||
set_callback(:"#{callback}", :before, *args, &block) | ||
end | ||
end | ||
|
||
def _define_around_callback(klass, callback) #:nodoc: | ||
klass.define_singleton_method("around_#{callback}") do |*args, &block| | ||
set_callback(:"#{callback}", :around, *args, &block) | ||
end | ||
end | ||
|
||
def _define_after_callback(klass, callback) #:nodoc: | ||
klass.define_singleton_method("after_#{callback}") do |*args, &block| | ||
set_callback(:"#{callback}", :after, *args, &block) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.