activecontext 0.1 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/activecontext.rb +25 -6
- data/lib/activecontext/container.rb +10 -4
- data/lib/activecontext/context.rb +22 -0
- data/lib/activecontext/rack.rb +1 -1
- data/spec/activecontext_spec.rb +18 -0
- data/spec/container_spec.rb +4 -5
- metadata +5 -4
data/lib/activecontext.rb
CHANGED
@@ -8,19 +8,38 @@ module ActiveContext
|
|
8
8
|
|
9
9
|
def inject(*names)
|
10
10
|
names.each do |name|
|
11
|
-
|
12
|
-
|
13
|
-
|
11
|
+
if name.is_a?(Hash)
|
12
|
+
name.each_pair {|key, val| create_inject_method(name, val)}
|
13
|
+
else
|
14
|
+
create_inject_method(name, name)
|
14
15
|
end
|
15
16
|
end
|
16
17
|
end
|
17
18
|
|
18
19
|
def outject(*names)
|
19
20
|
names.each do |name|
|
20
|
-
|
21
|
-
|
22
|
-
|
21
|
+
if name.is_a?(Hash)
|
22
|
+
name.each_pair {|key, val| create_outject_method(name, val)}
|
23
|
+
else
|
24
|
+
create_outject_method(name, name)
|
23
25
|
end
|
24
26
|
end
|
25
27
|
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
def create_outject_method(name, as)
|
32
|
+
raise "invalid outject name #{as}" if methods.include?(:"#{as}=")
|
33
|
+
define_method "#{as}=" do |*args|
|
34
|
+
Container.current[name] = args.first
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def create_inject_method(name, as)
|
39
|
+
raise "invalid inject name #{as}" if methods.include?(as)
|
40
|
+
define_method as do
|
41
|
+
(value = Container.current[name]) && value.is_a?(Proc) ? value.call : value
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
26
45
|
end
|
@@ -1,12 +1,17 @@
|
|
1
|
+
require "activecontext/context"
|
2
|
+
|
1
3
|
module ActiveContext
|
2
4
|
class Container
|
3
5
|
def initialize
|
4
6
|
@names = {}
|
7
|
+
@contexts = {}
|
5
8
|
end
|
6
9
|
|
7
|
-
def register(name,
|
10
|
+
def register(name, storage, &block)
|
8
11
|
raise "invalid context name" if methods.include?(name)
|
9
|
-
|
12
|
+
raise "invalid storage" unless storage.respond_to?(:"[]") && storage.respond_to?(:"[]=")
|
13
|
+
|
14
|
+
@contexts[name] = Context.new(storage)
|
10
15
|
|
11
16
|
eigen = class << self; self; end
|
12
17
|
eigen.send(:define_method, name) do |*args, &block2|
|
@@ -49,13 +54,14 @@ module ActiveContext
|
|
49
54
|
Thread.current[:contextualize] = nil
|
50
55
|
end unless c
|
51
56
|
end
|
52
|
-
|
57
|
+
|
53
58
|
def self.current
|
54
59
|
Thread.current[:contextualize]
|
55
60
|
end
|
56
61
|
|
57
62
|
def context(name)
|
58
|
-
|
63
|
+
yield(@contexts[name]) if block_given?
|
64
|
+
@contexts[name]
|
59
65
|
end
|
60
66
|
|
61
67
|
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module ActiveContext
|
2
|
+
class Context
|
3
|
+
attr_accessor :serialize, :deserialize
|
4
|
+
def initialize(storage)
|
5
|
+
@storage = storage
|
6
|
+
@values = {}
|
7
|
+
@serialize = ->(key, value){ value }
|
8
|
+
@deserialize = ->(key, value){ value }
|
9
|
+
end
|
10
|
+
def [](key)
|
11
|
+
if @values.key?(key)
|
12
|
+
@values[key]
|
13
|
+
else
|
14
|
+
@values[key] = @deserialize.call(key, @storage[key])
|
15
|
+
end
|
16
|
+
end
|
17
|
+
def []=(key, value)
|
18
|
+
@storage[key] = @serialize.call(key, value)
|
19
|
+
@values[key] = value
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
data/lib/activecontext/rack.rb
CHANGED
data/spec/activecontext_spec.rb
CHANGED
@@ -26,6 +26,15 @@ describe ActiveContext do
|
|
26
26
|
o.should respond_to(:somename)
|
27
27
|
end
|
28
28
|
|
29
|
+
it "should create an aliased method for the injected name" do
|
30
|
+
o = new_test_object
|
31
|
+
o.eigenclass.instance_eval do
|
32
|
+
extend ActiveContext
|
33
|
+
inject :somename => :othername
|
34
|
+
end
|
35
|
+
o.should respond_to(:othername)
|
36
|
+
end
|
37
|
+
|
29
38
|
it "should create multiple methods when passing multiple injected names" do
|
30
39
|
o = new_test_object
|
31
40
|
o.eigenclass.instance_eval do
|
@@ -118,6 +127,15 @@ describe ActiveContext do
|
|
118
127
|
o.should respond_to(:"othername=")
|
119
128
|
end
|
120
129
|
|
130
|
+
it "should create an aliased method for the outjected name" do
|
131
|
+
o = new_test_object
|
132
|
+
o.eigenclass.instance_eval do
|
133
|
+
extend ActiveContext
|
134
|
+
outject :somename => :othername
|
135
|
+
end
|
136
|
+
o.should respond_to(:"othername=")
|
137
|
+
end
|
138
|
+
|
121
139
|
it "should fail if a method with the same name as outjected already exist" do
|
122
140
|
o = new_test_object
|
123
141
|
lambda do
|
data/spec/container_spec.rb
CHANGED
@@ -53,9 +53,8 @@ describe ActiveContext::Container do
|
|
53
53
|
describe "registering a context" do
|
54
54
|
it "should create an instance variable with same name as context" do
|
55
55
|
c = ActiveContext::Container.new
|
56
|
-
|
57
|
-
c.
|
58
|
-
c.context("request").should eq(ctx)
|
56
|
+
c.register :request, {}
|
57
|
+
c.context(:request).should_not be_nil
|
59
58
|
end
|
60
59
|
|
61
60
|
it "should create a method with same name as context to declare names in this context" do
|
@@ -92,7 +91,7 @@ describe ActiveContext::Container do
|
|
92
91
|
c = ActiveContext::Container.new
|
93
92
|
c.register :request, {}
|
94
93
|
c.request :somename, :joe
|
95
|
-
c.context(
|
94
|
+
c.context(:request)[:somename].should be(:joe)
|
96
95
|
end
|
97
96
|
|
98
97
|
it "should take a initial value factory as block" do
|
@@ -100,7 +99,7 @@ describe ActiveContext::Container do
|
|
100
99
|
c.register :request, {}
|
101
100
|
factory = -> { :joe }
|
102
101
|
c.request :somename, &factory
|
103
|
-
c.context(
|
102
|
+
c.context(:request)[:somename].should be(factory)
|
104
103
|
end
|
105
104
|
|
106
105
|
it "should fail when passing a initial value argument and a block at the same time" do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: activecontext
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 0.2.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-06-
|
12
|
+
date: 2012-06-06 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
16
|
-
requirement: &
|
16
|
+
requirement: &70180248337160 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,7 +21,7 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70180248337160
|
25
25
|
description: ''
|
26
26
|
email:
|
27
27
|
- jan.zimmek@web.de
|
@@ -30,6 +30,7 @@ extensions: []
|
|
30
30
|
extra_rdoc_files: []
|
31
31
|
files:
|
32
32
|
- lib/activecontext/container.rb
|
33
|
+
- lib/activecontext/context.rb
|
33
34
|
- lib/activecontext/rack.rb
|
34
35
|
- lib/activecontext.rb
|
35
36
|
- spec/activecontext_spec.rb
|