Class: RestMyCase::Base

Inherits:
Object
  • Object
show all
Extended by:
Config::Base
Defined in:
lib/rest_my_case/base.rb

Direct Known Subclasses

Validator

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.



49
50
51
52
53
# File 'lib/rest_my_case/base.rb', line 49

def initialize(context, dependent_use_case = nil)
  @options            = {}
  @context            = context
  @dependent_use_case = dependent_use_case
end

Instance Attribute Details

#contextObject (readonly)

INSTANCE METHODS BELLOW ###########################



47
48
49
# File 'lib/rest_my_case/base.rb', line 47

def context
  @context
end

#dependent_use_caseObject (readonly)

INSTANCE METHODS BELLOW ###########################



47
48
49
# File 'lib/rest_my_case/base.rb', line 47

def dependent_use_case
  @dependent_use_case
end

#optionsObject (readonly)

INSTANCE METHODS BELLOW ###########################



47
48
49
# File 'lib/rest_my_case/base.rb', line 47

def options
  @options
end

Class Method Details

.context_accessor(*methods) ⇒ Object



30
31
32
33
# File 'lib/rest_my_case/base.rb', line 30

def self.context_accessor(*methods)
  context_writer(*methods)
  context_reader(*methods)
end

.context_reader(*methods) ⇒ Object



41
42
43
# File 'lib/rest_my_case/base.rb', line 41

def self.context_reader(*methods)
  methods.each { |method| define_method(method) { context.send(method) } }
end

.context_writer(*methods) ⇒ Object



35
36
37
38
39
# File 'lib/rest_my_case/base.rb', line 35

def self.context_writer(*methods)
  methods.each do |method|
    define_method("#{method}=") { |value| context.send "#{method}=", value }
  end
end

.dependenciesObject



16
17
18
# File 'lib/rest_my_case/base.rb', line 16

def self.dependencies
  @dependencies ||= []
end

.depends(*use_case_classes) ⇒ Object



12
13
14
# File 'lib/rest_my_case/base.rb', line 12

def self.depends(*use_case_classes)
  dependencies.push(*use_case_classes)
end

.perform(attributes = nil) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/rest_my_case/base.rb', line 20

def self.perform(attributes = nil)
  attributes ||= {}

  unless attributes.respond_to?(:to_hash)
    fail ArgumentError, 'Must respond_to method #to_hash'
  end

  trial_court.execute([self], attributes.to_hash).context
end

.trial_courtObject



7
8
9
10
# File 'lib/rest_my_case/base.rb', line 7

def self.trial_court
  @trial_court ||= Trial::Court.new \
    Judge::Base, DefenseAttorney::Base, RestMyCase::Base, Context::Base
end

Instance Method Details

#abortObject



73
74
75
# File 'lib/rest_my_case/base.rb', line 73

def abort
  silent_abort? ? dependent_use_case.abort : (options[:should_abort] = true)
end

#abort!Object



77
78
79
# File 'lib/rest_my_case/base.rb', line 77

def abort!
  abort && fail(Errors::Abort)
end

#error(error_data = '') ⇒ Object



81
82
83
84
85
86
87
# File 'lib/rest_my_case/base.rb', line 81

def error(error_data = '')
  error_data = { message: error_data } unless error_data.is_a?(Hash)

  error_data[:class_name] = self.class.name

  abort && context.errors.add(error_data)
end

#error!(error_data = '') ⇒ Object



89
90
91
# File 'lib/rest_my_case/base.rb', line 89

def error!(error_data = '')
  error(error_data) && fail(Errors::Abort)
end

#finalObject



61
# File 'lib/rest_my_case/base.rb', line 61

def final; end

#invoke(*use_case_classes) ⇒ Object



63
64
65
# File 'lib/rest_my_case/base.rb', line 63

def invoke(*use_case_classes)
  trial_court.execute(use_case_classes, context.to_hash).context
end

#invoke!(*use_case_classes) ⇒ Object



67
68
69
70
71
# File 'lib/rest_my_case/base.rb', line 67

def invoke!(*use_case_classes)
  trial_court.execute(use_case_classes, context).tap do |trial_case|
    abort if trial_case.aborted
  end.context
end

#performObject



57
# File 'lib/rest_my_case/base.rb', line 57

def perform;  end

#rollbackObject



59
# File 'lib/rest_my_case/base.rb', line 59

def rollback; end

#setupObject



55
# File 'lib/rest_my_case/base.rb', line 55

def setup;  end

#skipObject



93
94
95
# File 'lib/rest_my_case/base.rb', line 93

def skip
  options[:should_skip] = true
end

#skip!Object



97
98
99
# File 'lib/rest_my_case/base.rb', line 97

def skip!
  skip && fail(Errors::Skip)
end