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.



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

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

#contextObject (readonly)

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



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

def context
  @context
end

#optionsObject (readonly)

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



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

def options
  @options
end

Class Method Details

.context_accessor(*methods) ⇒ Object



44
45
46
47
# File 'lib/rest_my_case/base.rb', line 44

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

.context_reader(*methods) ⇒ Object



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

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

.context_writer(*methods) ⇒ Object



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

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

.dependenciesObject



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

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

.depends(*use_case_classes) ⇒ Object



26
27
28
# File 'lib/rest_my_case/base.rb', line 26

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

.perform(attributes = nil) ⇒ Object



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

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

.required_context(*schema) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/rest_my_case/base.rb', line 12

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_schemaObject



22
23
24
# File 'lib/rest_my_case/base.rb', line 22

def self.required_context_schema
  @required_context_schema ||= {}
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



93
94
95
96
97
98
99
# File 'lib/rest_my_case/base.rb', line 93

def abort
  if options[:silent_abort]
    options[:dependent_use_case].abort
  else
    options[:should_abort] = true
  end
end

#abort!Object



101
102
103
# File 'lib/rest_my_case/base.rb', line 101

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

#error(error_message = '') ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
# File 'lib/rest_my_case/base.rb', line 105

def error(error_message = '')
  if error_message.is_a?(Hash)
    error_data = error_message
  else
    error_data = { message: error_message }
  end

  error_data[:class_name] = self.class.name

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

#error!(error_data = '') ⇒ Object



117
118
119
# File 'lib/rest_my_case/base.rb', line 117

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

#finalObject



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

def final; end

#invoke(*use_case_classes) ⇒ Object



81
82
83
# File 'lib/rest_my_case/base.rb', line 81

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

#invoke!(*use_case_classes) ⇒ Object



85
86
87
88
89
90
91
# File 'lib/rest_my_case/base.rb', line 85

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

#performObject



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

def perform; end

#rollbackObject



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

def rollback; end

#setupObject



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

def setup; end

#skipObject



121
122
123
# File 'lib/rest_my_case/base.rb', line 121

def skip
  options[:should_skip] = true
end

#skip!Object



125
126
127
# File 'lib/rest_my_case/base.rb', line 125

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

#validate_context(schema = self.class.required_context_schema) ⇒ Object



129
130
131
132
133
134
135
# File 'lib/rest_my_case/base.rb', line 129

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



137
138
139
# File 'lib/rest_my_case/base.rb', line 137

def validate_context!(schema = self.class.required_context)
  validate_context(schema) && fail(Errors::Abort)
end