service_pack 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/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +12 -0
- data/lib/service_pack.rb +4 -0
- data/lib/service_pack/base.rb +10 -0
- data/lib/service_pack/core.rb +55 -0
- data/lib/service_pack/service_collection.rb +22 -0
- data/lib/service_pack/version.rb +3 -0
- data/service_pack.gemspec +23 -0
- data/spec/base_spec.rb +67 -0
- data/spec/spec_helper.rb +8 -0
- metadata +101 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: af9b887c7765bb01f3629221baa96b42ad48ed4c
|
4
|
+
data.tar.gz: b385cb5bda1e5fef8b96c6233a7c6d350fd6e723
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 323a1d9471f5ea144e9e8c2cd1a7d40310b6e59d95b6362c562e682c8aac7365dbb81f4874e39a2374e597378da0d975db7f24ca23931657e3b635f02ddfba90
|
7
|
+
data.tar.gz: ebcf22ac5a35a5b9501eaa44e3483d55ebc22b1b3fef3726d802941e82f02bc5758afc3252d338a069c04a4d4603f7d9a8a05f6f2a711ed5a478100b53fae174
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 TODO: Write your name
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# ServicePack
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'service_pack'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install service_pack
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/lib/service_pack.rb
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
require 'service_pack/service_collection'
|
2
|
+
|
3
|
+
module ServicePack
|
4
|
+
module Core
|
5
|
+
def self.included(cls)
|
6
|
+
cls.send :extend, ClassMethods
|
7
|
+
cls.send :include, InstanceMethods
|
8
|
+
end
|
9
|
+
|
10
|
+
module ClassMethods
|
11
|
+
def register(service_name, service_class)
|
12
|
+
services.add service_name, service_class
|
13
|
+
|
14
|
+
define_service_accessor service_name
|
15
|
+
end
|
16
|
+
|
17
|
+
def registered?(service_name)
|
18
|
+
services.added? service_name
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
def define_service_accessor(service_name)
|
23
|
+
define_method service_name do
|
24
|
+
call_service(service_name)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def services
|
29
|
+
@services ||= ServiceCollection.new
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
module InstanceMethods
|
34
|
+
def [](service_name)
|
35
|
+
call_service service_name
|
36
|
+
end
|
37
|
+
|
38
|
+
private
|
39
|
+
def services
|
40
|
+
self.class.send :services
|
41
|
+
end
|
42
|
+
|
43
|
+
def call_service(service_name)
|
44
|
+
cache service_name do
|
45
|
+
services.instanciate(self, service_name)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def cache(key, &block)
|
50
|
+
@cache ||= {}
|
51
|
+
@cache[key] ||= block.call
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module ServicePack
|
2
|
+
class ServiceCollection
|
3
|
+
def add(service_name, service_class)
|
4
|
+
services[service_name] = service_class
|
5
|
+
end
|
6
|
+
|
7
|
+
def instanciate(base, service_name)
|
8
|
+
raise NotImplementedError, "#{service_name.inspect} service" unless added?(service_name)
|
9
|
+
service_class = services[service_name]
|
10
|
+
service_class.new(base)
|
11
|
+
end
|
12
|
+
|
13
|
+
def added?(service_name)
|
14
|
+
services.key? service_name
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
def services
|
19
|
+
@services ||= {}
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'service_pack/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "service_pack"
|
8
|
+
spec.version = ServicePack::VERSION
|
9
|
+
spec.authors = ["Jakob Holderbaum"]
|
10
|
+
spec.email = ["jh@neopoly.de"]
|
11
|
+
spec.summary = %q{Simple base class to simplify service provider classes}
|
12
|
+
spec.homepage = ""
|
13
|
+
spec.license = "MIT"
|
14
|
+
|
15
|
+
spec.files = `git ls-files`.split($/)
|
16
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
17
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
21
|
+
spec.add_development_dependency "rake"
|
22
|
+
spec.add_development_dependency "minitest", "~> 5.0.7"
|
23
|
+
end
|
data/spec/base_spec.rb
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
class BaseSpec < Spec
|
4
|
+
|
5
|
+
let(:service_pack) { Class.new(Base) }
|
6
|
+
let(:services) { service_pack.new }
|
7
|
+
|
8
|
+
it 'provides predicate for checking registration' do
|
9
|
+
service_pack.register :test, Class.new
|
10
|
+
|
11
|
+
assert_equal true, service_pack.registered?(:test)
|
12
|
+
assert_equal false, service_pack.registered?(:other)
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'does not affect other derived classes' do
|
16
|
+
other_service_pack = Class.new(Base)
|
17
|
+
|
18
|
+
service_pack.register :test, Class.new
|
19
|
+
|
20
|
+
assert_equal false, other_service_pack.registered?(:test)
|
21
|
+
refute_respond_to other_service_pack.new, :test
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'passes the base instance to the instanciated service' do
|
25
|
+
service_pack.register :foo, Struct.new(:base)
|
26
|
+
|
27
|
+
assert_equal services, services.foo.base
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'raises an error if service is not registered' do
|
31
|
+
exception = assert_raises NotImplementedError do
|
32
|
+
services[:some_service]
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
it 'allows overwriting of a registered service' do
|
37
|
+
service_class = Struct.new(:arg)
|
38
|
+
other_service_class = Struct.new(:arg)
|
39
|
+
|
40
|
+
service_pack.register :abc, service_class
|
41
|
+
service_pack.register :abc, other_service_class
|
42
|
+
|
43
|
+
assert_kind_of other_service_class, services.abc
|
44
|
+
end
|
45
|
+
|
46
|
+
|
47
|
+
describe 'service access' do
|
48
|
+
let(:service_class) { Struct.new(:arg) }
|
49
|
+
before do
|
50
|
+
service_pack.register :abc, service_class
|
51
|
+
end
|
52
|
+
|
53
|
+
describe 'access to a service instance' do
|
54
|
+
it 'is possible via method' do
|
55
|
+
assert_kind_of service_class, services.abc
|
56
|
+
end
|
57
|
+
|
58
|
+
it 'is possible via index operator' do
|
59
|
+
assert_kind_of service_class, services[:abc]
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
it 'caches the service instance' do
|
64
|
+
assert_equal services.abc.object_id, services[:abc].object_id
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: service_pack
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jakob Holderbaum
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-10-07 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
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: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ~>
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 5.0.7
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 5.0.7
|
55
|
+
description:
|
56
|
+
email:
|
57
|
+
- jh@neopoly.de
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- .gitignore
|
63
|
+
- Gemfile
|
64
|
+
- LICENSE.txt
|
65
|
+
- README.md
|
66
|
+
- Rakefile
|
67
|
+
- lib/service_pack.rb
|
68
|
+
- lib/service_pack/base.rb
|
69
|
+
- lib/service_pack/core.rb
|
70
|
+
- lib/service_pack/service_collection.rb
|
71
|
+
- lib/service_pack/version.rb
|
72
|
+
- service_pack.gemspec
|
73
|
+
- spec/base_spec.rb
|
74
|
+
- spec/spec_helper.rb
|
75
|
+
homepage: ''
|
76
|
+
licenses:
|
77
|
+
- MIT
|
78
|
+
metadata: {}
|
79
|
+
post_install_message:
|
80
|
+
rdoc_options: []
|
81
|
+
require_paths:
|
82
|
+
- lib
|
83
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
84
|
+
requirements:
|
85
|
+
- - '>='
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '0'
|
88
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
89
|
+
requirements:
|
90
|
+
- - '>='
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: '0'
|
93
|
+
requirements: []
|
94
|
+
rubyforge_project:
|
95
|
+
rubygems_version: 2.0.0
|
96
|
+
signing_key:
|
97
|
+
specification_version: 4
|
98
|
+
summary: Simple base class to simplify service provider classes
|
99
|
+
test_files:
|
100
|
+
- spec/base_spec.rb
|
101
|
+
- spec/spec_helper.rb
|