rest_my_case 1.2.0 → 1.3.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1d0c344e572726b96515f59752258d4073b89937
4
- data.tar.gz: aa11435140ce2d8f8b37092b8ad80700194114dc
3
+ metadata.gz: f2d0b9d8de92ec372285332d8dce7720f3224323
4
+ data.tar.gz: 3bfcd6e611ee75c3ec7ff74cde2246ad80fb2a9e
5
5
  SHA512:
6
- metadata.gz: 28dfb80a2170e238e2c1298887f3bf7c49b21ff0ea3f2ec99060ea937423cf4d45a79ef9a6aff3fb68eb83da1bdb7041270ec9298c0a71ef02a4b6936628b022
7
- data.tar.gz: d18a6ace349f311fcf3ce082fd1bb6608593ccd9019d54ac3b3fc9010a45c8fe5cd80f279de2a3a84f831a319192cec7fb4dda2c9c48901744f46884300dde37
6
+ metadata.gz: a8f3aea034cbf77270ff5a6e9f910959cbd8c536029c66f2b822490947346b1eefea71a960a010f0b8df540d25672bec7cbd61c67aa3caf765d01b94ad2ec745
7
+ data.tar.gz: b000730a80b5b848b4890154c5993a26d4d11d52a55c63b5bafb0c8ad22b1c7a65399c62b928fb9771f4132255be863c504d0d3b20fc1b3860f16332dfa2ff83
data/lib/rest_my_case.rb CHANGED
@@ -1,4 +1,14 @@
1
- require "rest_my_case/configuration/base"
1
+ require 'ostruct'
2
+
3
+ require "rest_my_case/config/shared"
4
+ require "rest_my_case/config/base"
5
+ require "rest_my_case/config/general"
6
+
7
+ require "rest_my_case/judge/base"
8
+ require "rest_my_case/trial_case/base"
9
+ require "rest_my_case/trial_case/context"
10
+ require "rest_my_case/defense_attorney/base"
11
+
2
12
  require "rest_my_case/version"
3
13
  require "rest_my_case/errors"
4
14
  require "rest_my_case/base"
@@ -10,11 +20,11 @@ module RestMyCase
10
20
  end
11
21
 
12
22
  def self.config
13
- @config ||= Configuration::Base.new
23
+ @config ||= Config::General.new
14
24
  end
15
25
 
16
26
  def self.reset_config
17
- @config = Configuration::Base.new
27
+ @config = Config::General.new
18
28
  end
19
29
 
20
30
  def self.get_config(attribute, use_case)
@@ -1,11 +1,8 @@
1
- require "rest_my_case/defense_attorney"
2
- require "rest_my_case/judges/base"
3
-
4
1
  module RestMyCase
5
2
 
6
3
  class Base
7
4
 
8
- extend Configuration::Shared
5
+ extend Config::Base
9
6
 
10
7
  def self.depends(*use_cases)
11
8
  dependencies.push *use_cases
@@ -16,9 +13,15 @@ module RestMyCase
16
13
  end
17
14
 
18
15
  def self.perform(attributes = {})
19
- new_trial_case = DefenseAttorney.new(self, attributes).build_trial_case
16
+ trial_case = TrialCase::Base.new self, attributes
17
+ attorney = DefenseAttorney::Base.new trial_case
18
+ judge = Judge::Base.new trial_case
19
+
20
+ attorney.build_case_for_the_defendant
21
+
22
+ judge.execute_the_sentence
20
23
 
21
- Judges::Base.new(trial_case).execute_the_sentence
24
+ trial_case.context
22
25
  end
23
26
 
24
27
  def self.context_accessor(*methods)
@@ -38,12 +41,11 @@ module RestMyCase
38
41
 
39
42
  ##################### INSTANCE METHODS BELLOW ###################
40
43
 
41
- attr_reader :context, :should_abort, :should_skip
44
+ attr_reader :context, :options
42
45
 
43
- def initialize(context)
44
- @context = context
45
- @should_skip = false
46
- @should_abort = false
46
+ def initialize(context, options = {})
47
+ @context = context
48
+ @options = options
47
49
  end
48
50
 
49
51
  def setup; end
