acting 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.travis.yml +7 -0
- data/Gemfile.lock +1 -1
- data/README.md +13 -0
- data/lib/acting/version.rb +1 -1
- data/lib/acting.rb +52 -35
- data/spec/acting_spec.rb +49 -12
- data/spec/helper.rb +2 -0
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 22283894c1dc87a10cdf0f6d806b11d07098bd3d
|
4
|
+
data.tar.gz: e95812f00bf97328e93afa6c3c5f46a993a5e5d0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fd619483fc20e9e961b3c7f4dc5759f03e07bdb41bd2a96dc57b5e3bf07923a647e2c6a1d9c730c8d72d4d4d5deab88d93dc1c6e129c2346f42c70ddcaa25db5
|
7
|
+
data.tar.gz: c4000ebd6ad3871a3009b594a17c1d6a12dcfcbb7d0a958375ae6ac5e51edda4c732f400d403ff878537c5d43b72e0e6bfaf2aaf60d212a5e8f91e5a2d2cbe29
|
data/.travis.yml
ADDED
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,5 +1,9 @@
|
|
1
1
|
# Acting
|
2
2
|
|
3
|
+
[](https://travis-ci.org/featurefabrik/acting)
|
4
|
+
[](https://codeclimate.com/github/featurefabrik/acting)
|
5
|
+
[](http://badge.fury.io/rb/acting)
|
6
|
+
|
3
7
|
Acting lets your objects play a role. But just for a specific duration.
|
4
8
|
One of the main-concepts for a clean DCI implementation.
|
5
9
|
|
@@ -37,6 +41,15 @@ It is as simple as this:
|
|
37
41
|
|
38
42
|
model1.speak # => NoMethodError
|
39
43
|
```
|
44
|
+
|
45
|
+
## TODO:
|
46
|
+
|
47
|
+
For now, we are only running on MRI 1.9.2/1.9.3/2.0.0.
|
48
|
+
jRuby and RBX need to be configured.
|
49
|
+
|
50
|
+
There is the case of overriding a previously define method by a role, that
|
51
|
+
needs to be fixed, too.
|
52
|
+
|
40
53
|
## Contributing
|
41
54
|
|
42
55
|
1. Fork it
|
data/lib/acting/version.rb
CHANGED
data/lib/acting.rb
CHANGED
@@ -1,37 +1,62 @@
|
|
1
1
|
class Acting
|
2
2
|
def initialize(cast)
|
3
|
-
actors = cast.map do |actor, role|
|
3
|
+
@actors = cast.map do |actor, role|
|
4
4
|
Actor.new(actor, role)
|
5
5
|
end
|
6
|
-
|
7
|
-
@cast = Cast.new(actors)
|
8
6
|
end
|
9
7
|
|
10
8
|
def play(&block)
|
11
|
-
|
9
|
+
if block_given?
|
10
|
+
play_with_block(&block)
|
11
|
+
else
|
12
|
+
@actors.each(&:play)
|
13
|
+
end
|
14
|
+
end
|
12
15
|
|
13
|
-
|
16
|
+
def quit
|
17
|
+
@actors.each(&:quit)
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
def play_with_block(&block)
|
22
|
+
@actors.each(&:play)
|
14
23
|
|
24
|
+
block.call
|
15
25
|
ensure
|
16
|
-
|
26
|
+
quit
|
17
27
|
end
|
18
28
|
|
19
|
-
|
20
|
-
|
21
|
-
|
29
|
+
module Cast
|
30
|
+
private
|
31
|
+
def __inject_methods__(methods)
|
32
|
+
methods.each do |method|
|
33
|
+
__injected_methods__[method.name] = method
|
34
|
+
end
|
22
35
|
end
|
23
36
|
|
24
|
-
def
|
25
|
-
|
37
|
+
def __remove_methods__(methods)
|
38
|
+
methods.each do |method|
|
39
|
+
__injected_methods__.delete method.name
|
40
|
+
end
|
26
41
|
end
|
27
42
|
|
28
|
-
def
|
29
|
-
@
|
43
|
+
def __injected_methods__
|
44
|
+
@__injected_methods__ ||= {}
|
45
|
+
end
|
46
|
+
|
47
|
+
def method_missing(meth, *args, &blk)
|
48
|
+
method = __injected_methods__[meth]
|
49
|
+
if method
|
50
|
+
method.bind(self).call(*args)
|
51
|
+
else
|
52
|
+
super
|
53
|
+
end
|
30
54
|
end
|
31
55
|
end
|
32
56
|
|
33
57
|
class Actor
|
34
58
|
def initialize(actor, role)
|
59
|
+
raise "#{actor.inspect} not kind of Acting::Cast" unless actor.kind_of? Cast
|
35
60
|
@actor = actor
|
36
61
|
@role = role
|
37
62
|
end
|
@@ -45,43 +70,35 @@ class Acting
|
|
45
70
|
end
|
46
71
|
|
47
72
|
private
|
48
|
-
def actor_singleton_class
|
49
|
-
@singleton_class ||= (class << @actor; self; end)
|
50
|
-
end
|
51
|
-
|
52
73
|
def role_methods
|
53
74
|
unless @role_methods
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
75
|
+
method_names = @role.instance_methods
|
76
|
+
|
77
|
+
if RUBY_VERSION =~ /2\.0/
|
78
|
+
@role_methods = method_names.map do |name|
|
79
|
+
@role.instance_method name
|
80
|
+
end
|
58
81
|
else
|
82
|
+
# interim object so we can bind the method
|
83
|
+
# willy nilly (no 'kind_of?(@role) == true' required)
|
59
84
|
object = Object.new
|
60
85
|
object.extend @role
|
61
|
-
retrieval = :method
|
62
|
-
method_names = object.methods - Object.methods
|
63
|
-
end
|
64
86
|
|
65
|
-
|
66
|
-
|
87
|
+
@role_methods = method_names.map do |name|
|
88
|
+
object.method(name).unbind
|
89
|
+
end
|
67
90
|
end
|
91
|
+
|
68
92
|
end
|
69
93
|
@role_methods
|
70
94
|
end
|
71
95
|
|
72
96
|
def assign_behaviour
|
73
|
-
|
74
|
-
actor_singleton_class.send :define_method, method.name, method
|
75
|
-
end
|
76
|
-
|
97
|
+
@actor.send :__inject_methods__, role_methods
|
77
98
|
end
|
78
99
|
|
79
100
|
def revoke_behaviour
|
80
|
-
|
81
|
-
if @actor.respond_to?(method.name)
|
82
|
-
actor_singleton_class.send :remove_method, method.name
|
83
|
-
end
|
84
|
-
end
|
101
|
+
@actor.send :__remove_methods__, role_methods
|
85
102
|
end
|
86
103
|
end
|
87
104
|
end
|
data/spec/acting_spec.rb
CHANGED
@@ -1,28 +1,65 @@
|
|
1
1
|
require 'helper'
|
2
2
|
|
3
3
|
describe Acting do
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
'YES'
|
8
|
-
end
|
4
|
+
Positivist = Module.new do
|
5
|
+
def speak
|
6
|
+
'YES'
|
9
7
|
end
|
8
|
+
end
|
10
9
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
end
|
10
|
+
Negativist = Module.new do
|
11
|
+
def speak
|
12
|
+
'NO'
|
15
13
|
end
|
14
|
+
end
|
16
15
|
|
17
|
-
|
18
|
-
|
16
|
+
it 'lets the whole cast play for duration of a block' do
|
17
|
+
model1 = new_model
|
18
|
+
model2 = new_model
|
19
19
|
|
20
20
|
Acting.new(model1 => Positivist, model2 => Negativist).play do
|
21
21
|
assert_equal 'YES', model1.speak
|
22
|
-
assert_equal 'NO',
|
22
|
+
assert_equal 'NO', model2.speak
|
23
23
|
end
|
24
24
|
|
25
25
|
refute model1.respond_to?(:speak)
|
26
26
|
refute model2.respond_to?(:speak)
|
27
27
|
end
|
28
|
+
|
29
|
+
it 'can be manually startet/stopped by omitting a block' do
|
30
|
+
model1 = new_model
|
31
|
+
model2 = new_model
|
32
|
+
|
33
|
+
acting = Acting.new(model1 => Positivist, model2 => Negativist)
|
34
|
+
|
35
|
+
refute model1.respond_to?(:speak)
|
36
|
+
refute model2.respond_to?(:speak)
|
37
|
+
|
38
|
+
acting.play
|
39
|
+
|
40
|
+
assert_equal 'YES', model1.speak
|
41
|
+
assert_equal 'NO', model2.speak
|
42
|
+
|
43
|
+
acting.quit
|
44
|
+
|
45
|
+
refute model1.respond_to?(:speak)
|
46
|
+
refute model2.respond_to?(:speak)
|
47
|
+
end
|
48
|
+
|
49
|
+
it 'fails when actor is no cast' do
|
50
|
+
model = Object.new
|
51
|
+
|
52
|
+
error = assert_raises RuntimeError do
|
53
|
+
Acting.new(model => Positivist)
|
54
|
+
end
|
55
|
+
|
56
|
+
assert_match(/kind of Acting::Cast/, error.message)
|
57
|
+
end
|
58
|
+
|
59
|
+
private
|
60
|
+
def new_model
|
61
|
+
model = Object.new
|
62
|
+
model.extend Acting::Cast
|
63
|
+
model
|
64
|
+
end
|
28
65
|
end
|
data/spec/helper.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: acting
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jakob Holderbaum
|
@@ -46,6 +46,7 @@ extensions: []
|
|
46
46
|
extra_rdoc_files: []
|
47
47
|
files:
|
48
48
|
- .gitignore
|
49
|
+
- .travis.yml
|
49
50
|
- Gemfile
|
50
51
|
- Gemfile.lock
|
51
52
|
- README.md
|