role_playing 0.0.3

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.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in role_playing.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 John Axel Eriksson
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,83 @@
1
+ # RolePlaying
2
+
3
+ A ruby DCI implementation using SimpleDelegator. This was extracted from a Rails app I'm working on. It's a very simple and straightforward implementation.
4
+
5
+ I'm well aware that this is not "true" DCI but I believe it to be in the spirit of DCI while avoiding the awfulness that is object.extend.
6
+
7
+ Using object.extend in Ruby has two severe problems, one that makes it not true DCI and another that makes it really really slow:
8
+
9
+ 1. There is no unextend
10
+ 2. It blows rubys method cache when used
11
+
12
+ A further comment on 2 is that it means EVERY time you call extend it blows Rubys ENTIRE method cache - it doesn't mean just the object you're extending, it means everything.
13
+
14
+ ## Installation
15
+
16
+ Add this line to your application's Gemfile:
17
+
18
+ gem 'role_playing'
19
+
20
+ And then execute:
21
+
22
+ $ bundle
23
+
24
+ Or install it yourself as:
25
+
26
+ $ gem install role_playing
27
+
28
+ ## Usage
29
+
30
+ Using it is as simple as defining (usually) a context like so:
31
+
32
+ class MoneyTransfer
33
+ def initialize(from_account, to_account)
34
+ @from_account = from_account
35
+ @to_account = to_account
36
+ end
37
+ def call(amount)
38
+ withdrawal = @from_account.in_role(SourceAccount).withdraw(amount)
39
+ @to_account.in_role(DestinationAccount).deposit(withdrawal)
40
+ end
41
+
42
+ class SourceAccount < RolePlaying::Role
43
+ def withdraw(amount)
44
+ self.amount=self.amount-amount
45
+ amount
46
+ end
47
+ end
48
+
49
+ class DestinationAccount < RolePlaying::Role
50
+ def deposit(amount)
51
+ self.amount=self.amount+amount
52
+ end
53
+ end
54
+
55
+ end
56
+
57
+ Please read the specs for a better understanding. Also please look up DCI (data, context, interaction) for a better understanding of what this is trying to accomplish.
58
+
59
+ ## Rspec
60
+
61
+ Theres an Rspec extension included which basically aliases Rspecs context to role so the language used in Rspec can be closer to DCI when testing these things.
62
+ To use that extension just do require 'role_playing/rspec_role' in your spec_helper. Look at the specs in this gem to see what I mean.
63
+
64
+ ## Links
65
+
66
+ http://dci-in-ruby.info
67
+
68
+ http://www.clean-ruby.com
69
+
70
+ http://tonyarcieri.com/dci-in-ruby-is-completely-broken - on why extend is bad
71
+
72
+ http://rubysource.com/dci-the-evolution-of-the-object-oriented-paradigm/
73
+
74
+ http://vimeo.com/8235394 - the inventor himself talks about DCI
75
+
76
+
77
+ ## Contributing
78
+
79
+ 1. Fork it
80
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
81
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
82
+ 4. Push to the branch (`git push origin my-new-feature`)
83
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,2 @@
1
+ require "role_playing/version"
2
+ require "role_playing/dci"
@@ -0,0 +1,33 @@
1
+ require 'delegate'
2
+
3
+ module RolePlaying
4
+ class Role < SimpleDelegator
5
+ def class
6
+ role_player.class ## this makes self.class return the extended objects class instead of DCIRole - should make the extension completely transparent
7
+ end
8
+
9
+ ## return the FINAL wrapped object
10
+ def role_player
11
+ player = self
12
+ while player.respond_to?(:__getobj__)
13
+ player = player.__getobj__
14
+ end
15
+ player
16
+ end
17
+
18
+ end
19
+ end
20
+
21
+ ## PLEASE NOTE: This is almost completely unnecessary except for some syntax sugar which I like.
22
+ ## It basically does this: TheRole.new(theObject) and returns it and also runs an instance_eval
23
+ ## if a block was given. It can also take many Roles, not just one so you could do theObject.as(SomeRole, AnotherRole).
24
+ class Object
25
+ def in_roles(*roles, &block)
26
+ extended = roles.inject(self) { |extended, role| role.new(extended) }
27
+ extended.instance_eval(&block) if block_given?
28
+ extended
29
+ end
30
+ def in_role(role, &block)
31
+ in_roles(*role, &block)
32
+ end
33
+ end
@@ -0,0 +1,13 @@
1
+ module RolePlaying
2
+ module RspecRole
3
+ def self.included(base)
4
+ base.instance_eval do
5
+ alias :role :context
6
+ end
7
+ end
8
+ end
9
+ end
10
+
11
+ RSpec.configure do |config|
12
+ config.include(RolePlaying::RspecRole)
13
+ end
@@ -0,0 +1,3 @@
1
+ module RolePlaying
2
+ VERSION = "0.0.3"
3
+ end
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'role_playing/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "role_playing"
8
+ gem.version = RolePlaying::VERSION
9
+ gem.authors = ["John Axel Eriksson"]
10
+ gem.email = ["john@insane.se"]
11
+ gem.description = %q{A ruby DCI implementation}
12
+ gem.summary = %q{A ruby DCI implementation}
13
+ gem.homepage = ""
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_development_dependency("rspec", ">= 2.12.0")
21
+ gem.add_development_dependency("bundler", "~> 1.2.0")
22
+ end
@@ -0,0 +1,128 @@
1
+ require 'spec_helper'
2
+
3
+ class DataObject
4
+ attr_accessor :field_1
5
+ attr_accessor :field_2
6
+ def initialize(field1, field2)
7
+ self.field_1=field1
8
+ self.field_2=field2
9
+ end
10
+ end
11
+
12
+ class Account
13
+ attr_accessor :amount
14
+ def initialize(amount)
15
+ self.amount = amount
16
+ end
17
+ end
18
+
19
+ class MyRole < RolePlaying::Role
20
+ def two_fields
21
+ "#{field_1} #{field_2}"
22
+ end
23
+ end
24
+
25
+ ## a context
26
+ class MoneyTransfer
27
+ def initialize(from_account, to_account)
28
+ @from_account = from_account
29
+ @to_account = to_account
30
+ end
31
+ def call(amount)
32
+ withdrawal = @from_account.in_role(SourceAccount).withdraw(amount)
33
+ @to_account.in_role(DestinationAccount).deposit(withdrawal)
34
+ end
35
+
36
+ class SourceAccount < RolePlaying::Role
37
+ def withdraw(amount)
38
+ self.amount=self.amount-amount
39
+ amount
40
+ end
41
+ end
42
+
43
+ class DestinationAccount < RolePlaying::Role
44
+ def deposit(amount)
45
+ self.amount=self.amount+amount
46
+ end
47
+ end
48
+
49
+ end
50
+
51
+ describe RolePlaying do
52
+ role MyRole do
53
+ let(:field_1) { 'Data' }
54
+ let(:field_2) { 'Object' }
55
+ let(:bare_object) { DataObject.new(field_1, field_2) }
56
+ subject { MyRole.new(bare_object) }
57
+
58
+ it "should be of same class as wrapped object" do
59
+ subject.class.should == bare_object.class
60
+ end
61
+
62
+ it "should be equal to wrapped object" do
63
+ subject.should == bare_object
64
+ end
65
+
66
+ it "should respond_to additional methods" do
67
+ subject.should respond_to(:two_fields)
68
+ end
69
+
70
+ it "#two_fields should concatenate data objects two fields" do
71
+ subject.two_fields.should == "#{bare_object.field_1} #{bare_object.field_2}"
72
+ end
73
+
74
+ it "#role_player should not respond_to additional methods" do
75
+ subject.role_player.should_not respond_to(:two_fields)
76
+ end
77
+
78
+ end
79
+
80
+ context MoneyTransfer do
81
+
82
+ role MoneyTransfer::SourceAccount do
83
+ let(:original_amount) { 50 }
84
+ let(:bare_account) {Account.new(original_amount)}
85
+ subject { MoneyTransfer::SourceAccount.new(bare_account) }
86
+ it "adds a withdraw method to data object" do
87
+ bare_account.should_not respond_to(:withdraw)
88
+ subject.should respond_to(:withdraw)
89
+ end
90
+ it "withdraws a specified amount" do
91
+ subject.withdraw(10)
92
+ subject.amount.should == original_amount-10
93
+ bare_account.amount.should == original_amount-10
94
+ end
95
+ end
96
+
97
+ role MoneyTransfer::DestinationAccount do
98
+ let(:original_amount) { 50 }
99
+ let(:bare_account) {Account.new(original_amount)}
100
+ subject { MoneyTransfer::DestinationAccount.new(bare_account) }
101
+ it "adds a deposit method to data object" do
102
+ bare_account.should_not respond_to(:deposit)
103
+ subject.should respond_to(:deposit)
104
+ end
105
+ it "deposits a specified amount" do
106
+ subject.deposit(10)
107
+ subject.amount.should == original_amount+10
108
+ bare_account.amount.should == original_amount+10
109
+ end
110
+ end
111
+
112
+ let(:original_source_account_amount) { 100 }
113
+ let(:original_destination_account_amount) { 40 }
114
+ let(:source_account) { Account.new(original_source_account_amount) }
115
+ let(:destination_account) { Account.new(original_destination_account_amount) }
116
+ let(:transfer_amount) { 50 }
117
+ subject { MoneyTransfer.new(source_account, destination_account) }
118
+
119
+ it "transfers a specified amount from a SourceAccount to a DestinationAccount" do
120
+ source_account.amount.should == original_source_account_amount
121
+ destination_account.amount.should == original_destination_account_amount
122
+ subject.call(transfer_amount)
123
+ source_account.amount.should == original_source_account_amount-transfer_amount
124
+ destination_account.amount.should == original_destination_account_amount+transfer_amount
125
+ end
126
+
127
+ end
128
+ end
@@ -0,0 +1,29 @@
1
+ # -*- encoding : utf-8 -*-
2
+
3
+ require 'bundler/setup'
4
+ require 'fileutils'
5
+
6
+ role_playing_path = File.expand_path('./lib', File.dirname(__FILE__))
7
+ $:.unshift(role_playing_path) if File.directory?(role_playing_path) && !$:.include?(role_playing_path)
8
+
9
+ require 'role_playing'
10
+ require 'role_playing/rspec_role'
11
+
12
+ RSpec.configure do |config|
13
+ # == Mock Framework
14
+ #
15
+ # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
16
+ #
17
+ # config.mock_with :mocha
18
+ # config.mock_with :flexmock
19
+ # config.mock_with :rr
20
+
21
+ config.mock_with :rspec
22
+
23
+ ## perhaps this should be removed as well
24
+ ## and done in Rakefile?
25
+ config.color_enabled = true
26
+ ## dont do this, do it in Rakefile instead
27
+ #config.formatter = 'd'
28
+
29
+ end
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: role_playing
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - John Axel Eriksson
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-02-07 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 2.12.0
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 2.12.0
30
+ - !ruby/object:Gem::Dependency
31
+ name: bundler
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: 1.2.0
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 1.2.0
46
+ description: A ruby DCI implementation
47
+ email:
48
+ - john@insane.se
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - Gemfile
55
+ - LICENSE.txt
56
+ - README.md
57
+ - Rakefile
58
+ - lib/role_playing.rb
59
+ - lib/role_playing/dci.rb
60
+ - lib/role_playing/rspec_role.rb
61
+ - lib/role_playing/version.rb
62
+ - role_playing.gemspec
63
+ - spec/role_playing/dci_spec.rb
64
+ - spec/spec_helper.rb
65
+ homepage: ''
66
+ licenses: []
67
+ post_install_message:
68
+ rdoc_options: []
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ none: false
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ requirements: []
84
+ rubyforge_project:
85
+ rubygems_version: 1.8.23
86
+ signing_key:
87
+ specification_version: 3
88
+ summary: A ruby DCI implementation
89
+ test_files:
90
+ - spec/role_playing/dci_spec.rb
91
+ - spec/spec_helper.rb
92
+ has_rdoc: