acting 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ef5b67668bb5848b366ab7b029fca9ea5a2c6d9b
4
+ data.tar.gz: 5eb68911bb33de5f3ff5afaece87bad4413c437d
5
+ SHA512:
6
+ metadata.gz: 7ea10a69df14c3ee6000b2c4c91f5c6c1d3feec6b89c90c4f2ce37a6d6dced813289b02d692d46a36508e68f034f6fa30ae6c2b328d825298fed687b5338f47a
7
+ data.tar.gz: 15cc11a46691c3270cb9793798f3dbd7c44b360703df8f1a4195e8bd0dcdd5b7e9eadbb33e0586a82ebc5ab49f1093a0b661cea7058d3fd6f588bfb295f1576f
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ .ruby-version
2
+ .ruby-gemset
3
+ pkg
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in acting.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,17 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ acting (0.0.1)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ rake (10.0.4)
10
+
11
+ PLATFORMS
12
+ ruby
13
+
14
+ DEPENDENCIES
15
+ acting!
16
+ bundler (~> 1.3)
17
+ rake
data/README.md ADDED
@@ -0,0 +1,46 @@
1
+ # Acting
2
+
3
+ Acting lets your objects play a role. But just for a specific duration.
4
+ One of the main-concepts for a clean DCI implementation.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ gem 'acting'
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install acting
19
+
20
+ ## Usage
21
+
22
+ It is as simple as this:
23
+
24
+ ```ruby
25
+ module Positivist
26
+ def speak; 'YES'; end
27
+ end
28
+
29
+ module Negativist
30
+ def speak; 'NO'; end
31
+ end
32
+
33
+ Acting.new(user1 => Positivist, user2 => Negativist).play do
34
+ model1.speak # => 'YES'
35
+ model2.speak # => 'NO'
36
+ end
37
+
38
+ model1.speak # => NoMethodError
39
+ ```
40
+ ## Contributing
41
+
42
+ 1. Fork it
43
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
44
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
45
+ 4. Push to the branch (`git push origin my-new-feature`)
46
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,14 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:spec) do |t|
5
+ t.libs << 'lib'
6
+ t.libs << 'spec'
7
+ t.pattern = 'spec/**/*_spec.rb'
8
+ t.verbose = false
9
+ end
10
+
11
+ # alias ofr convenience
12
+ task :test => :spec
13
+
14
+ task :default => :spec
data/acting.gemspec ADDED
@@ -0,0 +1,21 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'acting/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "acting"
8
+ spec.version = Acting::VERSION
9
+ spec.authors = ["Jakob Holderbaum"]
10
+ spec.email = ["jakob@featurefabrik.de"]
11
+ spec.summary = %q{Let your objects play an acting - just for a while}
12
+ spec.homepage = ""
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files`.split($/)
16
+ spec.test_files = spec.files.grep(%r{^(spec)/})
17
+ spec.require_paths = ["lib"]
18
+
19
+ spec.add_development_dependency "bundler", "~> 1.3"
20
+ spec.add_development_dependency "rake"
21
+ end
data/lib/acting.rb ADDED
@@ -0,0 +1,87 @@
1
+ class Acting
2
+ def initialize(cast)
3
+ actors = cast.map do |actor, role|
4
+ Actor.new(actor, role)
5
+ end
6
+
7
+ @cast = Cast.new(actors)
8
+ end
9
+
10
+ def play(&block)
11
+ @cast.play
12
+
13
+ block.call
14
+
15
+ ensure
16
+ @cast.quit
17
+ end
18
+
19
+ class Cast
20
+ def initialize(actors)
21
+ @actors = actors
22
+ end
23
+
24
+ def play
25
+ @actors.each(&:play)
26
+ end
27
+
28
+ def quit
29
+ @actors.each(&:quit)
30
+ end
31
+ end
32
+
33
+ class Actor
34
+ def initialize(actor, role)
35
+ @actor = actor
36
+ @role = role
37
+ end
38
+
39
+ def play
40
+ assign_behaviour
41
+ end
42
+
43
+ def quit
44
+ revoke_behaviour
45
+ end
46
+
47
+ private
48
+ def actor_singleton_class
49
+ @singleton_class ||= (class << @actor; self; end)
50
+ end
51
+
52
+ def role_methods
53
+ unless @role_methods
54
+ if RUBY_VERSION =~ /^2/
55
+ object = @role
56
+ retrieval = :instance_method
57
+ method_names = @role.instance_methods
58
+ else
59
+ object = Object.new
60
+ object.extend @role
61
+ retrieval = :method
62
+ method_names = object.methods - Object.methods
63
+ end
64
+
65
+ @role_methods = method_names.map do |name|
66
+ object.send retrieval, name
67
+ end
68
+ end
69
+ @role_methods
70
+ end
71
+
72
+ def assign_behaviour
73
+ role_methods.each do |method|
74
+ actor_singleton_class.send :define_method, method.name, method
75
+ end
76
+
77
+ end
78
+
79
+ 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
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,3 @@
1
+ class Acting
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,28 @@
1
+ require 'helper'
2
+
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
9
+ end
10
+
11
+ Negativist = Module.new do
12
+ def speak
13
+ 'NO'
14
+ end
15
+ end
16
+
17
+ model1 = Object.new
18
+ model2 = Object.new
19
+
20
+ Acting.new(model1 => Positivist, model2 => Negativist).play do
21
+ assert_equal 'YES', model1.speak
22
+ assert_equal 'NO', model2.speak
23
+ end
24
+
25
+ refute model1.respond_to?(:speak)
26
+ refute model2.respond_to?(:speak)
27
+ end
28
+ end
data/spec/helper.rb ADDED
@@ -0,0 +1,4 @@
1
+ require 'minitest/spec'
2
+ require 'minitest/autorun'
3
+
4
+ require 'acting'
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: acting
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Jakob Holderbaum
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-07-05 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
+ description:
42
+ email:
43
+ - jakob@featurefabrik.de
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - .gitignore
49
+ - Gemfile
50
+ - Gemfile.lock
51
+ - README.md
52
+ - Rakefile
53
+ - acting.gemspec
54
+ - lib/acting.rb
55
+ - lib/acting/version.rb
56
+ - spec/acting_spec.rb
57
+ - spec/helper.rb
58
+ homepage: ''
59
+ licenses:
60
+ - MIT
61
+ metadata: {}
62
+ post_install_message:
63
+ rdoc_options: []
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - '>='
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ requirements: []
77
+ rubyforge_project:
78
+ rubygems_version: 2.0.3
79
+ signing_key:
80
+ specification_version: 4
81
+ summary: Let your objects play an acting - just for a while
82
+ test_files:
83
+ - spec/acting_spec.rb
84
+ - spec/helper.rb