@@ -63,7 +65,7 @@ module RestMyCase
63
65
  end
64
66
 
65
67
  def abort
66
- @should_abort = true
68
+ options[:should_abort] = true
67
69
  end
68
70
 
69
71
  def abort!
@@ -71,7 +73,7 @@ module RestMyCase
71
73
  end
72
74
 
73
75
  def skip
74
- @should_skip = true
76
+ options[:should_skip] = true
75
77
  end
76
78
 
77
79
  def skip!
@@ -0,0 +1,13 @@
1
+ module RestMyCase
2
+ module Config
3
+
4
+ module Base
5
+
6
+ include Shared
7
+
8
+ attr_accessor :silence_dependencies_abort
9
+
10
+ end
11
+
12
+ end
13
+ end
@@ -1,9 +1,7 @@
1
- require "rest_my_case/configuration/shared"
2
-
3
1
  module RestMyCase
4
- module Configuration
2
+ module Config
5
3
 
6
- class Base
4
+ class General
7
5
 
8
6
  include Shared
9
7
 
@@ -1,5 +1,5 @@
1
1
  module RestMyCase
2
- module Configuration
2
+ module Config
3
3
 
4
4
  module Shared
5
5
 
@@ -0,0 +1,61 @@
1
+ module RestMyCase
2
+ module DefenseAttorney
3
+
4
+ class Base
5
+
6
+ def initialize(trial_case)
7
+ @trial_case = trial_case
8
+ end
9
+
10
+ def build_case_for_the_defendant
11
+ @trial_case.use_cases = all_dependencies @trial_case.defendant
12
+ end
13
+
14
+ protected ###################### PROTECTED #########################
15
+
16
+ def all_dependencies(use_case, options = {})
17
+ return [] unless use_case.respond_to?(:dependencies)
18
+
19
+ parent_dependencies = all_dependencies(use_case.superclass, options)
20
+
21
+ if RestMyCase.get_config(:parent_dependencies_first, use_case)
22
+ parent_dependencies | deep_dependencies(use_case, options)
23
+ else
24
+ deep_dependencies(use_case, options) | parent_dependencies
25
+ end
26
+ end
27
+
28
+ def deep_dependencies(use_case, options)
29
+ options = build_options(use_case, options)
30
+ deep_dependencies = []
31
+
32
+ use_case.dependencies.each do |dependency|
33
+ deep_dependencies.push *all_dependencies(dependency, options)
34
+ end
35
+
36
+ include_current_use_case(deep_dependencies, use_case, options)
37
+ end
38
+
39
+ private ####################### PRIVATE ###########################
40
+
41
+ def build_options(use_case, options)
42
+ unless use_case.silence_dependencies_abort.nil?
43
+ options[:silent_abort] = use_case.silence_dependencies_abort
44
+ end
45
+
46
+ options
47
+ end
48
+
49
+ def include_current_use_case(dependencies, use_case, options)
50
+ return dependencies unless use_case.superclass.respond_to? :dependencies
51
+
52
+ append_method =
53
+ RestMyCase.get_config(:dependencies_first, use_case) ? :push : :unshift
54
+
55
+ dependencies.send append_method, use_case.new(@trial_case.context, options)
56
+ end
57
+
58
+ end
59
+
60
+ end
61
+ end
@@ -1,10 +1,10 @@
1
1
  module RestMyCase
2
- module Judges
2
+ module Judge
3
3
 
4
4
  class Base
5
5
 
6
- def initialize(use_cases)
7
- @use_cases = use_cases
6
+ def initialize(trial_case)
7
+ @trial_case = trial_case
8
8
  @performed_use_cases = []
9
9
  @use_case_that_aborted = false
10
10
  end
@@ -19,7 +19,7 @@ module RestMyCase
19
19
  protected #################### PROTECTED ####################
20
20
 
21
21
  def run_setup_methods
22
- @use_cases.each do |use_case|
22
+ @trial_case.use_cases.each do |use_case|
23
23
  break if method_setup_has_aborted use_case
24
24
  end
25
25
  end
@@ -27,7 +27,7 @@ module RestMyCase
27
27
  def run_perform_methods
28
28
  return nil if @use_case_that_aborted
29
29
 
