wrapper_based 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/.travis.yml +5 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +107 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/examples/dijkstra/data.rb +223 -0
- data/examples/dijkstra.rb +68 -0
- data/examples/money_transfer.rb +38 -0
- data/examples/toy_shop.rb +53 -0
- data/lib/wrapper_based/cast.rb +27 -0
- data/lib/wrapper_based/context.rb +35 -0
- data/lib/wrapper_based/dci.rb +19 -0
- data/lib/wrapper_based/decorator.rb +13 -0
- data/lib/wrapper_based/version.rb +3 -0
- data/lib/wrapper_based.rb +17 -0
- data/wrapper_based.gemspec +27 -0
- metadata +105 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: faf4cdd4a94e4efb43510aebec002dbddaf36862
|
4
|
+
data.tar.gz: 99b700d370a49024855173211a64f0927720ed26
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ea3a2475aa844cc8f7d59bff32343d73652a6d3a47aaf6d045e5834a704b25a35a7db9a8f010436a092c3a4688d02fa29e51b769c42dce88945f9b30ff0be8b3
|
7
|
+
data.tar.gz: cda1f749d20f6af2f787a4c3c34d1a53f0cc5e6f225a48282b5736cda892d9b11a1e02498ffe8b0dc367ebce3dcc8f115bf2247ca74b9785d27c565f5c90a2ca
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 Ritchie Paul Buitre
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,107 @@
|
|
1
|
+
# WrapperBased
|
2
|
+
|
3
|
+
Wrapper Based DCI implementation in Ruby.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'wrapper_based'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install wrapper_based
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
require_relative 'dijkstra/data'
|
25
|
+
# See https://github.com/RichOrElse/wrapper-based/blob/master/test/dijkstra_test.rb
|
26
|
+
|
27
|
+
# Behaviors
|
28
|
+
|
29
|
+
module Map
|
30
|
+
def distance_between(a, b)
|
31
|
+
@distances[Edge.new(a, b)]
|
32
|
+
end
|
33
|
+
|
34
|
+
def distance_of(path)
|
35
|
+
Distance[within: self].of(path)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
module CurrentIntersection
|
40
|
+
def neighbors(nearest:)
|
41
|
+
east_neighbor = nearest.east_neighbor_of(self)
|
42
|
+
south_neighbor = nearest.south_neighbor_of(self)
|
43
|
+
[south_neighbor, east_neighbor].compact
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
module DestinationNode
|
48
|
+
def shortest_path(from:, within:)
|
49
|
+
return [self] if equal? from
|
50
|
+
Shortest[to: self, from: from, city: within].path
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
# Contexts
|
55
|
+
|
56
|
+
DCI = WrapperBased::DCI.new unless defined? DCI
|
57
|
+
|
58
|
+
class Distance < DCI::Context(:within)
|
59
|
+
within.as Map
|
60
|
+
|
61
|
+
def between(a, b)
|
62
|
+
within.distance_between(a, b)
|
63
|
+
end
|
64
|
+
|
65
|
+
def of(path)
|
66
|
+
path.reverse.each_cons(2).inject(0) { |total, pair| total + between(*pair) }
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
class Shortest < DCI::Context(:from, :to, :city)
|
71
|
+
from.as CurrentIntersection
|
72
|
+
to.as DestinationNode
|
73
|
+
city.as Map
|
74
|
+
|
75
|
+
def distance
|
76
|
+
city.distance_of path
|
77
|
+
end
|
78
|
+
|
79
|
+
def path
|
80
|
+
_shortest_path + [@from]
|
81
|
+
end
|
82
|
+
|
83
|
+
private
|
84
|
+
|
85
|
+
def _shortest_path
|
86
|
+
from.
|
87
|
+
neighbors(nearest: @city).
|
88
|
+
map { |neighbor| to.shortest_path from: neighbor, within: @city }.
|
89
|
+
min_by { |path| city.distance_of path }
|
90
|
+
end
|
91
|
+
end
|
92
|
+
```
|
93
|
+
[View more examples](https://github.com/RichOrElse/wrapper-based/tree/master/examples)
|
94
|
+
|
95
|
+
## Development
|
96
|
+
|
97
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
98
|
+
|
99
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
100
|
+
|
101
|
+
## Contributing
|
102
|
+
|
103
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/RichOrElse/wrapper-based.
|
104
|
+
|
105
|
+
## License
|
106
|
+
|
107
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "wrapper_based"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,223 @@
|
|
1
|
+
# Source: http://fulloo.info/Examples/RubyExamples/Dijkstra/DijkstraListing.html
|
2
|
+
|
3
|
+
def infinity
|
4
|
+
2**(0.size * 8 -2) -1
|
5
|
+
end
|
6
|
+
|
7
|
+
#
|
8
|
+
# Consider street corners on a Manhattan grid. We want to find the
|
9
|
+
# minimal path from the most northeast city to the most
|
10
|
+
# southeast city. Use Dijstra's algorithm
|
11
|
+
#
|
12
|
+
|
13
|
+
# Data classes
|
14
|
+
|
15
|
+
Edge = Struct.new :from, :to
|
16
|
+
|
17
|
+
class Node
|
18
|
+
attr_reader :name
|
19
|
+
|
20
|
+
def initialize(name)
|
21
|
+
@name = name
|
22
|
+
end
|
23
|
+
|
24
|
+
def eql?(another_node)
|
25
|
+
# Nodes are == equal if they have the same name. This is explicitly
|
26
|
+
# defined here to call out the importance of the difference between
|
27
|
+
# object equality and identity
|
28
|
+
name == another_node.name
|
29
|
+
end
|
30
|
+
|
31
|
+
def ==(another_node)
|
32
|
+
# Equality used in the Map algorithms is object identity
|
33
|
+
super
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
#
|
38
|
+
# --- Geometry is the interface to the data class that has all
|
39
|
+
# --- the information about the map. This is kind of silly in Ruby
|
40
|
+
#
|
41
|
+
|
42
|
+
# In the domain model we have a general model of streets and avenues. The notions of
|
43
|
+
# an east and south neighbor are not part of the domain model, but are germane to
|
44
|
+
# the Dijkstra problem. Though they evaluate to the same thing we use different
|
45
|
+
# names to reflect these two different (mental) models of Manhattan streets.
|
46
|
+
class ManhattanGeometry
|
47
|
+
def east_neighbor_of(a) end
|
48
|
+
def south_neighbor_of(a) end
|
49
|
+
def root; end
|
50
|
+
def destination; end
|
51
|
+
def nodes; @nodes end
|
52
|
+
end
|
53
|
+
|
54
|
+
class ManhattanGeometry1 < ManhattanGeometry
|
55
|
+
def initialize
|
56
|
+
super
|
57
|
+
|
58
|
+
@nodes, @distances = [], {}
|
59
|
+
|
60
|
+
names = %w(a b c d a b g h i)
|
61
|
+
|
62
|
+
3.times do |i|
|
63
|
+
3.times do |j|
|
64
|
+
@nodes << Node.new(names[(i*3)+j])
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
# Aliases to help set up the grid. Grid is of Manhattan form:
|
69
|
+
#
|
70
|
+
# a - 2 - b - 3 - c
|
71
|
+
# | | |
|
72
|
+
# 1 2 1
|
73
|
+
# | | |
|
74
|
+
# d - 1 - e - 1 - f
|
75
|
+
# | |
|
76
|
+
# 2 4
|
77
|
+
# | |
|
78
|
+
# g - 1 - h - 2 - i
|
79
|
+
#
|
80
|
+
|
81
|
+
%w(a b c d e f g h i).each_with_index do |name, index|
|
82
|
+
instance_variable_set :"@#{name}", @nodes[index]
|
83
|
+
end
|
84
|
+
|
85
|
+
9.times do |i|
|
86
|
+
9.times do |j|
|
87
|
+
@distances[Edge.new(@nodes[i], @nodes[j])] = infinity
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
@distances[Edge.new(@a, @b)] = 2
|
92
|
+
@distances[Edge.new(@b, @c)] = 3
|
93
|
+
@distances[Edge.new(@c, @f)] = 1
|
94
|
+
@distances[Edge.new(@f, @i)] = 4
|
95
|
+
@distances[Edge.new(@b, @e)] = 2
|
96
|
+
@distances[Edge.new(@e, @f)] = 1
|
97
|
+
@distances[Edge.new(@a, @d)] = 1
|
98
|
+
@distances[Edge.new(@d, @g)] = 2
|
99
|
+
@distances[Edge.new(@g, @h)] = 1
|
100
|
+
@distances[Edge.new(@h, @i)] = 2
|
101
|
+
@distances[Edge.new(@d, @e)] = 1
|
102
|
+
@distances.freeze
|
103
|
+
|
104
|
+
@next_down_the_street_from = {}
|
105
|
+
@next_down_the_street_from[@a] = @b
|
106
|
+
@next_down_the_street_from[@b] = @c
|
107
|
+
@next_down_the_street_from[@d] = @e
|
108
|
+
@next_down_the_street_from[@e] = @f
|
109
|
+
@next_down_the_street_from[@g] = @h
|
110
|
+
@next_down_the_street_from[@h] = @i
|
111
|
+
@next_down_the_street_from.freeze
|
112
|
+
|
113
|
+
@next_along_the_avenue_from = Hash.new
|
114
|
+
@next_along_the_avenue_from[@a] = @d
|
115
|
+
@next_along_the_avenue_from[@b] = @e
|
116
|
+
@next_along_the_avenue_from[@c] = @f
|
117
|
+
@next_along_the_avenue_from[@d] = @g
|
118
|
+
@next_along_the_avenue_from[@f] = @i
|
119
|
+
@next_along_the_avenue_from.freeze
|
120
|
+
end
|
121
|
+
|
122
|
+
def east_neighbor_of(a)
|
123
|
+
@next_down_the_street_from[a]
|
124
|
+
end
|
125
|
+
|
126
|
+
def south_neighbor_of(a)
|
127
|
+
@next_along_the_avenue_from[a]
|
128
|
+
end
|
129
|
+
|
130
|
+
def root; @a end
|
131
|
+
def destination; @i end
|
132
|
+
end
|
133
|
+
|
134
|
+
class ManhattanGeometry2 < ManhattanGeometry
|
135
|
+
def initialize
|
136
|
+
super
|
137
|
+
|
138
|
+
@nodes = %w(a b c d a b g h i j k).map { |name| Node.new name }
|
139
|
+
|
140
|
+
# Aliases to help set up the grid. Grid is of Manhattan form:
|
141
|
+
#
|
142
|
+
# a - 2 - b - 3 - c - 1 - j
|
143
|
+
# | | | |
|
144
|
+
# 1 2 1 |
|
145
|
+
# | | | |
|
146
|
+
# d - 1 - e - 1 - f 1
|
147
|
+
# | | |
|
148
|
+
# 2 4 |
|
149
|
+
# | | |
|
150
|
+
# g - 1 - h - 2 - i - 2 - k
|
151
|
+
|
152
|
+
@a = @nodes[0]
|
153
|
+
@b = @nodes[1]
|
154
|
+
@c = @nodes[2]
|
155
|
+
@d = @nodes[3]
|
156
|
+
@e = @nodes[4]
|
157
|
+
@f = @nodes[5]
|
158
|
+
@g = @nodes[6]
|
159
|
+
@h = @nodes[7]
|
160
|
+
@i = @nodes[8]
|
161
|
+
@j = @nodes[9]
|
162
|
+
@k = @nodes[10]
|
163
|
+
|
164
|
+
@distances = {}
|
165
|
+
@nodes.each do |i|
|
166
|
+
@nodes.each do |j|
|
167
|
+
@distances[Edge.new(i, j)] = infinity
|
168
|
+
end
|
169
|
+
end
|
170
|
+
|
171
|
+
@distances[Edge.new(@a, @b)] = 2
|
172
|
+
@distances[Edge.new(@b, @c)] = 3
|
173
|
+
@distances[Edge.new(@c, @f)] = 1
|
174
|
+
@distances[Edge.new(@f, @i)] = 4
|
175
|
+
@distances[Edge.new(@b, @e)] = 2
|
176
|
+
@distances[Edge.new(@e, @f)] = 1
|
177
|
+
@distances[Edge.new(@a, @d)] = 1
|
178
|
+
@distances[Edge.new(@d, @g)] = 2
|
179
|
+
@distances[Edge.new(@g, @h)] = 1
|
180
|
+
@distances[Edge.new(@h, @i)] = 2
|
181
|
+
@distances[Edge.new(@d, @e)] = 1
|
182
|
+
@distances[Edge.new(@c, @j)] = 1
|
183
|
+
@distances[Edge.new(@j, @k)] = 1
|
184
|
+
@distances[Edge.new(@i, @k)] = 2
|
185
|
+
@distances.freeze
|
186
|
+
|
187
|
+
@next_down_the_street_from = {}
|
188
|
+
@next_down_the_street_from[@a] = @b
|
189
|
+
@next_down_the_street_from[@b] = @c
|
190
|
+
@next_down_the_street_from[@c] = @j
|
191
|
+
@next_down_the_street_from[@d] = @e
|
192
|
+
@next_down_the_street_from[@e] = @f
|
193
|
+
@next_down_the_street_from[@g] = @h
|
194
|
+
@next_down_the_street_from[@h] = @i
|
195
|
+
@next_down_the_street_from[@i] = @k
|
196
|
+
@next_down_the_street_from.freeze
|
197
|
+
|
198
|
+
@next_along_the_avenue_from = {}
|
199
|
+
@next_along_the_avenue_from[@a] = @d
|
200
|
+
@next_along_the_avenue_from[@b] = @e
|
201
|
+
@next_along_the_avenue_from[@c] = @f
|
202
|
+
@next_along_the_avenue_from[@d] = @g
|
203
|
+
@next_along_the_avenue_from[@f] = @i
|
204
|
+
@next_along_the_avenue_from[@j] = @k
|
205
|
+
@next_along_the_avenue_from.freeze
|
206
|
+
end
|
207
|
+
|
208
|
+
def east_neighbor_of(a)
|
209
|
+
@next_down_the_street_from[a]
|
210
|
+
end
|
211
|
+
|
212
|
+
def south_neighbor_of(a)
|
213
|
+
@next_along_the_avenue_from[a]
|
214
|
+
end
|
215
|
+
|
216
|
+
def root
|
217
|
+
@a
|
218
|
+
end
|
219
|
+
|
220
|
+
def destination
|
221
|
+
@k
|
222
|
+
end
|
223
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
require_relative 'dijkstra/data'
|
2
|
+
# See https://github.com/RichOrElse/wrapper-based/blob/master/test/dijkstra_test.rb
|
3
|
+
|
4
|
+
# Behaviors
|
5
|
+
|
6
|
+
module Map
|
7
|
+
def distance_between(a, b)
|
8
|
+
@distances[Edge.new(a, b)]
|
9
|
+
end
|
10
|
+
|
11
|
+
def distance_of(path)
|
12
|
+
Distance[within: self].of(path)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
module CurrentIntersection
|
17
|
+
def neighbors(nearest:)
|
18
|
+
east_neighbor = nearest.east_neighbor_of(self)
|
19
|
+
south_neighbor = nearest.south_neighbor_of(self)
|
20
|
+
[south_neighbor, east_neighbor].compact
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
module DestinationNode
|
25
|
+
def shortest_path(from:, within:)
|
26
|
+
return [self] if equal? from
|
27
|
+
Shortest[to: self, from: from, city: within].path
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
# Contexts
|
32
|
+
|
33
|
+
DCI = WrapperBased::DCI.new unless defined? DCI
|
34
|
+
|
35
|
+
class Distance < DCI::Context(:within)
|
36
|
+
within.as Map
|
37
|
+
|
38
|
+
def between(a, b)
|
39
|
+
within.distance_between(a, b)
|
40
|
+
end
|
41
|
+
|
42
|
+
def of(path)
|
43
|
+
path.reverse.each_cons(2).inject(0) { |total, pair| total + between(*pair) }
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
class Shortest < DCI::Context(:from, :to, :city)
|
48
|
+
from.as CurrentIntersection
|
49
|
+
to.as DestinationNode
|
50
|
+
city.as Map
|
51
|
+
|
52
|
+
def distance
|
53
|
+
city.distance_of path
|
54
|
+
end
|
55
|
+
|
56
|
+
def path
|
57
|
+
_shortest_path + [@from]
|
58
|
+
end
|
59
|
+
|
60
|
+
private
|
61
|
+
|
62
|
+
def _shortest_path
|
63
|
+
from.
|
64
|
+
neighbors(nearest: @city).
|
65
|
+
map { |neighbor| to.shortest_path from: neighbor, within: @city }.
|
66
|
+
min_by { |path| city.distance_of path }
|
67
|
+
end
|
68
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
Log = method(:puts)
|
2
|
+
Account = Struct.new(:number, :balance)
|
3
|
+
NotEnoughFund = Class.new(StandardError)
|
4
|
+
|
5
|
+
module SourceAccount
|
6
|
+
def decrease_balance_by(amount)
|
7
|
+
raise NotEnoughFund, "Balance is below amount.", caller if balance < amount
|
8
|
+
self.balance -= amount
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
module DestinationAccount
|
13
|
+
def increase_balance_by(amount)
|
14
|
+
self.balance += amount
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
DCI = WrapperBased::DCI.new unless defined? DCI
|
19
|
+
|
20
|
+
class TransferMoney < DCI::Context(:from, :to)
|
21
|
+
from.as SourceAccount
|
22
|
+
to.as DestinationAccount
|
23
|
+
|
24
|
+
def withdraw(amount)
|
25
|
+
from.decrease_balance_by(amount)
|
26
|
+
Log["Withdraw", amount, from]
|
27
|
+
end
|
28
|
+
|
29
|
+
def deposit(amount)
|
30
|
+
to.increase_balance_by(amount)
|
31
|
+
Log["Deposit", amount, to]
|
32
|
+
end
|
33
|
+
|
34
|
+
def call(amount:)
|
35
|
+
withdraw(amount)
|
36
|
+
deposit(amount)
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require_relative '../lib/wrapper_based'
|
2
|
+
|
3
|
+
# Data
|
4
|
+
|
5
|
+
Purchase = Struct.new(:toy, :buyer)
|
6
|
+
Deliver = Struct.new(:toy, :recipient, :purchase, :status)
|
7
|
+
|
8
|
+
# Behaviors
|
9
|
+
|
10
|
+
module Buyer
|
11
|
+
def buy(toy)
|
12
|
+
Purchase.new toy, self
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
module Recipient
|
17
|
+
def receive(purchased)
|
18
|
+
Deliver.new purchased.toy, self, purchased, :pending
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
# Contexts
|
23
|
+
DCI = WrapperBased::DCI.new unless defined? DCI
|
24
|
+
|
25
|
+
class PurchaseToy < DCI::Context(:purchaser)
|
26
|
+
purchaser.as Buyer
|
27
|
+
purchaser.as Recipient
|
28
|
+
|
29
|
+
def call(toy)
|
30
|
+
purchased = purchaser.buy toy
|
31
|
+
purchaser.receive purchased
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
|
36
|
+
class GiftToy < DCI::Context(:gifter, :giftee)
|
37
|
+
gifter.as Buyer
|
38
|
+
giftee.as Recipient
|
39
|
+
|
40
|
+
def call(toy)
|
41
|
+
gift = gifter.buy toy
|
42
|
+
giftee.receive gift
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
# Interactions
|
47
|
+
|
48
|
+
finn_purchase_toy = PurchaseToy[purchaser: 'Finn']
|
49
|
+
finn_purchase_toy.call 'Rusty sword'
|
50
|
+
finn_purchase_toy.('Armor of Zeldron')
|
51
|
+
finn_purchase_toy['The Enchiridion']
|
52
|
+
|
53
|
+
['Card Wars', 'Ice Ninja Manual', 'Bacon'].each &GiftToy[gifter: 'Jake', giftee: 'Finn']
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'set'
|
2
|
+
|
3
|
+
module WrapperBased
|
4
|
+
class Cast
|
5
|
+
attr_reader :name, :dci
|
6
|
+
|
7
|
+
def initialize(name, dci)
|
8
|
+
@name = name.to_sym
|
9
|
+
@casting = Set.new
|
10
|
+
@dci = dci
|
11
|
+
end
|
12
|
+
|
13
|
+
def as(extention)
|
14
|
+
@casting << extention
|
15
|
+
self
|
16
|
+
end
|
17
|
+
|
18
|
+
def cast_type(type)
|
19
|
+
@dci.wrapper_for type, *@casting
|
20
|
+
end
|
21
|
+
|
22
|
+
def typecast(actor)
|
23
|
+
return actor if @casting.empty?
|
24
|
+
cast_type(actor.class).new actor
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module WrapperBased
|
2
|
+
class Context
|
3
|
+
def initialize(**where)
|
4
|
+
where.each { |role, player| instance_variable_set :"@#{role}", player }
|
5
|
+
end
|
6
|
+
|
7
|
+
class << self
|
8
|
+
def [](**role_cast, &block)
|
9
|
+
context_class = Class.new(self, &block)
|
10
|
+
|
11
|
+
context_class.class_eval do
|
12
|
+
role_cast.each do |role, cast|
|
13
|
+
class_variable_set :"@@#{role}", cast
|
14
|
+
define_method(role) do
|
15
|
+
self.class.class_variable_get(:"@@#{role}").
|
16
|
+
typecast instance_variable_get(:"@#{role}")
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
context_class.singleton_class.class_eval do
|
22
|
+
role_cast.each do |role, _|
|
23
|
+
define_method(role) do
|
24
|
+
class_variable_get :"@@#{role}"
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
alias_method :[], :new
|
29
|
+
end
|
30
|
+
|
31
|
+
context_class
|
32
|
+
end # [] method
|
33
|
+
end # singleton class
|
34
|
+
end # Context class
|
35
|
+
end # WrapperBased module
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module WrapperBased
|
2
|
+
class DCI < Module
|
3
|
+
def initialize(wrapper_for = FORWARDING)
|
4
|
+
@@wrapper_for = Hash.new do |cache, type_casting|
|
5
|
+
cache[type_casting] = wrapper_for[*type_casting]
|
6
|
+
end unless defined? @@wrapper_for
|
7
|
+
|
8
|
+
def wrapper_for(*type_casting)
|
9
|
+
@@wrapper_for[type_casting]
|
10
|
+
end unless defined? self.wrapper_for
|
11
|
+
|
12
|
+
def Context(*roles, &block)
|
13
|
+
dci = self
|
14
|
+
cast = roles.inject({}) { |table, role| table[role.to_sym] = WrapperBased::Cast.new(role, dci); table }
|
15
|
+
WrapperBased::Context[**cast, &block]
|
16
|
+
end unless defined? self.Context
|
17
|
+
end # initialize method
|
18
|
+
end # DCI class
|
19
|
+
end # WrapperBased module
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'delegate'
|
2
|
+
|
3
|
+
module WrapperBased
|
4
|
+
class Decorator < Delegator
|
5
|
+
alias_method :initialize, def __setobj__(obj)
|
6
|
+
@delegate_sd_obj = obj # change delegation object
|
7
|
+
end
|
8
|
+
|
9
|
+
alias_method :~@, def __getobj__
|
10
|
+
@delegate_sd_obj # return object we are delegating to
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require "wrapper_based/version"
|
2
|
+
require "wrapper_based/cast"
|
3
|
+
require "wrapper_based/context"
|
4
|
+
require "wrapper_based/dci"
|
5
|
+
require "wrapper_based/decorator"
|
6
|
+
|
7
|
+
module WrapperBased
|
8
|
+
FORWARDING = -> type, *behaviors do
|
9
|
+
Class.new(Decorator) do
|
10
|
+
using Module.new { refine(type) { prepend(*behaviors.reverse) } }
|
11
|
+
|
12
|
+
def method_missing(meth, *args, &block)
|
13
|
+
__getobj__.send(meth, *args, &block)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "wrapper_based/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "wrapper_based"
|
8
|
+
spec.version = WrapperBased::VERSION
|
9
|
+
spec.authors = ["Ritchie Paul Buitre"]
|
10
|
+
spec.email = ["ritchie@richorelse.com"]
|
11
|
+
|
12
|
+
spec.summary = "Wrapper Based DCI implementation in Ruby."
|
13
|
+
spec.homepage = "https://github.com/RichOrElse/wrapper-based/"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
17
|
+
f.match(%r{^(test|spec|features)/})
|
18
|
+
end
|
19
|
+
spec.bindir = "exe"
|
20
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
21
|
+
spec.require_paths = ["lib"]
|
22
|
+
spec.required_ruby_version = ">= 2.4.0"
|
23
|
+
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.15"
|
25
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
26
|
+
spec.add_development_dependency "minitest", "~> 5.0"
|
27
|
+
end
|
metadata
ADDED
@@ -0,0 +1,105 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: wrapper_based
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ritchie Paul Buitre
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-06-16 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.15'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.15'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '5.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '5.0'
|
55
|
+
description:
|
56
|
+
email:
|
57
|
+
- ritchie@richorelse.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".travis.yml"
|
64
|
+
- Gemfile
|
65
|
+
- LICENSE.txt
|
66
|
+
- README.md
|
67
|
+
- Rakefile
|
68
|
+
- bin/console
|
69
|
+
- bin/setup
|
70
|
+
- examples/dijkstra.rb
|
71
|
+
- examples/dijkstra/data.rb
|
72
|
+
- examples/money_transfer.rb
|
73
|
+
- examples/toy_shop.rb
|
74
|
+
- lib/wrapper_based.rb
|
75
|
+
- lib/wrapper_based/cast.rb
|
76
|
+
- lib/wrapper_based/context.rb
|
77
|
+
- lib/wrapper_based/dci.rb
|
78
|
+
- lib/wrapper_based/decorator.rb
|
79
|
+
- lib/wrapper_based/version.rb
|
80
|
+
- wrapper_based.gemspec
|
81
|
+
homepage: https://github.com/RichOrElse/wrapper-based/
|
82
|
+
licenses:
|
83
|
+
- MIT
|
84
|
+
metadata: {}
|
85
|
+
post_install_message:
|
86
|
+
rdoc_options: []
|
87
|
+
require_paths:
|
88
|
+
- lib
|
89
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
90
|
+
requirements:
|
91
|
+
- - ">="
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: 2.4.0
|
94
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
95
|
+
requirements:
|
96
|
+
- - ">="
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: '0'
|
99
|
+
requirements: []
|
100
|
+
rubyforge_project:
|
101
|
+
rubygems_version: 2.6.8
|
102
|
+
signing_key:
|
103
|
+
specification_version: 4
|
104
|
+
summary: Wrapper Based DCI implementation in Ruby.
|
105
|
+
test_files: []
|