spank 0.0.2 → 0.0.1369197478

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: 304d66de564215dc7a9cbd7153f748de769a7615
4
- data.tar.gz: c7b0c3cf438d75a656b73b4f519c13de9a498d19
3
+ metadata.gz: fe2d3405d95996eb84b361f6f06ee14008b2ea60
4
+ data.tar.gz: 82bca104d9ed75db7169d0e19c9140cde00ef70e
5
5
  SHA512:
6
- metadata.gz: 9fe5f5ee9ee2702e28854e508f5ad76fefcafcf2bc0afd28a1aa4d55d4c128a1c4dd0f4e8309c30656745fc773bbb218bcd934729a39f1d31fde6dcb62ee81b9
7
- data.tar.gz: a490063b125cb26c2904575a9c74093277c61649c9e3472e82552d4731ff5db7354e406862a912aaa502c3bc05080f6879985bc31850b9226cc5339291b8fb8d
6
+ metadata.gz: 0c370a5b0478f3f819fd735b14aaf14fd6c6d6e4f24dcef5e50ea726a8dac6457f3f62039c7c5e2b5de8d377e0f23c7b144de4de38f4f9ddb7d418e2f76dc63e
7
+ data.tar.gz: 90f0199ece1db2a3f907acc0eb86fe196f427d3c301e0339325d0eedb163ee595dc3b8cab3bd2fc102e47aa51ffad143d19f941114d26028f5f5b079c8736a30
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- spank (0.0.2)
4
+ spank (0.0.1369197478)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
  A simple light weight inversion of control container written in ruby.
4
4
 