30
- @use_cases.each do |use_case|
30
+ @trial_case.use_cases.each do |use_case|
31
31
  break if method_perform_has_aborted use_case
32
32
  end
33
33
  end
@@ -41,7 +41,7 @@ module RestMyCase
41
41
  end
42
42
 
43
43
  def run_final_methods
44
- @use_cases.each { |use_case| run_method(:final, use_case) }
44
+ @trial_case.use_cases.each { |use_case| run_method(:final, use_case) }
45
45
  end
46
46
 
47
47
  private #################### PRIVATE ######################
@@ -51,7 +51,7 @@ module RestMyCase
51
51
  end
52
52
 
53
53
  def method_perform_has_aborted(use_case)
54
- return false if use_case.should_skip
54
+ return false if use_case.options[:should_skip]
55
55
 
56
56
  @performed_use_cases.push use_case
57
57
 
@@ -62,7 +62,7 @@ module RestMyCase
62
62
  begin
63
63
  run_method(method_name, use_case)
64
64
 
65
- use_case.should_abort
65
+ use_case.options[:should_abort]
66
66
  rescue Errors::Skip => exception
67
67
  false
68
68
  rescue Errors::Abort => exception
@@ -0,0 +1,25 @@
1
+ module RestMyCase
2
+ module TrialCase
3
+
4
+ class Base
5
+
6
+ attr_reader :defendant, :context
7
+
8
+ attr_accessor :use_cases
9
+
10
+ def initialize(defendant, attributes = {})
11
+ attributes ||= {}
12
+
13
+ unless attributes.respond_to?(:to_hash)
14
+ raise ArgumentError.new('Must respond_to method #to_hash')
15
+ end
16
+
17
+ @context = Context.new attributes.to_hash
18
+ @defendant = defendant
19
+ @use_cases = []
20
+ end
21
+
22
+ end
23
+
24
+ end
25
+ end
@@ -0,0 +1,25 @@
1
+ module RestMyCase
2
+ module TrialCase
3
+
4
+ class Context < OpenStruct
5
+
6
+ def errors
7
+ @errors ||= Hash.new { |hash, key| hash[key] = [] }
8
+ end
9
+
10
+ def valid?
11
+ errors.empty?
12
+ end
13
+
14
+ alias :ok? :valid?
15
+
16
+ def serializable_hash(options = nil)
17
+ marshal_dump
18
+ end
19
+
20
+ alias :to_hash :serializable_hash
21
+
22
+ end
23
+
24
+ end
25
+ end
@@ -1,5 +1,5 @@
1
1
  module RestMyCase
2
2
 
3
- VERSION = "1.2.0"
3
+ VERSION = "1.3.2"
4
4
 
5
5
  end
@@ -2,37 +2,15 @@ require 'spec_helper'
2
2
 
3
3
  describe RestMyCase::Base do
4
4
 
5
- module RestMyCaseBase
6
- class LogEvents < RestMyCase::Base; end
7
- class AnalyseEvents < RestMyCase::Base; end
8
- class BuildPost < RestMyCase::Base; end
9
- class SavePost < RestMyCase::Base; end
10
- class BuildComments < RestMyCase::Base; end
11
- class SaveComments < RestMyCase::Base; end
12
-
13
- class UseCaseWrapper < RestMyCase::Base
14
- context_writer :id
15
- context_reader :session
16
- context_accessor :comment
17
- depends LogEvents, AnalyseEvents
18
- end
19
- class CreatePost < UseCaseWrapper
20
- depends BuildPost, SavePost
21
- end
22
- class CreatePostWithComments < CreatePost
23
- depends BuildComments, SaveComments
24
- end
25
- end
26
-
27
5
  describe ".dependencies" do
28
6
  it " should only list the class's dependencies" do
29
7
  expect(RestMyCaseBase::CreatePostWithComments.dependencies).to \
30
- eq [RestMyCaseBase::BuildComments, RestMyCaseBase::SaveComments]
8
+ eq [RestMyCaseBase::BuildComments, RestMyCaseBase::CreateComments]
31
9
  end
32
10
  end
33
11
 
34
12
  describe ".context_accessor" do
35
- let(:context) { RestMyCase::Context.new(id: 1, comment: 'my comment', session: -1) }
13
+ let(:context) { RestMyCase::TrialCase::Context.new(id: 1, comment: 'my comment', session: -1) }
36
14
  let(:use_case) { RestMyCaseBase::CreatePostWithComments.new(context) }
37
15
 
38
16
  it "Should create getters targeting to context" do
@@ -54,7 +32,7 @@ describe RestMyCase::Base do
54
32
  end
55
33
 
56
34
  describe ".context_writer" do
57
- let(:use_case) { RestMyCaseBase::CreatePostWithComments.new(RestMyCase::Context.new) }
35
+ let(:use_case) { RestMyCaseBase::CreatePostWithComments.new(RestMyCase::TrialCase::Context.new) }
58
36
 
59
37
  it "Should create setters targeting to context" do
60
38
  expect(use_case.respond_to?(:id)).to be false
@@ -69,7 +47,7 @@ describe RestMyCase::Base do
69
47
  end
70
48
 
71
49
  describe ".context_reader" do
72
- let(:context) { RestMyCase::Context.new(id: 1, comment: 'my comment', session: -1) }
50
+ let(:context) { RestMyCase::TrialCase::Context.new(id: 1, comment: 'my comment', session: -1) }
73
51
  let(:use_case) { RestMyCaseBase::CreatePostWithComments.new(context) }
74
52
 
75
53
  it "Should create getters targeting to context" do
@@ -82,4 +60,35 @@ describe RestMyCase::Base do
82
60
  end
83
61
  end
84
62
 
63
+ describe ".perform" do
64
+
65
+ context "When something that doesn't responde to #to_hash is used" do
66
+ it "should raise an exception" do
67
+ expect { RestMyCaseBase::CreatePost.perform(Object.new) }.to \
68
+ raise_error(ArgumentError)
69
+ end
70
+ end
71
+
72
+ context "When a use case #abort during the setup process" do
73
+ before { @context = Perform::CreatePost.perform }
74
+
75
+ it "context should reflect an invalid state" do
76
+ expect(@context.valid?).to be false
77
+ end
78
+
79
+ it "context should contain only one error" do
80
+ expect(@context.errors.keys.length).to be 1
81
+ end
82
+
83
+ it "context must not be populated by the #perform methods" do
84
+ # binding.pry
85
+ expect(@context.setup.length).to be 2
86
+ expect(@context.perform.length).to be 0
87
+ expect(@context.rollback.length).to be 2
88
+ expect(@context.final.length).to be 6
89
+ end
90
+ end
91
+
92
+ end
93
+
85
94
  end
@@ -1,34 +1,11 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe RestMyCase::DefenseAttorney do
3
+ describe RestMyCase::DefenseAttorney::Base do
4
4
 
5
- module DefenseAttorney
6
- class BuilEvent < RestMyCase::Base; end
7
- class SaveEvent < RestMyCase::Base; end
8
- class CreateEvent < RestMyCase::Base
9
- depends SaveEvent
10
- end
11
- class LogEvents < RestMyCase::Base
12
- depends BuilEvent, CreateEvent
13
- end
14
- class AnalyseEvents < RestMyCase::Base; end
15
- class BuildPost < RestMyCase::Base; end
16
- class SavePost < RestMyCase::Base; end
17
- class BuildComments < RestMyCase::Base; end
18
- class SaveComments < RestMyCase::Base; end
19
- class CreateComments < RestMyCase::Base
20
- depends SaveComments
21
- end
5
+ let(:use_cases) do
6
+ described_class.new(trial_case).build_case_for_the_defendant
22
7
 
23
- class UseCaseWrapper < RestMyCase::Base
24
- depends LogEvents, AnalyseEvents
25
- end
26
- class CreatePost < UseCaseWrapper
27
- depends BuildPost, SavePost
28
- end
29
- class CreatePostWithComments < CreatePost
30
- depends BuildComments, CreateComments
31
- end
8
+ trial_case.use_cases
32
9
  end
33
10
 
34
11
  shared_examples "a porper shepherd" do |dependencies|
@@ -48,9 +25,7 @@ describe RestMyCase::DefenseAttorney do
48
25
  end
