wrapper_based 0.4.0 → 0.5.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ff350bc479ae984adc21032e70fab33aea8f1ce5
4
- data.tar.gz: 281d02a14989a788d9b53ba6eb0b3f41c1ed86a6
3
+ metadata.gz: 9411d5ab7b4f35eb11de0d1c80a8f94a452001d4
4
+ data.tar.gz: afef2d54a25ab9110a9403f5be0ec19a2dbda211
5
5
  SHA512:
6
- metadata.gz: 4b0f9bab7b974ff6a94b8be0e91e63f779f8df8c0fd6e6d959a4d28632c91ef85ad5382a0ec6521ed572d82e060e0e9add15117370649ebd298d5d669eac8330
7
- data.tar.gz: 696723ab1d675cf1acb1cb9a6765823853bb84b31e95fe689dd4fcfe7afdf427c8bf9f00f7594c1316129675443366b22c3c424f2a760a457ab39722d4524b83
6
+ metadata.gz: 1a91041080d96a68f4c25ad70ef0dd6ba21d55befdfef62d845b3d42cb6bc21ca89453ae48f02a1c57b98c71204d80c75d0665a1d6bf70e6dd2d40a6d4b07f3a
7
+ data.tar.gz: 3d6421e710976af3cb2b13b9a3dd354c114ef7d2dfbf389c83291958931e405b6fb73e383baf4b6d6bd3999397765a73b218fb4196be86f37acb7e77ef083856
@@ -1,10 +1,10 @@
1
- LogTransaction = method(:puts)
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
- raise NotEnoughFunds, "Balance is below amount.", caller if balance < amount
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
- withdraw(amount)
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
- def [](**role_cast, &block)
20
- context_class = Class.new(self, &block)
21
-
22
- context_class.class_eval do
23
- role_cast.each do |role, cast|
24
- role_player = :"@#{role}"
25
-
26
- define_method(:"#{role}=") do |actor|
27
- instance_variable_set(role_player, actor)
28
- @_casting[role] = context_class.send(role).typecast(actor)
29
- end
30
-
31
- define_method(role) do
32
- @_casting.fetch(role) { fail UnassignedRole, "Role '#{role}' is missing." }
33
- end
34
- end
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
- context_class.singleton_class.class_eval do
38
- role_cast.each do |role, cast|
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
- alias_method :[], :new
47
+ singleton_class.class_eval do
48
+ define_method(role) { class_variable_get role_caster }
45
49
  end
46
50
 
47
- context_class
48
- end # [] method
49
- end # singleton class
51
+ class_variable_set role_caster, caster
52
+ end
53
+ end # ClassMethods module
50
54
  end # Context class
51
55
  end # WrapperBased module
@@ -11,8 +11,10 @@ module WrapperBased
11
11
 
12
12
  def Context(*roles, &block)
13
13
  dci = self
14
- cast = roles.inject({}) { |table, role| table[role.to_sym] = WrapperBased::Cast.new(role, dci); table }
15
- WrapperBased::Context[**cast, &block]
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
@@ -1,3 +1,3 @@
1
1
  module WrapperBased
2
- VERSION = "0.4.0"
2
+ VERSION = "0.5.0"
3
3
  end
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.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-23 00:00:00.000000000 Z
11
+ date: 2017-06-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler