-
Notifications
You must be signed in to change notification settings - Fork 78
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 #408 from projecthydra-labs/callbacks
Implements a callback api
- Loading branch information
Showing
12 changed files
with
196 additions
and
47 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
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
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
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,26 @@ | ||
require 'active_support/concern' | ||
require 'curation_concerns/callbacks/registry' | ||
|
||
module CurationConcerns | ||
module Callbacks | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
# Define class instance variable as endpoint to the | ||
# Callback::Registry api. | ||
@callback = Registry.new | ||
end | ||
|
||
module ClassMethods | ||
# Reader for class instance variable containing callback definitions. | ||
def callback | ||
@callback | ||
end | ||
end | ||
|
||
# Accessor to Callback::Registry api for instances. | ||
def callback | ||
self.class.callback | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
module CurationConcerns | ||
module Callbacks | ||
class Registry | ||
attr_reader :callbacks | ||
|
||
def initialize | ||
@callbacks = {} | ||
end | ||
|
||
# Enables a callback by specifying one or more hooks. | ||
def enable(hook, *more_hooks) | ||
([hook] + more_hooks).each { |h| @callbacks[h] ||= nil } | ||
end | ||
|
||
# Returns all enabled callback hooks. | ||
def enabled | ||
@callbacks.keys | ||
end | ||
|
||
# Returns true if the callback hook has been enabled. | ||
def enabled?(hook) | ||
@callbacks.key? hook | ||
end | ||
|
||
# Defines a callback for a given hook. | ||
def set(hook, &block) | ||
raise NoBlockGiven, "a block is required when setting a callback" unless block_given? | ||
@callbacks[hook] = proc(&block) | ||
end | ||
|
||
# Returns true if a callback has been defined for a given hook. | ||
def set?(hook) | ||
enabled?(hook) && @callbacks[hook].respond_to?(:call) | ||
end | ||
|
||
# Runs the callback defined for a given hook, with the arguments provided | ||
def run(hook, *args) | ||
raise NotEnabled unless enabled?(hook) | ||
return nil unless set?(hook) | ||
@callbacks[hook].call(*args) | ||
end | ||
end | ||
|
||
# Custom exceptions | ||
class NotEnabled < StandardError; end | ||
class NoBlockGiven < StandardError; 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
require 'curation_concerns/callbacks/registry' | ||
|
||
describe CurationConcerns::Callbacks::Registry do | ||
subject { described_class.new } | ||
|
||
describe "#enabled?" do | ||
it "returns false if the specified callback has not been enabled" do | ||
expect(subject.enabled?(:foo)) | ||
end | ||
|
||
it "returns true after enabling the specified callback" do | ||
subject.enable(:foo) | ||
expect(subject.enabled?(:foo)).to eq true | ||
end | ||
end | ||
|
||
describe "#set?" do | ||
it "returns false if the callback has not been set" do | ||
expect(subject.set?(:foo)).to eq false | ||
end | ||
|
||
it "returns true if the callback has been set" do | ||
subject.set(:foo) { nil } | ||
expect(subject.set?(:foo)).to eq true | ||
end | ||
end | ||
|
||
describe "#set" do | ||
it "raises an error if a block is not given" do | ||
expect { subject.set(:foo) }.to raise_error CurationConcerns::Callbacks::NoBlockGiven | ||
end | ||
|
||
it "raises an error if given no arguments" do | ||
expect { subject.set }.to raise_error ArgumentError | ||
end | ||
end | ||
|
||
describe "#run" do | ||
it "raises a NotEnabled error if the callback has not been enabled" do | ||
expect { subject.run(:foo) }.to raise_error CurationConcerns::Callbacks::NotEnabled | ||
end | ||
|
||
it "runs the specified callback with parameters" do | ||
subject.set(:foo) { |x, y| x + y } | ||
expect(subject.run(:foo, 1, 2)).to eq 3 | ||
end | ||
|
||
it "runs the most recently set callback" do | ||
subject.set(:foo) { "first" } | ||
subject.set(:foo) { "second" } | ||
expect(subject.run(:foo)).to eq "second" | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
require 'curation_concerns/callbacks' | ||
|
||
describe CurationConcerns::Callbacks do | ||
context 'when included in a class,' do | ||
before do | ||
class TestClass | ||
include CurationConcerns::Callbacks | ||
end | ||
end | ||
|
||
after do | ||
Object.send(:remove_const, :TestClass) | ||
end | ||
|
||
describe '.callback' do | ||
it 'returns an instance of Callbacks::Registry' do | ||
expect(TestClass.callback).to be_a CurationConcerns::Callbacks::Registry | ||
end | ||
end | ||
|
||
describe '#callback' do | ||
it 'is an instance method shortcut to the class method of the same name' do | ||
expect(TestClass.new.callback).to eq TestClass.callback | ||
end | ||
end | ||
end | ||
end |