wrapper_based 0.2.0 → 0.3.0
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/README.md +22 -3
- data/examples/dijkstra.rb +3 -3
- data/examples/money_transfer.rb +2 -2
- data/lib/wrapper_based/context.rb +8 -0
- data/lib/wrapper_based/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 860bb92daa686c64bcb7807cc743a0e1f41d892d
|
4
|
+
data.tar.gz: 15482c725f191bc7fa9a336fddebbc10e71a3051
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ab743403db24d5918f180321e2c6f45c93f5284c7d9bfab97038cf19b399abcb2d0f28870e98db9d0dc854d99e79a1afafa38e35288ccdb3c63285ab6718fbcb
|
7
|
+
data.tar.gz: 817c1a3a8494e3d259df6602f59eaf15a6931208ea530333201895dc8c8c1bb124160e4e75712ca62bb675ca515654330a6d1795bc700949af2096a4b950012a
|
data/README.md
CHANGED
@@ -60,12 +60,12 @@ DCI = WrapperBased::DCI.new unless defined? DCI
|
|
60
60
|
class Distance < DCI::Context(:within)
|
61
61
|
within.as Map
|
62
62
|
|
63
|
-
def between(
|
64
|
-
within.distance_between(
|
63
|
+
def between(from, to)
|
64
|
+
within.distance_between(from, to)
|
65
65
|
end
|
66
66
|
|
67
67
|
def of(path)
|
68
|
-
path.reverse.each_cons(2).inject(0) { |
|
68
|
+
path.reverse.each_cons(2).inject(0) { |total_distance, pair| total_distance + between(*pair) }
|
69
69
|
end
|
70
70
|
end
|
71
71
|
|
@@ -92,8 +92,27 @@ class Shortest < DCI::Context(:from, :to, :city)
|
|
92
92
|
end
|
93
93
|
end
|
94
94
|
```
|
95
|
+
|
95
96
|
[View more examples](https://github.com/RichOrElse/wrapper-based/tree/master/examples)
|
96
97
|
|
98
|
+
## Context methods
|
99
|
+
|
100
|
+
### to_proc
|
101
|
+
|
102
|
+
Returns call method as a Proc.
|
103
|
+
|
104
|
+
```ruby
|
105
|
+
['Card Wars', 'Ice Ninja Manual', 'Bacon'].map &GiftToy[gifter: 'Jake', giftee: 'Finn']
|
106
|
+
```
|
107
|
+
|
108
|
+
### context[params,...]
|
109
|
+
|
110
|
+
Square brackets are alias for call method.
|
111
|
+
|
112
|
+
```ruby
|
113
|
+
TransferMoney[from: source_account, to: destination_account][amount: 100]
|
114
|
+
```
|
115
|
+
|
97
116
|
## Development
|
98
117
|
|
99
118
|
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.
|
data/examples/dijkstra.rb
CHANGED
@@ -35,12 +35,12 @@ DCI = WrapperBased::DCI.new unless defined? DCI
|
|
35
35
|
class Distance < DCI::Context(:within)
|
36
36
|
within.as Map
|
37
37
|
|
38
|
-
def between(
|
39
|
-
within.distance_between(
|
38
|
+
def between(from, to)
|
39
|
+
within.distance_between(from, to)
|
40
40
|
end
|
41
41
|
|
42
42
|
def of(path)
|
43
|
-
path.reverse.each_cons(2).inject(0) { |
|
43
|
+
path.reverse.each_cons(2).inject(0) { |total_distance, pair| total_distance + between(*pair) }
|
44
44
|
end
|
45
45
|
end
|
46
46
|
|
data/examples/money_transfer.rb
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
Log = method(:puts)
|
2
2
|
Account = Struct.new(:number, :balance)
|
3
|
-
|
3
|
+
NotEnoughFunds = Class.new(StandardError)
|
4
4
|
|
5
5
|
module SourceAccount
|
6
6
|
def decrease_balance_by(amount)
|
7
|
-
raise
|
7
|
+
raise NotEnoughFunds, "Balance is below amount.", caller if balance < amount
|
8
8
|
self.balance -= amount
|
9
9
|
end
|
10
10
|
end
|
@@ -4,6 +4,14 @@ module WrapperBased
|
|
4
4
|
where.each { |role, player| instance_variable_set :"@#{role}", player }
|
5
5
|
end
|
6
6
|
|
7
|
+
def to_proc
|
8
|
+
method(:call).to_proc
|
9
|
+
end
|
10
|
+
|
11
|
+
def [](*args, &block)
|
12
|
+
call(*args, &block)
|
13
|
+
end
|
14
|
+
|
7
15
|
class << self
|
8
16
|
def [](**role_cast, &block)
|
9
17
|
context_class = Class.new(self, &block)
|