handler_registerable 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/handler_registerable.rb +10 -0
- data/lib/handler_registerable/exceptions.rb +6 -0
- data/lib/handler_registerable/exceptions/no_handler_accepted.rb +12 -0
- data/lib/handler_registerable/registry.rb +52 -0
- data/lib/handler_registerable/version.rb +4 -0
- data/spec/exceptions_spec.rb +13 -0
- data/spec/registry_spec.rb +90 -0
- data/spec/spec_helper.rb +103 -0
- metadata +181 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 40814232bc83eb10f4be63141b15f20985a51f41
|
4
|
+
data.tar.gz: 3f6c9f5905cd2e357d6a80609cd6e237fce24be2
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: dd6baaa5db10da81336a247eaf8625954234a0d21ad707b2fc30477c69c229dbf62cbc66e021a2ef395b798e564efee04421ea87984851ada6e52cfc02ad081f
|
7
|
+
data.tar.gz: c0eeb6fa7718816a7bb58e85ea98989c2983fb1d1467a4df3e3c387f0b8143262145f818613be2fcc92056566f80b2f1abf5bf242d947bbd8a56fc89e3710350
|
@@ -0,0 +1,10 @@
|
|
1
|
+
# Module which can be extended into a class/module to provide the following class methods:
|
2
|
+
#
|
3
|
+
# registered_handlers - returns a hash which can be modified
|
4
|
+
# obtain - loop sthrough all registered handlers and returns the first one that handles the
|
5
|
+
# given condition (tested by calling the handles? class method on the handler class)
|
6
|
+
module HandlerRegisterable
|
7
|
+
autoload :Exceptions, 'handler_registerable/exceptions'
|
8
|
+
autoload :Registry, 'handler_registerable/registry'
|
9
|
+
autoload :Version, 'handler_registerable/version'
|
10
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
module HandlerRegisterable
|
2
|
+
# Exceptions
|
3
|
+
module Exceptions
|
4
|
+
# Exception when no handler meets the conditions
|
5
|
+
class NoHandlerAccepted < StandardError
|
6
|
+
# Meaningful message when for this exception
|
7
|
+
def message
|
8
|
+
"No Handler Accepted"
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
# HandlerRegisterable Registry
|
2
|
+
module HandlerRegisterable
|
3
|
+
# Registry Concept
|
4
|
+
module Registry
|
5
|
+
# Enables setting the default handler to use.
|
6
|
+
def default=(value)
|
7
|
+
@default = value
|
8
|
+
end
|
9
|
+
|
10
|
+
# Get the default handler
|
11
|
+
def default
|
12
|
+
@default
|
13
|
+
end
|
14
|
+
|
15
|
+
# Register a new item in the store
|
16
|
+
def register(item, key)
|
17
|
+
registered_handlers[key] = item
|
18
|
+
end
|
19
|
+
|
20
|
+
# Return the registered handlers
|
21
|
+
def registered_handlers
|
22
|
+
@registered_handlers ||= {}
|
23
|
+
end
|
24
|
+
|
25
|
+
# @param [Object] conditions An argument to be passed to the handles? and initialize methods
|
26
|
+
# of the handler classes. Used to determine if they can deal with the request or not.
|
27
|
+
# @return [Object, nil] An instance of the first handler class that returns true to handles?
|
28
|
+
# when given the condition argument. The instance is initialized with the given condition.
|
29
|
+
def obtain(*conditions)
|
30
|
+
registered_handlers = self.registered_handlers
|
31
|
+
# Reverses the order of the handlers, this allows for ones defined in an application rather
|
32
|
+
# than an engine to come first (to allow for overriding a handle)
|
33
|
+
registered_handlers = Hash[registered_handlers.to_a.reverse] unless no_handlers_defined?
|
34
|
+
registered_handlers.each do |identifier, h|
|
35
|
+
return h.new(*conditions) if h.handles?(*conditions)
|
36
|
+
end
|
37
|
+
|
38
|
+
# If no handler is found and there is a default, use that instead
|
39
|
+
if default
|
40
|
+
default.new(*conditions)
|
41
|
+
else
|
42
|
+
raise HandlerRegisterable::Exceptions::NoHandlerAccepted
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
private
|
47
|
+
|
48
|
+
def no_handlers_defined?
|
49
|
+
self.registered_handlers.nil? || self.registered_handlers.empty?
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe HandlerRegisterable::Exceptions::NoHandlerAccepted do
|
4
|
+
subject { described_class.new }
|
5
|
+
|
6
|
+
it 'returns a custom message' do
|
7
|
+
expect(subject.message).to eq('No Handler Accepted')
|
8
|
+
end
|
9
|
+
|
10
|
+
it 'is an instance of StandardError' do
|
11
|
+
expect(subject).to be_a_kind_of(StandardError)
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,90 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe HandlerRegisterable::Registry do
|
4
|
+
class TestHandlerRegistry
|
5
|
+
extend HandlerRegisterable::Registry
|
6
|
+
end
|
7
|
+
|
8
|
+
subject { TestHandlerRegistry }
|
9
|
+
|
10
|
+
describe 'obtain' do
|
11
|
+
context 'when no handler is found for the given condition' do
|
12
|
+
it "raises an exception" do
|
13
|
+
expect { subject.obtain('unknown') }.to raise_error(HandlerRegisterable::Exceptions::NoHandlerAccepted)
|
14
|
+
end
|
15
|
+
|
16
|
+
it "is a standard error which can be caught" do
|
17
|
+
rescued = false
|
18
|
+
begin
|
19
|
+
subject.obtain('this handler does not exist')
|
20
|
+
rescue
|
21
|
+
rescued = true
|
22
|
+
end
|
23
|
+
|
24
|
+
expect(rescued).to be_truthy
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
context 'when no handler is found but a default has been set' do
|
29
|
+
let(:default_handler) do
|
30
|
+
Class.new do
|
31
|
+
def initialize(condition)
|
32
|
+
@condition = condition
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
around :each do |example|
|
38
|
+
original = TestHandlerRegistry.default
|
39
|
+
TestHandlerRegistry.default = default_handler
|
40
|
+
|
41
|
+
example.run
|
42
|
+
|
43
|
+
TestHandlerRegistry.default = original
|
44
|
+
end
|
45
|
+
|
46
|
+
it 'will call the default handler' do
|
47
|
+
expect(default_handler).to receive(:new)
|
48
|
+
subject.obtain('unknown')
|
49
|
+
end
|
50
|
+
|
51
|
+
it 'will not raise an exception' do
|
52
|
+
expect { subject.obtain('unknown') }.to_not raise_error
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
context 'when a handler is found for the given condition' do
|
58
|
+
let(:handler_one) do
|
59
|
+
Class.new do
|
60
|
+
def initialize(condition)
|
61
|
+
@condition = condition
|
62
|
+
end
|
63
|
+
|
64
|
+
def self.handles?(condition)
|
65
|
+
condition == :test
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
let(:handler_two) do
|
71
|
+
Class.new do
|
72
|
+
def initialize(condition)
|
73
|
+
@condition = condition
|
74
|
+
end
|
75
|
+
|
76
|
+
def self.handles?(condition)
|
77
|
+
condition == :other_test
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
it 'returns the correct handler' do
|
83
|
+
TestHandlerRegistry.register handler_one, :handler_one
|
84
|
+
TestHandlerRegistry.register handler_two, :handler_two
|
85
|
+
|
86
|
+
expect(TestHandlerRegistry.obtain(:test)).to be_an_instance_of(handler_one)
|
87
|
+
expect(TestHandlerRegistry.obtain(:other_test)).to be_an_instance_of(handler_two)
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,103 @@
|
|
1
|
+
require 'simplecov'
|
2
|
+
SimpleCov.start do
|
3
|
+
add_filter 'spec/'
|
4
|
+
end
|
5
|
+
|
6
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
7
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
8
|
+
# The generated `.rspec` file contains `--require spec_helper` which will cause
|
9
|
+
# this file to always be loaded, without a need to explicitly require it in any
|
10
|
+
# files.
|
11
|
+
#
|
12
|
+
# Given that it is always loaded, you are encouraged to keep this file as
|
13
|
+
# light-weight as possible. Requiring heavyweight dependencies from this file
|
14
|
+
# will add to the boot time of your test suite on EVERY test run, even for an
|
15
|
+
# individual file that may not need all of that loaded. Instead, consider making
|
16
|
+
# a separate helper file that requires the additional dependencies and performs
|
17
|
+
# the additional setup, and require it from the spec files that actually need
|
18
|
+
# it.
|
19
|
+
#
|
20
|
+
# The `.rspec` file also contains a few flags that are not defaults but that
|
21
|
+
# users commonly want.
|
22
|
+
#
|
23
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
24
|
+
RSpec.configure do |config|
|
25
|
+
config.requires = ['handler_registerable']
|
26
|
+
|
27
|
+
# rspec-expectations config goes here. You can use an alternate
|
28
|
+
# assertion/expectation library such as wrong or the stdlib/minitest
|
29
|
+
# assertions if you prefer.
|
30
|
+
config.expect_with :rspec do |expectations|
|
31
|
+
# This option will default to `true` in RSpec 4. It makes the `description`
|
32
|
+
# and `failure_message` of custom matchers include text for helper methods
|
33
|
+
# defined using `chain`, e.g.:
|
34
|
+
# be_bigger_than(2).and_smaller_than(4).description
|
35
|
+
# # => "be bigger than 2 and smaller than 4"
|
36
|
+
# ...rather than:
|
37
|
+
# # => "be bigger than 2"
|
38
|
+
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
|
39
|
+
end
|
40
|
+
|
41
|
+
# rspec-mocks config goes here. You can use an alternate test double
|
42
|
+
# library (such as bogus or mocha) by changing the `mock_with` option here.
|
43
|
+
config.mock_with :rspec do |mocks|
|
44
|
+
# Prevents you from mocking or stubbing a method that does not exist on
|
45
|
+
# a real object. This is generally recommended, and will default to
|
46
|
+
# `true` in RSpec 4.
|
47
|
+
mocks.verify_partial_doubles = true
|
48
|
+
end
|
49
|
+
|
50
|
+
# The settings below are suggested to provide a good initial experience
|
51
|
+
# with RSpec, but feel free to customize to your heart's content.
|
52
|
+
=begin
|
53
|
+
# These two settings work together to allow you to limit a spec run
|
54
|
+
# to individual examples or groups you care about by tagging them with
|
55
|
+
# `:focus` metadata. When nothing is tagged with `:focus`, all examples
|
56
|
+
# get run.
|
57
|
+
config.filter_run :focus
|
58
|
+
config.run_all_when_everything_filtered = true
|
59
|
+
|
60
|
+
# Allows RSpec to persist some state between runs in order to support
|
61
|
+
# the `--only-failures` and `--next-failure` CLI options. We recommend
|
62
|
+
# you configure your source control system to ignore this file.
|
63
|
+
config.example_status_persistence_file_path = "spec/examples.txt"
|
64
|
+
|
65
|
+
# Limits the available syntax to the non-monkey patched syntax that is
|
66
|
+
# recommended. For more details, see:
|
67
|
+
# - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/
|
68
|
+
# - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
|
69
|
+
# - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode
|
70
|
+
config.disable_monkey_patching!
|
71
|
+
|
72
|
+
# This setting enables warnings. It's recommended, but in some cases may
|
73
|
+
# be too noisy due to issues in dependencies.
|
74
|
+
config.warnings = true
|
75
|
+
|
76
|
+
# Many RSpec users commonly either run the entire suite or an individual
|
77
|
+
# file, and it's useful to allow more verbose output when running an
|
78
|
+
# individual spec file.
|
79
|
+
if config.files_to_run.one?
|
80
|
+
# Use the documentation formatter for detailed output,
|
81
|
+
# unless a formatter has already been configured
|
82
|
+
# (e.g. via a command-line flag).
|
83
|
+
config.default_formatter = 'doc'
|
84
|
+
end
|
85
|
+
|
86
|
+
# Print the 10 slowest examples and example groups at the
|
87
|
+
# end of the spec run, to help surface which specs are running
|
88
|
+
# particularly slow.
|
89
|
+
config.profile_examples = 10
|
90
|
+
|
91
|
+
# Run specs in random order to surface order dependencies. If you find an
|
92
|
+
# order dependency and want to debug it, you can fix the order by providing
|
93
|
+
# the seed, which is printed after each run.
|
94
|
+
# --seed 1234
|
95
|
+
config.order = :random
|
96
|
+
|
97
|
+
# Seed global randomization in this process using the `--seed` CLI option.
|
98
|
+
# Setting this allows you to use `--seed` to deterministically reproduce
|
99
|
+
# test failures related to randomization by passing the same `--seed` value
|
100
|
+
# as the one that triggered the failure.
|
101
|
+
Kernel.srand config.seed
|
102
|
+
=end
|
103
|
+
end
|
metadata
ADDED
@@ -0,0 +1,181 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: handler_registerable
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Sage One Development Team
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-03-20 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rspec
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: pry
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: fudge
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: simplecov
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: cane
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: flay
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: flog
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: yard
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - ">="
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
type: :development
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - ">="
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0'
|
139
|
+
description: A gem to provide handler registry functionality
|
140
|
+
email: support@sageone.com
|
141
|
+
executables: []
|
142
|
+
extensions: []
|
143
|
+
extra_rdoc_files: []
|
144
|
+
files:
|
145
|
+
- lib/handler_registerable.rb
|
146
|
+
- lib/handler_registerable/exceptions.rb
|
147
|
+
- lib/handler_registerable/exceptions/no_handler_accepted.rb
|
148
|
+
- lib/handler_registerable/registry.rb
|
149
|
+
- lib/handler_registerable/version.rb
|
150
|
+
- spec/exceptions_spec.rb
|
151
|
+
- spec/registry_spec.rb
|
152
|
+
- spec/spec_helper.rb
|
153
|
+
homepage: https://www.github.com/Sage/handler_registerable
|
154
|
+
licenses:
|
155
|
+
- Apache-2.0
|
156
|
+
metadata: {}
|
157
|
+
post_install_message:
|
158
|
+
rdoc_options: []
|
159
|
+
require_paths:
|
160
|
+
- lib
|
161
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
162
|
+
requirements:
|
163
|
+
- - ">="
|
164
|
+
- !ruby/object:Gem::Version
|
165
|
+
version: '0'
|
166
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
167
|
+
requirements:
|
168
|
+
- - ">="
|
169
|
+
- !ruby/object:Gem::Version
|
170
|
+
version: '0'
|
171
|
+
requirements: []
|
172
|
+
rubyforge_project:
|
173
|
+
rubygems_version: 2.6.12
|
174
|
+
signing_key:
|
175
|
+
specification_version: 4
|
176
|
+
summary: A gem to provide handler registry functionality
|
177
|
+
test_files:
|
178
|
+
- spec/spec_helper.rb
|
179
|
+
- spec/exceptions_spec.rb
|
180
|
+
- spec/registry_spec.rb
|
181
|
+
has_rdoc:
|