spank 0.0.0 → 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f3ad3f44fbf8bd01849891acc1cfde2bf1362cf8
4
- data.tar.gz: 5becabf3cc3fa7942f94992085a63b20022b4000
3
+ metadata.gz: c8b671e694e2f9a62cab98cfcf48ba66ec7eb2b8
4
+ data.tar.gz: 5b0e709269ed25ae1adc33190b27aa5a1ea0e7e7
5
5
  SHA512:
6
- metadata.gz: cae9beacbbeac865f02a72e6781bb61b9d4d21b75c104945cb5913b6832563027c1dc09e54e107bbc0a12321eee67ece062c6409bdf209ad752068cde63b0828
7
- data.tar.gz: e4b59eda7db01c0de19138c0acc44a5fd224ed4ad68c96c19ed8f519849ff72dee2f0cf0142da2edb023463966486aa43d18caece52d6c3ca6c8931a99eba7ee
6
+ metadata.gz: 59e85d5e83e8e493026c6c7407eb3d7df57841634cf3ce855052e71797d037582076e279551e3d9acf104323ef7217cc3bf3d4892194b155ccc3a335caa7278e
7
+ data.tar.gz: 98bd5e90dba0c47663a4b88a709040cbd8ed6023d2e03087d2e0c1fc445623c3806a2ae32e6e7fda55b5415dd0cdacb116dc95adca3db65410e563c3503499ea
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ *.gem
2
+ pkg/*
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ spank
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ ruby-2.0.0-p195
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source "https://rubygems.org"
2
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,17 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ spank (0.0.1)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ rake (10.0.3)
10
+
11
+ PLATFORMS
12
+ ruby
13
+
14
+ DEPENDENCIES
15
+ bundler (~> 1.3)
16
+ rake
17
+ spank!
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 mo khan
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
+ # Spank
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'spank'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install spank
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
@@ -0,0 +1,15 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ task :default => :spec
4
+
5
+ task :spec do
6
+ system 'rspec'
7
+ end
8
+
9
+ task :build do
10
+ system 'gem build spank.gemspec'
11
+ end
12
+
13
+ task :publish => :build do
14
+ system 'gem push *.gem'
15
+ end
data/lib/spank.rb CHANGED
@@ -0,0 +1,8 @@
1
+ require 'spank/lambda_behaviours.rb'
2
+ require 'spank/component.rb'
3
+ require 'spank/container.rb'
4
+ require 'spank/interceptor_registration.rb'
5
+ require 'spank/invocation.rb'
6
+ require 'spank/ioc.rb'
7
+ require 'spank/proxy.rb'
8
+ require "spank/version"
@@ -0,0 +1,28 @@
1
+ module Spank
2
+ class Component
3
+ include LambdaBehaviours
4
+
5
+ def initialize(class_key, &block)
6
+ @factory_method = block
7
+ @interceptors = []
8
+ end
9
+
10
+ def create(container)
11
+ instance = @factory_method.call(container)
12
+ @interceptors.each do |interceptor|
13
+ instance = interceptor.intercept(instance)
14
+ end
15
+ instance
16
+ end
17
+
18
+ def as_singleton
19
+ @factory_method = memoize(@factory_method)
20
+ end
21
+
22
+ def intercept(method)
23
+ interceptor = InterceptorRegistration.new(method)
24
+ @interceptors.push(interceptor)
25
+ interceptor
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,52 @@
1
+ module Spank
2
+ class ContainerError < Exception
3
+ def intialize(message)
4
+ @message = message
5
+ end
6
+ end
7
+ class Container
8
+ def initialize
9
+ @items = {}
10
+ register(:container) { self }
11
+ end
12
+ def register(key, &block)
13
+ component = Component.new(key, &block)
14
+ components_for(key).push(component)
15
+ component
16
+ end
17
+ def resolve(key)
18
+ instantiate(components_for(key).first, key)
19
+ end
20
+ def resolve_all(key)
21
+ components_for(key).map {|item| instantiate(item, key) }
22
+ end
23
+ def build(type)
24
+ try("I could not create: #{type}"){ build!(type) }
25
+ end
26
+ def build!(type)
27
+ constructor = type.instance_method('initialize')
28
+ parameters = constructor.parameters.map do |req, parameter|
29
+ resolve(parameter.to_sym)
30
+ end
31
+ type.send(:new, *parameters)
32
+ end
33
+
34
+ private
35
+
36
+ def components_for(key)
37
+ @items[key] = [] unless @items[key]
38
+ @items[key]
39
+ end
40
+ def instantiate(component, key)
41
+ raise ContainerError.new("#{key} is not a registered component.") unless component
42
+ component.create(self)
43
+ end
44
+ def try(error = nil, &lambda)
45
+ begin
46
+ lambda.call
47
+ rescue => e
48
+ raise Spank::ContainerError.new(error ||= "Oops: #{e}")
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,17 @@
1
+ module Spank
2
+ class InterceptorRegistration
3
+ def initialize(method_symbol)
4
+ @method = method_symbol
5
+ end
6
+
7
+ def with(interceptor)
8
+ @interceptor = interceptor
9
+ end
10
+
11
+ def intercept(instance)
12
+ proxy= Proxy.new(instance)
13
+ proxy.add_interceptor(@method, @interceptor)
14
+ proxy
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,20 @@
1
+ module Spank
2
+ class Invocation
3
+ attr_reader :instance, :method, :arguments, :result
4
+
5
+ def initialize(instance, method, args, block)
6
+ @instance = instance
7
+ @method = method
8
+ @arguments = args
9
+ @block = block
10
+ end
11
+
12
+ def proceed
13
+ if @block
14
+ @result = @instance.public_send(@method, @arguments, @block)
15
+ else
16
+ @result = @instance.public_send(@method, @arguments)
17
+ end
18
+ end
19
+ end
20
+ end
data/lib/spank/ioc.rb ADDED
@@ -0,0 +1,10 @@
1
+ module Spank
2
+ class IOC
3
+ def self.bind_to(container)
4
+ @@container = container
5
+ end
6
+ def self.resolve(symbol)
7
+ @@container.resolve(symbol)
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,7 @@
1
+ module Spank
2
+ module LambdaBehaviours
3
+ def memoize(lambda_method)
4
+ lambda { |container| @cache ||= lambda_method.call(container) }
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,36 @@
1
+ module Spank
2
+ class Proxy
3
+ def initialize(target)
4
+ @target = target
5
+ end
6
+
7
+ def add_interceptor(method, interceptor)
8
+ self.extend(create_module_for(method, interceptor))
9
+ self
10
+ end
11
+
12
+ private
13
+
14
+ def create_invocation_for(method, args, block)
15
+ Invocation.new(@target, method, args, block)
16
+ end
17
+
18
+ def method_missing(method, *args, &block)
19
+ if block
20
+ @target.public_send(method, *args, block)
21
+ else
22
+ @target.public_send(method, *args)
23
+ end
24
+ end
25
+
26
+ def create_module_for(method, interceptor)
27
+ Module.new do
28
+ define_method(method.to_sym) do |*args, &block|
29
+ invocation = create_invocation_for(method, args, block)
30
+ interceptor.intercept(invocation)
31
+ invocation.result
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,3 @@
1
+ module Spank
2
+ VERSION = "0.0.1"
3
+ end
data/spank.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'spank/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "spank"
8
+ spec.version = Spank::VERSION
9
+ spec.authors = ["mo khan"]
10
+ spec.email = ["mo@mokhan.ca"]
11
+ spec.description = %q{A simple ruby container}
12
+ spec.summary = %q{spank!}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ spec.add_development_dependency "rspec-fakes"
25
+ end
@@ -0,0 +1,2 @@
1
+ require 'fakes-rspec'
2
+ require 'spank'
@@ -0,0 +1,131 @@
1
+ require "spec_helper"
2
+
3
+ module Spank
4
+ describe Container do
5
+ let(:sut) { Container.new }
6
+
7
+ describe "when resolving an item that has been registered" do
8
+ let(:registered_item) { Object.new }
9
+ before :each do
10
+ sut.register(:item) do
11
+ registered_item
12
+ end
13
+ end
14
+ before :each do
15
+ @result = sut.resolve(:item)
16
+ end
17
+ it "should return the registered item" do
18
+ @result.should == registered_item
19
+ end
20
+ end
21
+ describe "when resolving the container" do
22
+ it "should return itself" do
23
+ sut.resolve(:container).should == sut
24
+ end
25
+ end
26
+ describe "when multiple items are registered with the same key" do
27
+ let(:jeans) { fake }
28
+ let(:dress_pants) { fake }
29
+ before :each do
30
+ sut.register(:pants) { jeans }
31
+ sut.register(:pants) { dress_pants }
32
+ end
33
+ context "when resolving a single item" do
34
+ before :each do
35
+ @result = sut.resolve(:pants)
36
+ end
37
+ it "should return the first one registered" do
38
+ @result.should == jeans
39
+ end
40
+ end
41
+ context "when resolving all items" do
42
+ before :each do
43
+ @results = sut.resolve_all(:pants)
44
+ end
45
+ it "should return them all" do
46
+ @results.should == [jeans, dress_pants]
47
+ end
48
+ end
49
+ context "when resolving all items for an unknown key" do
50
+ it "should return an empty array" do
51
+ sut.resolve_all(:shirts).should be_empty
52
+ end
53
+ end
54
+ end
55
+ context "when a component is registered as a singleton" do
56
+ before :each do
57
+ sut.register(:singleton) { fake }.as_singleton
58
+ end
59
+ it "should return the same instance of that component each time it is resolved" do
60
+ sut.resolve(:singleton).should == sut.resolve(:singleton)
61
+ end
62
+ end
63
+ context "when invoking the factory method" do
64
+ before :each do
65
+ sut.register(:item){ |item| @result = item }
66
+ sut.resolve(:item)
67
+ end
68
+ it "should pass the container through to the block" do
69
+ @result.should == sut
70
+ end
71
+ end
72
+ context "when automatically resolving dependencies" do
73
+ class Child
74
+ def initialize(mom,dad)
75
+ end
76
+ def greeting(message)
77
+ end
78
+ end
79
+
80
+ context "when the dependencies have been registered" do
81
+ let(:mom) { fake }
82
+ let(:dad) { fake }
83
+ before :each do
84
+ sut.register(:mom) { mom }
85
+ sut.register(:dad) { dad }
86
+ end
87
+ it "should be able to glue the pieces together automatically" do
88
+ sut.build(Child).should be_a_kind_of(Child)
89
+ end
90
+ end
91
+ context "when a component cannot automatically be constructed" do
92
+ it "should raise an error" do
93
+ expect { sut.build(Child) }.to raise_error(ContainerError)
94
+ end
95
+ end
96
+ end
97
+ context "when registering an interceptor" do
98
+ class TestInterceptor
99
+ attr_reader :called
100
+ def intercept(invocation)
101
+ @called = true
102
+ invocation.proceed
103
+ end
104
+ end
105
+ class TestCommand
106
+ attr_reader :called, :received
107
+ def run(input)
108
+ @called = true
109
+ @received = input
110
+ end
111
+ end
112
+ let(:command) { TestCommand.new }
113
+ let(:interceptor) { TestInterceptor.new }
114
+
115
+ before :each do
116
+ sut.register(:command) { command }.intercept(:run).with(interceptor)
117
+ sut.resolve(:command).run("hi")
118
+ end
119
+
120
+ it "should allow the interceptor to intercept calls to the target" do
121
+ interceptor.called.should be_true
122
+ end
123
+
124
+ it "should forward the args to the command" do
125
+ #command.should have_received(:run, 'hi')
126
+ command.called.should be_true
127
+ command.received.should == ['hi']
128
+ end
129
+ end
130
+ end
131
+ end
@@ -0,0 +1,46 @@
1
+ require "spec_helper"
2
+
3
+ module Spank
4
+ describe Proxy do
5
+ let(:sut) { Proxy.new(target) }
6
+ let(:target) { fake }
7
+
8
+ context "when invoking a method" do
9
+ before { sut.greet('blah') }
10
+
11
+ it "should send the message to the target" do
12
+ target.should have_received(:greet, 'blah')
13
+ end
14
+ end
15
+
16
+ context "when an interceptor is registered" do
17
+ context "when invoking a method" do
18
+ let(:interceptor) { fake }
19
+
20
+ before :each do
21
+ sut.add_interceptor(:greet, interceptor)
22
+ sut.greet("blah")
23
+ end
24
+ it "should allow the interceptor to intercept the call" do
25
+ interceptor.should have_received(:intercept)
26
+ end
27
+ end
28
+ context "when invoking a method with a block" do
29
+ it "should pass the block to the target" do
30
+ proxy = Proxy.new([])
31
+ expect do
32
+ proxy.each do |x|
33
+ raise StandardError
34
+ end
35
+ end.to raise_error
36
+ end
37
+ end
38
+ end
39
+
40
+ context "when invoking a method that is not defined on the target" do
41
+ it "should raise an error" do
42
+ expect { Proxy.new("blah").goodbye }.to raise_error
43
+ end
44
+ end
45
+ end
46
+ end
metadata CHANGED
@@ -1,24 +1,102 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: spank
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0
4
+ version: 0.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - mo khan
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-05-17 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2013-05-18 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: rspec
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: rspec-fakes
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'
13
69
  description: A simple ruby container
14
- email: mo@mokhan.ca
70
+ email:
71
+ - mo@mokhan.ca
15
72
  executables: []
16
73
  extensions: []
17
74
  extra_rdoc_files: []
18
75
  files:
76
+ - .gitignore
77
+ - .ruby-gemset
78
+ - .ruby-version
79
+ - Gemfile
80
+ - Gemfile.lock
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
19
84
  - lib/spank.rb
20
- homepage: http://rubygems.org/gems/spank
21
- licenses: []
85
+ - lib/spank/component.rb
86
+ - lib/spank/container.rb
87
+ - lib/spank/interceptor_registration.rb
88
+ - lib/spank/invocation.rb
89
+ - lib/spank/ioc.rb
90
+ - lib/spank/lambda_behaviours.rb
91
+ - lib/spank/proxy.rb
92
+ - lib/spank/version.rb
93
+ - spank.gemspec
94
+ - spec/spec_helper.rb
95
+ - spec/unit/container_spec.rb
96
+ - spec/unit/proxy_spec.rb
97
+ homepage: ''
98
+ licenses:
99
+ - MIT
22
100
  metadata: {}
23
101
  post_install_message:
24
102
  rdoc_options: []
@@ -40,4 +118,7 @@ rubygems_version: 2.0.3
40
118
  signing_key:
41
119
  specification_version: 4
42
120
  summary: spank!
43
- test_files: []
121
+ test_files:
122
+ - spec/spec_helper.rb
123
+ - spec/unit/container_spec.rb
124
+ - spec/unit/proxy_spec.rb