wrapper_based 0.4.0 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/examples/money_transfer.rb +7 -4
- data/lib/wrapper_based/context.rb +31 -27
- data/lib/wrapper_based/dci.rb +4 -2
- data/lib/wrapper_based/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9411d5ab7b4f35eb11de0d1c80a8f94a452001d4
|
4
|
+
data.tar.gz: afef2d54a25ab9110a9403f5be0ec19a2dbda211
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1a91041080d96a68f4c25ad70ef0dd6ba21d55befdfef62d845b3d42cb6bc21ca89453ae48f02a1c57b98c71204d80c75d0665a1d6bf70e6dd2d40a6d4b07f3a
|
7
|
+
data.tar.gz: 3d6421e710976af3cb2b13b9a3dd354c114ef7d2dfbf389c83291958931e405b6fb73e383baf4b6d6bd3999397765a73b218fb4196be86f37acb7e77ef083856
|
data/examples/money_transfer.rb
CHANGED
@@ -1,10 +1,10 @@
|
|
1
|
-
LogTransaction =
|
1
|
+
LogTransaction = Struct.new(:transaction_type, :amount, :account_number)
|
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
|
-
|
7
|
+
fail NotEnoughFunds, "Balance is below amount.", caller if balance < amount
|
8
8
|
self.balance -= amount
|
9
9
|
end
|
10
10
|
end
|
@@ -32,7 +32,10 @@ class TransferMoney < DCI::Context(:from, :to)
|
|
32
32
|
end
|
33
33
|
|
34
34
|
def call(amount:)
|
35
|
-
|
36
|
-
deposit(amount)
|
35
|
+
accounts = [@from, @to]
|
36
|
+
transaction_logs = [withdraw(amount), deposit(amount)]
|
37
|
+
[:success, { logs: transaction_logs }, accounts]
|
38
|
+
rescue NotEnoughFunds => error
|
39
|
+
[:failure, { message: error.message }, accounts]
|
37
40
|
end
|
38
41
|
end
|
@@ -13,39 +13,43 @@ module WrapperBased
|
|
13
13
|
call(*args, &block)
|
14
14
|
end
|
15
15
|
|
16
|
-
UnassignedRole = StandardError
|
16
|
+
UnassignedRole = Class.new(StandardError)
|
17
17
|
|
18
18
|
class << self
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
19
|
+
alias_method :[], :new
|
20
|
+
|
21
|
+
protected
|
22
|
+
|
23
|
+
def add_role(role, caster)
|
24
|
+
add_reader_for(role)
|
25
|
+
add_writer_for(role)
|
26
|
+
add_to_class(role, caster)
|
27
|
+
end
|
28
|
+
|
29
|
+
def add_reader_for(role)
|
30
|
+
define_method(role) do
|
31
|
+
@_casting.fetch(role) { raise UnassignedRole, "Role '#{role}' is missing." }
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def add_writer_for(role)
|
36
|
+
role_player = :"@#{role}"
|
37
|
+
|
38
|
+
define_method(:"#{role}=") do |actor|
|
39
|
+
instance_variable_set(role_player, actor)
|
40
|
+
@_casting[role] = self.class.send(role).typecast(actor)
|
35
41
|
end
|
42
|
+
end
|
36
43
|
|
37
|
-
|
38
|
-
|
39
|
-
variable = :"@@#{role}"
|
40
|
-
define_method(role) { class_variable_get variable }
|
41
|
-
context_class.class_variable_set variable, cast
|
42
|
-
end
|
44
|
+
def add_to_class(role, caster)
|
45
|
+
role_caster = :"@@#{role}"
|
43
46
|
|
44
|
-
|
47
|
+
singleton_class.class_eval do
|
48
|
+
define_method(role) { class_variable_get role_caster }
|
45
49
|
end
|
46
50
|
|
47
|
-
|
48
|
-
end
|
49
|
-
end #
|
51
|
+
class_variable_set role_caster, caster
|
52
|
+
end
|
53
|
+
end # ClassMethods module
|
50
54
|
end # Context class
|
51
55
|
end # WrapperBased module
|
data/lib/wrapper_based/dci.rb
CHANGED
@@ -11,8 +11,10 @@ module WrapperBased
|
|
11
11
|
|
12
12
|
def Context(*roles, &block)
|
13
13
|
dci = self
|
14
|
-
|
15
|
-
|
14
|
+
Class.new(WrapperBased::Context) do
|
15
|
+
roles.each { |role| add_role role, WrapperBased::Cast.new(role, dci) }
|
16
|
+
class_eval(&block) unless block.nil?
|
17
|
+
end
|
16
18
|
end unless defined? self.Context
|
17
19
|
end # initialize method
|
18
20
|
end # DCI class
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: wrapper_based
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ritchie Paul Buitre
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-06-
|
11
|
+
date: 2017-06-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|