Class: RestMyCase::Base
Instance Attribute Summary collapse
Attributes included from Config::Base
#parent_dependencies_first, #silence_dependencies_abort
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(context, dependent_use_case = nil) ⇒ Base
Returns a new instance of Base.
61
62
63
64
65
66
67
68
69
|
# File 'lib/rest_my_case/base.rb', line 61
def initialize(context, dependent_use_case = nil)
@context = context
@options = { dependent_use_case: dependent_use_case }
return unless dependent_use_case
@options[:silent_abort] = RestMyCase.get_config \
:silence_dependencies_abort, dependent_use_case.class
end
|
Instance Attribute Details
#context ⇒ Object
INSTANCE METHODS BELLOW ###########################
59
60
61
|
# File 'lib/rest_my_case/base.rb', line 59
def context
@context
end
|
#options ⇒ Object
INSTANCE METHODS BELLOW ###########################
59
60
61
|
# File 'lib/rest_my_case/base.rb', line 59
def options
@options
end
|
Class Method Details
.context_accessor(*methods) ⇒ Object
42
43
44
45
|
# File 'lib/rest_my_case/base.rb', line 42
def self.context_accessor(*methods)
context_writer(*methods)
context_reader(*methods)
end
|
.context_reader(*methods) ⇒ Object
53
54
55
|
# File 'lib/rest_my_case/base.rb', line 53
def self.context_reader(*methods)
methods.each { |method| define_method(method) { context.send(method) } }
end
|
.context_writer(*methods) ⇒ Object
47
48
49
50
51
|
# File 'lib/rest_my_case/base.rb', line 47
def self.context_writer(*methods)
methods.each do |method|
define_method("#{method}=") { |value| context.send "#{method}=", value }
end
end
|
.dependencies ⇒ Object
28
29
30
|
# File 'lib/rest_my_case/base.rb', line 28
def self.dependencies
@dependencies ||= []
end
|
.depends(*use_case_classes) ⇒ Object
24
25
26
|
# File 'lib/rest_my_case/base.rb', line 24
def self.depends(*use_case_classes)
dependencies.concat(use_case_classes)
end
|
32
33
34
35
36
37
38
39
40
|
# File 'lib/rest_my_case/base.rb', line 32
def self.perform(attributes = nil)
attributes ||= {}
unless attributes.respond_to?(:to_hash)
raise ArgumentError, 'Must respond_to method #to_hash'
end
trial_court.execute([self], attributes.to_hash).context
end
|
.required_context(*schema) ⇒ Object
10
11
12
13
14
15
16
17
18
|
# File 'lib/rest_my_case/base.rb', line 10
def self.required_context(*schema)
@required_context_schema = schema
if schema.length == 1 && (schema[0].is_a?(Hash) || schema[0].is_a?(Array))
@required_context_schema = schema[0]
end
@required_context_schema.each { |context_key| context_reader context_key }
end
|
.required_context_schema ⇒ Object
20
21
22
|
# File 'lib/rest_my_case/base.rb', line 20
def self.required_context_schema
@required_context_schema ||= {}
end
|
Instance Method Details
#abort ⇒ Object
91
92
93
94
95
96
97
|
# File 'lib/rest_my_case/base.rb', line 91
def abort
if options[:silent_abort]
options[:dependent_use_case].abort
else
options[:should_abort] = true
end
end
|
#abort! ⇒ Object
99
100
101
|
# File 'lib/rest_my_case/base.rb', line 99
def abort!
abort && raise(Errors::Abort)
end
|
#error(error_message = '') ⇒ Object
103
104
105
106
107
108
109
110
|
# File 'lib/rest_my_case/base.rb', line 103
def error(error_message = '')
error_data = \
error_message.is_a?(Hash) ? error_message : { message: error_message }
error_data[:class_name] = self.class.name
abort && context.errors.add(error_data)
end
|
#error!(error_data = '') ⇒ Object
112
113
114
|
# File 'lib/rest_my_case/base.rb', line 112
def error!(error_data = '')
error(error_data) && raise(Errors::Abort)
end
|
#final ⇒ Object
77
|
# File 'lib/rest_my_case/base.rb', line 77
def final; end
|
#invoke(*use_case_classes) ⇒ Object
79
80
81
|
# File 'lib/rest_my_case/base.rb', line 79
def invoke(*use_case_classes)
self.class.trial_court.execute(use_case_classes, context.to_hash).context
end
|
#invoke!(*use_case_classes) ⇒ Object
83
84
85
86
87
88
89
|
# File 'lib/rest_my_case/base.rb', line 83
def invoke!(*use_case_classes)
trial_case = self.class.trial_court.execute(use_case_classes, context)
abort! if trial_case.aborted
trial_case.context
end
|
73
|
# File 'lib/rest_my_case/base.rb', line 73
def perform; end
|
#rollback ⇒ Object
75
|
# File 'lib/rest_my_case/base.rb', line 75
def rollback; end
|
#setup ⇒ Object
71
|
# File 'lib/rest_my_case/base.rb', line 71
def setup; end
|
#skip ⇒ Object
116
117
118
|
# File 'lib/rest_my_case/base.rb', line 116
def skip
options[:should_skip] = true
end
|
#skip! ⇒ Object
120
121
122
|
# File 'lib/rest_my_case/base.rb', line 120
def skip!
skip && raise(Errors::Skip)
end
|
#validate_context(schema = self.class.required_context_schema) ⇒ Object
124
125
126
127
128
129
130
|
# File 'lib/rest_my_case/base.rb', line 124
def validate_context(schema = self.class.required_context_schema)
errors = context.validate_schema(schema)
error(context_errors: errors, message: 'invalid context') if errors
Helpers.blank? errors
end
|
#validate_context!(schema = self.class.required_context) ⇒ Object
132
133
134
|
# File 'lib/rest_my_case/base.rb', line 132
def validate_context!(schema = self.class.required_context)
validate_context(schema) && raise(Errors::Abort)
end
|