acting 0.0.1 → 0.0.2

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: ef5b67668bb5848b366ab7b029fca9ea5a2c6d9b
4
- data.tar.gz: 5eb68911bb33de5f3ff5afaece87bad4413c437d
3
+ metadata.gz: 22283894c1dc87a10cdf0f6d806b11d07098bd3d
4
+ data.tar.gz: e95812f00bf97328e93afa6c3c5f46a993a5e5d0
5
5
  SHA512:
6
- metadata.gz: 7ea10a69df14c3ee6000b2c4c91f5c6c1d3feec6b89c90c4f2ce37a6d6dced813289b02d692d46a36508e68f034f6fa30ae6c2b328d825298fed687b5338f47a
7
- data.tar.gz: 15cc11a46691c3270cb9793798f3dbd7c44b360703df8f1a4195e8bd0dcdd5b7e9eadbb33e0586a82ebc5ab49f1093a0b661cea7058d3fd6f588bfb295f1576f
6
+ metadata.gz: fd619483fc20e9e961b3c7f4dc5759f03e07bdb41bd2a96dc57b5e3bf07923a647e2c6a1d9c730c8d72d4d4d5deab88d93dc1c6e129c2346f42c70ddcaa25db5
7
+ data.tar.gz: c4000ebd6ad3871a3009b594a17c1d6a12dcfcbb7d0a958375ae6ac5e51edda4c732f400d403ff878537c5d43b72e0e6bfaf2aaf60d212a5e8f91e5a2d2cbe29
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
4
+ - 1.9.3
5
+ - 1.9.2
6
+ # - jruby-19mode # JRuby in 1.9 mode
7
+ # - rbx-19mode
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- acting (0.0.1)
4
+ acting (0.0.2)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # Acting
2
2
 
3
+ [![Build Status](https://travis-ci.org/featurefabrik/acting.png?branch=master)](https://travis-ci.org/featurefabrik/acting)
4
+ [![Code Climate](https://codeclimate.com/github/featurefabrik/acting.png)](https://codeclimate.com/github/featurefabrik/acting)
5
+ [![Gem Version](https://badge.fury.io/rb/acting.png)](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
@@ -1,3 +1,3 @@
1
1
  class Acting
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
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
- @cast.play
9
+ if block_given?
10
+ play_with_block(&block)
11
+ else
12
+ @actors.each(&:play)
13
+ end
14
+ end
12
15
 
13
- block.call
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
- @cast.quit
26
+ quit
17
27
  end
18
28
 
19
- class Cast
20
- def initialize(actors)
21
- @actors = actors
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 play
25
- @actors.each(&:play)
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 quit
29
- @actors.each(&:quit)
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
- if RUBY_VERSION =~ /^2/
55
- object = @role
56
- retrieval = :instance_method
57
- method_names = @role.instance_methods
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
- @role_methods = method_names.map do |name|
66
- object.send retrieval, name
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
- role_methods.each do |method|
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
- role_methods.each do |method|
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
- it 'lets the whole cast play for duration of a block' do
5
- Positivist = Module.new do
6
- def speak
7
- 'YES'
8
- end
4
+ Positivist = Module.new do
5
+ def speak
6
+ 'YES'
9
7
  end
8
+ end
10
9
 
11
- Negativist = Module.new do
12
- def speak
13
- 'NO'
14
- end
10
+ Negativist = Module.new do
11
+ def speak
12
+ 'NO'
15
13
  end
14
+ end
16
15
 
17
- model1 = Object.new
18
- model2 = Object.new
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', model2.speak
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
@@ -2,3 +2,5 @@ require 'minitest/spec'
2
2
  require 'minitest/autorun'
3
3
 
4
4
  require 'acting'
5
+
6
+ puts "Running on #{RUBY_VERSION.inspect}"
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.1
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