bottle 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +18 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +29 -0
- data/Rakefile +10 -0
- data/bottle.gemspec +17 -0
- data/lib/bottle.rb +8 -0
- data/lib/bottle/class.rb +8 -0
- data/lib/bottle/instance_modifier.rb +33 -0
- data/lib/bottle/ioc.rb +39 -0
- data/lib/bottle/registration.rb +33 -0
- data/lib/bottle/registry.rb +13 -0
- data/lib/bottle/version.rb +3 -0
- data/spec/basic_ioc.rb +56 -0
- data/spec/inject_exceptions.rb +30 -0
- data/spec/instance_injection.rb +29 -0
- data/spec/spec_helper.rb +7 -0
- metadata +87 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Dusty Candland
|
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
|
+
# Bottle
|
2
|
+
|
3
|
+
Mostly an experiment with IoC in Ruby.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'bottle'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install bottle
|
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/bottle.gemspec
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/bottle/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Dusty Candland"]
|
6
|
+
gem.email = ["candland@gmail.com"]
|
7
|
+
gem.description = %q{IoC for ruby}
|
8
|
+
gem.summary = %q{IoC for ruby}
|
9
|
+
gem.homepage = ""
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "bottle"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = Bottle::VERSION
|
17
|
+
end
|
data/lib/bottle.rb
ADDED
data/lib/bottle/class.rb
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
module Bottle
|
2
|
+
class InstanceModifier
|
3
|
+
attr_accessor :klass
|
4
|
+
|
5
|
+
def initialize klass
|
6
|
+
@klass = klass
|
7
|
+
end
|
8
|
+
|
9
|
+
def inject_or_assign_accessor name
|
10
|
+
assert_no_method_conflict name
|
11
|
+
|
12
|
+
iname = "@#{name}".to_sym
|
13
|
+
|
14
|
+
klass.class_eval do
|
15
|
+
define_method(name) do
|
16
|
+
if instance_variable_defined? iname
|
17
|
+
else
|
18
|
+
instance_variable_set(iname, ::Bottle::Ioc.instance.resolve(name))
|
19
|
+
end
|
20
|
+
instance_variable_get(iname)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def assert_no_method_conflict name
|
26
|
+
if klass.instance_methods.include?(name)
|
27
|
+
raise Bottle::MethodExistsError.new(
|
28
|
+
"Method '#{klass}##{name}' already exists, please alias inject :#{name}, alias: my_#{name}")
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
end
|
data/lib/bottle/ioc.rb
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'singleton'
|
2
|
+
require_relative 'registration'
|
3
|
+
require_relative 'registry'
|
4
|
+
require_relative 'instance_modifier'
|
5
|
+
|
6
|
+
module Bottle
|
7
|
+
class Ioc
|
8
|
+
include Singleton
|
9
|
+
|
10
|
+
def self.configure &block
|
11
|
+
Ioc.instance.tap &block
|
12
|
+
end
|
13
|
+
|
14
|
+
def registry
|
15
|
+
@registry ||= Registry.new
|
16
|
+
end
|
17
|
+
|
18
|
+
def register name, &block
|
19
|
+
if registry[name]
|
20
|
+
registry[name].block = block
|
21
|
+
else
|
22
|
+
registry[name.to_sym] = Registration.new(name, block)
|
23
|
+
end
|
24
|
+
self
|
25
|
+
end
|
26
|
+
|
27
|
+
def resolve name
|
28
|
+
registry.resolve(name)
|
29
|
+
end
|
30
|
+
|
31
|
+
def inject_to klass, as, options = {}
|
32
|
+
name = as.to_sym
|
33
|
+
registration = (registry[name] ||= Registration.new(name))
|
34
|
+
registration.inject_to(klass, name, options)
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module Bottle
|
2
|
+
class Registration
|
3
|
+
attr_accessor :name, :block
|
4
|
+
|
5
|
+
def initialize name, block = nil, options = {}
|
6
|
+
@name = name
|
7
|
+
@options = options
|
8
|
+
@block = block
|
9
|
+
end
|
10
|
+
|
11
|
+
def injected_to klass
|
12
|
+
injected << klass
|
13
|
+
end
|
14
|
+
|
15
|
+
def injected_to? klass
|
16
|
+
injected.include?(klass)
|
17
|
+
end
|
18
|
+
|
19
|
+
def inject_to klass, name, options = {}
|
20
|
+
if not injected_to?(klass)
|
21
|
+
InstanceModifier.new(klass).inject_or_assign_accessor(name)
|
22
|
+
injected_to(klass)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def injected
|
29
|
+
@injected_to ||= []
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module Bottle
|
2
|
+
class Registry < Hash
|
3
|
+
|
4
|
+
def resolve name
|
5
|
+
if self[name.to_sym] and self[name.to_sym].block
|
6
|
+
return self[name.to_sym].block.call
|
7
|
+
else
|
8
|
+
raise RegistrationNotFoundError.new("Registration #{name} not found.")
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
end
|
13
|
+
end
|
data/spec/basic_ioc.rb
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
require_relative 'spec_helper'
|
2
|
+
|
3
|
+
|
4
|
+
describe Bottle, "basic IOC" do
|
5
|
+
Bottle::Ioc.configure do |c|
|
6
|
+
c.register :basic_ioc do
|
7
|
+
"my service x"
|
8
|
+
end
|
9
|
+
c.register(:logger) {Logger.new STDOUT}
|
10
|
+
end
|
11
|
+
|
12
|
+
class B
|
13
|
+
inject :basic_ioc
|
14
|
+
inject :logger
|
15
|
+
end
|
16
|
+
|
17
|
+
let (:b) {
|
18
|
+
B.new
|
19
|
+
}
|
20
|
+
|
21
|
+
describe "inject method into class" do
|
22
|
+
it "must respond to the inject method" do
|
23
|
+
b.must_respond_to :basic_ioc
|
24
|
+
end
|
25
|
+
|
26
|
+
it "must return my service x" do
|
27
|
+
b.basic_ioc.must_equal "my service x"
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
describe "inject new class into accessor" do
|
32
|
+
end
|
33
|
+
|
34
|
+
describe "register_directory" do
|
35
|
+
it "must look for classes in a directory" do
|
36
|
+
end
|
37
|
+
|
38
|
+
it "must use class in environment module" do
|
39
|
+
# / <- production components
|
40
|
+
# /test <- test components or anything matching environment.
|
41
|
+
# -component_x
|
42
|
+
# -compenent_x
|
43
|
+
#
|
44
|
+
# Given test environment use Test::ComponentX assume production.
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
describe "register_module" do
|
49
|
+
it "must register classes under the given module" do
|
50
|
+
end
|
51
|
+
|
52
|
+
it "must handle namespacing by using the first registered class found" do
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require_relative 'spec_helper'
|
2
|
+
|
3
|
+
describe Bottle, "injection exceptions" do
|
4
|
+
class Y
|
5
|
+
inject :inject_errors
|
6
|
+
|
7
|
+
def me
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
let(:y) {Y.new}
|
12
|
+
|
13
|
+
describe "service not found" do
|
14
|
+
it "must throw when not found on use" do
|
15
|
+
-> {
|
16
|
+
y.inject_errors
|
17
|
+
}.must_raise Bottle::RegistrationNotFoundError
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
describe "method exists" do
|
22
|
+
it "must raise method exists exception" do
|
23
|
+
-> {
|
24
|
+
class Y
|
25
|
+
inject :me
|
26
|
+
end
|
27
|
+
}.must_raise Bottle::MethodExistsError
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require_relative 'spec_helper'
|
2
|
+
|
3
|
+
describe Bottle, "instance injection" do
|
4
|
+
Bottle::Ioc.configure do |c|
|
5
|
+
c.register :instance_injection do
|
6
|
+
Hash.new
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
class X
|
11
|
+
inject :instance_injection
|
12
|
+
end
|
13
|
+
|
14
|
+
let(:x) {X.new}
|
15
|
+
|
16
|
+
describe "inject method into instance variable in class" do
|
17
|
+
it "must respond to instance_injection" do
|
18
|
+
x.must_respond_to :instance_injection
|
19
|
+
end
|
20
|
+
|
21
|
+
it "wont be same as class instance" do
|
22
|
+
x.instance_injection.wont_be_same_as X.new.instance_injection
|
23
|
+
end
|
24
|
+
|
25
|
+
it "wont respond to instance_injection on class" do
|
26
|
+
X.wont_respond_to :instance_injection
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: bottle
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Dusty Candland
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2012-07-14 00:00:00 -06:00
|
19
|
+
default_executable:
|
20
|
+
dependencies: []
|
21
|
+
|
22
|
+
description: IoC for ruby
|
23
|
+
email:
|
24
|
+
- candland@gmail.com
|
25
|
+
executables: []
|
26
|
+
|
27
|
+
extensions: []
|
28
|
+
|
29
|
+
extra_rdoc_files: []
|
30
|
+
|
31
|
+
files:
|
32
|
+
- .gitignore
|
33
|
+
- Gemfile
|
34
|
+
- LICENSE
|
35
|
+
- README.md
|
36
|
+
- Rakefile
|
37
|
+
- bottle.gemspec
|
38
|
+
- lib/bottle.rb
|
39
|
+
- lib/bottle/class.rb
|
40
|
+
- lib/bottle/instance_modifier.rb
|
41
|
+
- lib/bottle/ioc.rb
|
42
|
+
- lib/bottle/registration.rb
|
43
|
+
- lib/bottle/registry.rb
|
44
|
+
- lib/bottle/version.rb
|
45
|
+
- spec/basic_ioc.rb
|
46
|
+
- spec/inject_exceptions.rb
|
47
|
+
- spec/instance_injection.rb
|
48
|
+
- spec/spec_helper.rb
|
49
|
+
has_rdoc: true
|
50
|
+
homepage: ""
|
51
|
+
licenses: []
|
52
|
+
|
53
|
+
post_install_message:
|
54
|
+
rdoc_options: []
|
55
|
+
|
56
|
+
require_paths:
|
57
|
+
- lib
|
58
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
59
|
+
none: false
|
60
|
+
requirements:
|
61
|
+
- - ">="
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
hash: 3
|
64
|
+
segments:
|
65
|
+
- 0
|
66
|
+
version: "0"
|
67
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
68
|
+
none: false
|
69
|
+
requirements:
|
70
|
+
- - ">="
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
hash: 3
|
73
|
+
segments:
|
74
|
+
- 0
|
75
|
+
version: "0"
|
76
|
+
requirements: []
|
77
|
+
|
78
|
+
rubyforge_project:
|
79
|
+
rubygems_version: 1.5.3
|
80
|
+
signing_key:
|
81
|
+
specification_version: 3
|
82
|
+
summary: IoC for ruby
|
83
|
+
test_files:
|
84
|
+
- spec/basic_ioc.rb
|
85
|
+
- spec/inject_exceptions.rb
|
86
|
+
- spec/instance_injection.rb
|
87
|
+
- spec/spec_helper.rb
|