5
+ [![Build Status](https://travis-ci.org/mokhan/spank.png)](https://travis-ci.org/mokhan/spank)
6
+
5
7
  ## Installation
6
8
 
7
9
  Add this line to your application's Gemfile:
@@ -23,11 +25,9 @@ Register a single component and resolve it.
23
25
  ```ruby
24
26
 
25
27
  container = Container.new
26
- container.register(:item) do
28
+ container.register(:item) do |container|
27
29
  "ITEM"
28
30
  end
29
- container.register(:pants) { jeans }
30
- container.register(:pants) { dress_pants }
31
31
  item = container.resolve(:item)
32
32
 
33
33
  ```
@@ -39,7 +39,7 @@ Register multiple items, and resolve them.
39
39
  container = Container.new
40
40
  container.register(:pants) { jeans }
41
41
  container.register(:pants) { dress_pants }
42
- pants = container.resolve_all(:item)
42
+ pants = container.resolve_all(:pants)
43
43
 
44
44
  ```
45
45
 
@@ -98,7 +98,7 @@ Register selective interceptors.
98
98
 
99
99
  container = Container.new
100
100
  Spank::IOC.bind_to(container)
101
- Spank::IOC.resolve(:item)
101
+ item = Spank::IOC.resolve(:item)
102
102
 
103
103
  ```
104
104
 
@@ -4,25 +4,31 @@ module Spank
4
4
  @message = message
5
5
  end
6
6
  end
7
+
7
8
  class Container
8
9
  def initialize
9
10
  @items = {}
10
11
  register(:container) { self }
11
12
  end
13
+
12
14
  def register(key, &block)
13
15
  component = Component.new(key, &block)
14
16
  components_for(key).push(component)
15
17
  component
16
18
  end
19
+
17
20
  def resolve(key)
18
21
  instantiate(components_for(key).first, key)
19
22
  end
23
+
20
24
  def resolve_all(key)
21
25
  components_for(key).map {|item| instantiate(item, key) }
22
26
  end
27
+
23
28
  def build(type)
24
29
  try("I could not create: #{type}"){ build!(type) }
25
30
  end
31
+
26
32
  def build!(type)
27
33
  constructor = type.instance_method('initialize')
28
34
  parameters = constructor.parameters.map do |req, parameter|
@@ -37,10 +43,12 @@ module Spank
37
43
  @items[key] = [] unless @items[key]
38
44
  @items[key]
39
45
  end
46
+
40
47
  def instantiate(component, key)
41
48
  raise ContainerError.new("#{key} is not a registered component.") unless component
42
49
  component.create(self)
43
50
  end
51
+
44
52
  def try(error = nil, &lambda)
45
53
  begin
46
54
  lambda.call
@@ -0,0 +1,13 @@
1
+ class InterceptorChain
2
+ def initialize(interceptors = [])
3
+ @interceptors = interceptors
4
+ end
5
+
6
+ def push(interceptor)
7
+ @interceptors.push(interceptor)
8
+ end
9
+
10
+ def each(&block)
11
+ @interceptors.each(&block)
12
+ end
13
+ end
@@ -2,15 +2,23 @@ module Spank
2
2
  class InterceptorRegistration
3
3
  def initialize(method_symbol)
4
4
  @method = method_symbol
5
+ @interceptors = []
5
6
  end
6
7
 
7
8
  def with(interceptor)
8
- @interceptor = interceptor
9
+ @interceptors.push(interceptor)
10
+ self
11
+ end
12
+
13
+ def and(interceptor)
14
+ with(interceptor)
9
15
  end
10
16
 
11
17
  def intercept(instance)
12
- proxy= Proxy.new(instance)
13
- proxy.add_interceptor(@method, @interceptor)
18
+ proxy = Proxy.new(instance)
19
+ @interceptors.each do |interceptor|
20
+ proxy.add_interceptor(@method, interceptor)
21
+ end
14
22
  proxy
15
23
  end
16
24
  end
data/lib/spank/proxy.rb CHANGED
@@ -1,11 +1,13 @@
1
1
  module Spank
2
2
  class Proxy
3
- def initialize(target)
3
+ def initialize(target, interceptor_chain = InterceptorChain.new)
4
4
  @target = target
5
+ @interceptor_chain = interceptor_chain
5
6
  end
6
7
 
7
8
  def add_interceptor(method, interceptor)
8
- self.extend(create_module_for(method, interceptor))
9
+ @interceptor_chain.push(interceptor)
10
+ self.extend(create_module_for(method))
9
11
  self
10
12
  end
11
13
 
@@ -23,11 +25,13 @@ module Spank
23
25
  end
24
26
  end
25
27
 
26
- def create_module_for(method, interceptor)
28
+ def create_module_for(method)
27
29
  Module.new do
28
30
  define_method(method.to_sym) do |*args, &block|
29
31
  invocation = create_invocation_for(method, args, block)
30
- interceptor.intercept(invocation)
32
+ @interceptor_chain.each do |interceptor|
33
+ interceptor.intercept(invocation)
34
+ end
31
35
  invocation.result
32
36
  end
33
37
  end
data/lib/spank/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Spank
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.#{Time.now.to_i}"
3
3
  end
data/lib/spank.rb CHANGED
@@ -5,4 +5,5 @@ require 'spank/interceptor_registration.rb'
5
5
  require 'spank/invocation.rb'
6
6
  require 'spank/ioc.rb'
7
7
  require 'spank/proxy.rb'
8
- require "spank/version"
8
+ require 'spank/interceptor_chain'
9
+ require 'spank/version'
data/spank.gemspec CHANGED
@@ -17,7 +17,7 @@ Gem::Specification.new do |spec|
17
17
  spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ["lib"]
20
- spec.required_ruby_version = '>= 1.9.3'
20
+ spec.required_ruby_version = '>= 2.0.0'
21
21
 
22
22
  spec.add_development_dependency "bundler", "~> 1.3"
23
23
  spec.add_development_dependency "rake"
@@ -108,9 +108,13 @@ module Spank
108
108
  end
109
109
  end
110
110
 
111
- context "when registering an interceptor" do
111
+ context "when registering interceptors" do
112
112
  class TestInterceptor
113
- attr_reader :called
113
+ attr_reader :called, :name
114
+ def initialize(name)
115
+ @name = name
116
+ @called = false
117
+ end
114
118
  def intercept(invocation)
115
119
  @called = true
116
120
  invocation.proceed
@@ -126,17 +130,23 @@ module Spank
126
130
  end
127
131
 
128
132
  let(:command) { TestCommand.new }
129
- let(:interceptor) { TestInterceptor.new }
133
+ let(:interceptor) { TestInterceptor.new("first") }
134
+ let(:other_interceptor) { TestInterceptor.new("second") }
130
135
 
131
136
  before :each do
132
- sut.register(:command) { command }.intercept(:run).with(interceptor)
137
+ sut.register(:command) { command }.intercept(:run).with(interceptor).and(other_interceptor)
138
+ sut.register(:single_command) { command }.intercept(:run).with(interceptor)
133
139
  sut.resolve(:command).run("hi")
134
140
  end
135
141
 
136
- it "should allow the interceptor to intercept calls to the target" do
142
+ it "should allow the first interceptor to intercept calls to the target" do
137
143
  interceptor.called.should be_true
138
144
  end
139
145
 
146
+ it "should allow the second interceptor to intercept calls to the target" do
147
+ other_interceptor.called.should be_true
148
+ end
149
+
140
150
  it "should forward the args to the command" do
141
151
  command.called.should be_true
142
152
  command.received.should == ['hi']
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: spank
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.1369197478
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-18 00:00:00.000000000 Z
11
+ date: 2013-05-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -90,6 +90,7 @@ files:
90
90
  - .gitignore
91
91
  - .ruby-gemset
92
92
  - .ruby-version
93
+ - .travis.yml
93
94
  - Gemfile
94
95
  - Gemfile.lock
95
96
  - LICENSE.txt
@@ -98,6 +99,7 @@ files:
98
99
  - lib/spank.rb
99
100
  - lib/spank/component.rb
100
101
  - lib/spank/container.rb
102
+ - lib/spank/interceptor_chain.rb
101
103
  - lib/spank/interceptor_registration.rb
102
104
  - lib/spank/invocation.rb
103
105
  - lib/spank/ioc.rb
@@ -121,7 +123,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
121
123
  requirements:
122
124
  - - '>='
123
125
  - !ruby/object:Gem::Version
124
- version: 1.9.3
126
+ version: 2.0.0
125
127
  required_rubygems_version: !ruby/object:Gem::Requirement
126
128
  requirements:
127
129
  - - '>='