49
26
 
50
27
  context "When a use case depends on other use cases" do
51
- let(:use_cases) do
52
- described_class.new(DefenseAttorney::UseCaseWrapper, id: 1).build_trial_case
53
- end
28
+ let(:trial_case) { RestMyCase::TrialCase::Base.new(DefenseAttorney::UseCaseWrapper, id: 1) }
54
29
 
55
30
  it_behaves_like "a porper shepherd", [
56
31
  DefenseAttorney::BuilEvent,
@@ -63,9 +38,7 @@ describe RestMyCase::DefenseAttorney do
63
38
  end
64
39
 
65
40
  context "When a use case inherits from another that also has its own dependencies" do
66
- let(:use_cases) do
67
- described_class.new(DefenseAttorney::CreatePostWithComments, id: 1).build_trial_case
68
- end
41
+ let(:trial_case) { RestMyCase::TrialCase::Base.new DefenseAttorney::CreatePostWithComments, id: 1 }
69
42
 
70
43
  it_behaves_like "a porper shepherd", [
71
44
  DefenseAttorney::BuilEvent,
@@ -85,6 +58,8 @@ describe RestMyCase::DefenseAttorney do
85
58
  end
86
59
 
87
60
  context "When general configuration has parent_dependencies_first = false" do
61
+ let(:trial_case) { RestMyCase::TrialCase::Base.new DefenseAttorney::CreatePostWithComments, id: 1 }
62
+
88
63
  before do
89
64
  RestMyCase.configure do |config|
90
65
  config.parent_dependencies_first = false
@@ -93,10 +68,6 @@ describe RestMyCase::DefenseAttorney do
93
68
 
94
69
  after { RestMyCase.reset_config }
95
70
 
96
- let(:use_cases) do
97
- described_class.new(DefenseAttorney::CreatePostWithComments, id: 1).build_trial_case
98
- end
99
-
100
71
  it_behaves_like "a porper shepherd", [
101
72
  DefenseAttorney::BuildComments,
102
73
  DefenseAttorney::SaveComments,
@@ -117,6 +88,8 @@ describe RestMyCase::DefenseAttorney do
117
88
  end
118
89
 
119
90
  context "When general configuration has dependencies_first = false" do
91
+ let(:trial_case) { RestMyCase::TrialCase::Base.new DefenseAttorney::CreatePostWithComments, id: 1 }
92
+
120
93
  before do
121
94
  RestMyCase.configure do |config|
122
95
  config.dependencies_first = false
@@ -125,10 +98,6 @@ describe RestMyCase::DefenseAttorney do
125
98
 
126
99
  after { RestMyCase.reset_config }
127
100
 
128
- let(:use_cases) do
129
- described_class.new(DefenseAttorney::CreatePostWithComments, id: 1).build_trial_case
130
- end
131
-
132
101
  it_behaves_like "a porper shepherd", [
133
102
  DefenseAttorney::UseCaseWrapper,
134
103
  DefenseAttorney::LogEvents,
@@ -147,16 +116,14 @@ describe RestMyCase::DefenseAttorney do
147
116
  end
148
117
 
149
118
  context "When an use case class configuration has dependencies_first = false" do
119
+ let(:trial_case) { RestMyCase::TrialCase::Base.new DefenseAttorney::CreatePostWithComments, id: 1 }
120
+
150
121
  before do
151
122
  DefenseAttorney::UseCaseWrapper.dependencies_first = false
152
123
  end
153
124
 
154
125
  after { DefenseAttorney::UseCaseWrapper.dependencies_first = nil }
155
126
 
156
- let(:use_cases) do
157
- described_class.new(DefenseAttorney::CreatePostWithComments, id: 1).build_trial_case
158
- end
159
-
160
127
  it_behaves_like "a porper shepherd", [
161
128
  DefenseAttorney::UseCaseWrapper,
162
129
  DefenseAttorney::BuilEvent,
@@ -0,0 +1,37 @@
1
+ require 'spec_helper'
2
+
3
+ describe RestMyCase::TrialCase::Base do
4
+
5
+ context "When something that doesn't responds to #to_hash is passed down" do
6
+ it "should raise an exception" do
7
+ expect { described_class.new(Object, Object.new) }.to \
8
+ raise_error(ArgumentError)
9
+ end
10
+ end
11
+
12
+ context "When nil is passed down" do
13
+ it "should NOT raise an exception" do
14
+ expect { described_class.new(Object, nil) }.not_to raise_error
15
+ end
16
+ end
17
+
18
+ context "When nothing is passed down" do
19
+ it "should NOT raise an exception" do
20
+ expect { described_class.new(Object) }.not_to raise_error
21
+ end
22
+ end
23
+
24
+ context "When something that responds to #to_hash is passed down" do
25
+ let(:context) { described_class.new(Object, id: 1, name: '2').context }
26
+
27
+ it "should create a context with it" do
28
+ expect(context).to be_a RestMyCase::TrialCase::Context
29
+ end
30
+
31
+ it "context should contain the attributes passed down" do
32
+ expect(context.id).to be 1
33
+ expect(context.name).to eq '2'
34
+ end
35
+ end
36
+
37
+ end
@@ -0,0 +1,41 @@
1
+ module DefenseAttorney
2
+
3
+ class BuilEvent < RestMyCase::Base; end
4
+
5
+ class SaveEvent < RestMyCase::Base; end
6
+
7
+ class CreateEvent < RestMyCase::Base
8
+ depends SaveEvent
9
+ end
10
+
11
+ class LogEvents < RestMyCase::Base
12
+ depends BuilEvent, CreateEvent
13
+ end
14
+
15
+ class AnalyseEvents < RestMyCase::Base; end
16
+
17
+ class BuildPost < RestMyCase::Base; end
18
+
19
+ class SavePost < RestMyCase::Base; end
20
+
21
+ class BuildComments < RestMyCase::Base; end
22
+
23
+ class SaveComments < RestMyCase::Base; end
24
+
25
+ class CreateComments < RestMyCase::Base
26
+ depends SaveComments
27
+ end
28
+
29
+ class UseCaseWrapper < RestMyCase::Base
30
+ depends LogEvents, AnalyseEvents
31
+ end
32
+
33
+ class CreatePost < UseCaseWrapper
34
+ depends BuildPost, SavePost
35
+ end
36
+
37
+ class CreatePostWithComments < CreatePost
38
+ depends BuildComments, CreateComments
39
+ end
40
+
41
+ end
@@ -0,0 +1,128 @@
1
+ module Perform
2
+
3
+ class ValidateName < RestMyCase::Base
4
+ context_reader :name
5
+
6
+ def setup
7
+ fail('no name present!') if name.nil?
8
+
9
+ context.setup << self.class.name
10
+ end
11
+
12
+ def perform
13
+ context.perform << self.class.name
14
+ end
15
+
16
+ def rollback
17
+ context.rollback << self.class.name
18
+ end
19
+
20
+ def final
21
+ context.final << self.class.name
22
+ end
23
+ end
24
+
25
+ class ValidateBody < RestMyCase::Base
26
+ context_reader :body
27
+
28
+ def setup
29
+ fail('no body present!') if body.nil?
30
+
31
+ context.setup << self.class.name
32
+ end
33
+
34
+ def perform
35
+ context.perform << self.class.name
36
+ end
37
+
38
+ def rollback
39
+ context.rollback << self.class.name
40
+ end
41
+
42
+ def final
43
+ context.final << self.class.name
44
+ end
45
+ end
46
+
47
+ class Validations < RestMyCase::Base
48
+ depends ValidateName, ValidateBody
49
+
50
+ def setup
51
+ context.setup << self.class.name
52
+ end
53
+
54
+ def perform
55
+ context.perform << self.class.name
56
+ end
57
+
58
+ def rollback
59
+ context.rollback << self.class.name
60
+ end
61
+
62
+ def final
63
+ context.final << self.class.name
64
+ end
65
+ end
66
+
67
+ class BuildPost < RestMyCase::Base
68
+ def setup
69
+ context.setup = []
70
+ context.perform = []
71
+ context.rollback = []
72
+ context.final = []
73
+
74
+ context.setup << self.class.name
75
+ end
76
+
77
+ def perform
78
+ context.perform << self.class.name
79
+ end
80
+
81
+ def rollback
82
+ context.rollback << self.class.name
83
+ end
84
+
85
+ def final
86
+ context.final << self.class.name
87
+ end
88
+ end
89
+
90
+ class SavePost < RestMyCase::Base
91
+ def setup
92
+ context.setup << self.class.name
93
+ end
94
+
95
+ def perform
96
+ context.perform << self.class.name
97
+ end
98
+
99
+ def rollback
100
+ context.rollback << self.class.name
101
+ end
102
+
103
+ def final
104
+ context.final << self.class.name
105
+ end
106
+ end
107
+
108
+ class CreatePost < RestMyCase::Base
109
+ depends BuildPost, Validations, SavePost
110
+
111
+ def setup
112
+ context.setup << self.class.name
113
+ end
114
+
115
+ def perform
116
+ context.perform << self.class.name
117
+ end
118
+
119
+ def rollback
120
+ context.rollback << self.class.name
121
+ end
122
+
123
+ def final
124
+ context.final << self.class.name
125
+ end
126
+ end
127
+
128
+ end
@@ -0,0 +1,47 @@
1
+ module RestMyCaseBase
2
+
3
+ class BuildPost < RestMyCase::Base; end
4
+
5
+ class ValidateName < RestMyCase::Base
6
+ def perform; fail('no name present!'); end
7
+ end
8
+
9
+ class ValidateBody < RestMyCase::Base
10
+ def perform; fail('no body present!'); end
11
+ end
12
+
13
+ class Validations < RestMyCase::Base
14
+ depends ValidateName, ValidateBody
15
+
16
+ self.silence_dependencies_abort = true
17
+ end
18
+
19
+ class SavePost < RestMyCase::Base; end
20
+
21
+ class BuildComments < RestMyCase::Base; end
22
+
23
+ class SaveComments < RestMyCase::Base; end
24
+
25
+ class CreateComments < RestMyCase::Base
26
+ depends SaveComments
27
+ end
28
+
29
+ class UseCaseWrapper < RestMyCase::Base
30
+ context_writer :id
31
+ context_reader :session
32
+ context_accessor :comment
33
+ end
34
+
35
+ class CreatePost < UseCaseWrapper
36
+ depends BuildPost, SavePost
37
+ end
38
+
39
+ class CreatePostWithValidations < CreatePost
40
+ depends Validations
41
+ end
42
+
43
+ class CreatePostWithComments < CreatePost
44
+ depends BuildComments, CreateComments
45
+ end
46
+
47
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rest_my_case
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - goncalvesjoao
@@ -83,18 +83,23 @@ files:
83
83
  - Rakefile
84
84
  - lib/rest_my_case.rb
85
85
  - lib/rest_my_case/base.rb
86
- - lib/rest_my_case/configuration/base.rb
87
- - lib/rest_my_case/configuration/shared.rb
88
- - lib/rest_my_case/context.rb
89
- - lib/rest_my_case/defense_attorney.rb
86
+ - lib/rest_my_case/config/base.rb
87
+ - lib/rest_my_case/config/general.rb
88
+ - lib/rest_my_case/config/shared.rb
89
+ - lib/rest_my_case/defense_attorney/base.rb
90
90
  - lib/rest_my_case/errors.rb
91
- - lib/rest_my_case/judges/base.rb
91
+ - lib/rest_my_case/judge/base.rb
92
+ - lib/rest_my_case/trial_case/base.rb
93
+ - lib/rest_my_case/trial_case/context.rb
92
94
  - lib/rest_my_case/version.rb
93
95
  - rest_my_case.gemspec
94
96
  - spec/rest_my_case/base_spec.rb
95
- - spec/rest_my_case/defense_attorney_spec.rb
96
- - spec/rest_my_case/judges/base_spec.rb
97
+ - spec/rest_my_case/defense_attorney/base_spec.rb
98
+ - spec/rest_my_case/trial_case/base_spec.rb
97
99
  - spec/spec_helper.rb
100
+ - spec/support/defense_attorney.rb
101
+ - spec/support/perform.rb
102
+ - spec/support/rest_my_case_base.rb
98
103
  homepage: https://github.com/goncalvesjoao/rest_my_case
99
104
  licenses:
100
105
  - MIT
@@ -121,6 +126,9 @@ specification_version: 4
121
126
  summary: Quick and light "The Clean Architecture" use case implementation.
122
127
  test_files:
123
128
  - spec/rest_my_case/base_spec.rb
124
- - spec/rest_my_case/defense_attorney_spec.rb
125
- - spec/rest_my_case/judges/base_spec.rb
129
+ - spec/rest_my_case/defense_attorney/base_spec.rb
130
+ - spec/rest_my_case/trial_case/base_spec.rb
126
131
  - spec/spec_helper.rb
132
+ - spec/support/defense_attorney.rb
133
+ - spec/support/perform.rb
134
+ - spec/support/rest_my_case_base.rb
@@ -1,25 +0,0 @@
1
- require 'ostruct'
2
-
3
- module RestMyCase
4
-
5
- class Context < OpenStruct
6
-
7
- def errors
8
- @errors ||= Hash.new { |hash, key| hash[key] = [] }
9
- end
10
-
11
- def valid?
12
- errors.empty?
13
- end
14
-
15
- alias :ok? :valid?
16
-
17
- def serializable_hash(options = nil)
18
- marshal_dump
19
- end
20
-
21
- alias :to_hash :serializable_hash
22
-
23
- end
24
-
25
- end
@@ -1,58 +0,0 @@
1
- require "rest_my_case/context"
2
-
3
- module RestMyCase
4
-
5
- class DefenseAttorney
6
-
7
- def initialize(starting_point_use_case, attributes)
8
- unless attributes.respond_to?(:to_hash)
9
- raise ArgumentError.new('Must respond_to method #to_hash')
10
- end
11
-
12
- @shared_context = Context.new attributes.to_hash
13
- @starting_point_use_case = starting_point_use_case
14
- end
15
-
16
- def build_trial_case
17
- all_dependencies(@starting_point_use_case).map do |use_case|
18
- use_case.new(@shared_context)
19
- end
20
- end
21
-
22
- protected ###################### PROTECTED #########################
23
-
24
- def all_dependencies(use_case)
25
- return [] unless use_case.respond_to?(:dependencies)
26
-
27
- if RestMyCase.get_config(:parent_dependencies_first, use_case)
28
- all_dependencies(use_case.superclass) | deep_dependencies(use_case)
29
- else
30
- deep_dependencies(use_case) | all_dependencies(use_case.superclass)
31
- end
32
- end
33
-
34
- def deep_dependencies(use_case)
35
- deep_dependencies = []
36
-
37
- use_case.dependencies.each do |use_case|
38
- deep_dependencies.push *all_dependencies(use_case)
39
- end
40
-
41
- include_current_use_case(deep_dependencies, use_case)
42
- end
43
-
44
- private ####################### PRIVATE ###########################
45
-
46
- def include_current_use_case(dependencies, use_case)
47
- return dependencies unless use_case.superclass.respond_to?(:dependencies)
48
-
49
- if RestMyCase.get_config(:dependencies_first, use_case)
50
- dependencies.push(use_case)
51
- else
52
- dependencies.unshift(use_case)
53
- end
54
- end
55
-
56
- end
57
-
58
- end
@@ -1,28 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe RestMyCase::Judges::Base do
4
-
5
- describe "#build_use_cases_for_the_defendant" do
6
-
7
- # context "When a use case depends on other use cases" do
8
- # before { @result = defense_attorney.build_use_cases_for_the_defendant }
9
- # let(:attributes) { { id: 1, comment: 'my comment', session: -1 } }
10
- # let(:defense_attorney) do
11
- # RestMyCase::DefenseAttorney.new(Comments::Create::Base, attributes)
12
- # end
13
-
14
- # it "Should instanciate 3 objects" do
15
- # expect(@result.length).to be 3
16
- # end
17
-
18
- # it "dass" do
19
- # expect(@result[0]).to be_a Comments::Create::BuildComment
20
- # expect(@result[1]).to be_a Comments::SaveComment
21
- # expect(@result[2]).to be_a Comments::Create::SendEmail
22
- # end
23
-
24
- # end
25
-
26
- end
27
-
28
- end