Module: LedgerSync::Domains::Operation::Mixin

Included in:
Resource
Defined in:
lib/ledger_sync/domains/operation.rb

Overview

rubocop:disable Metrics/ModuleLength

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#paramsObject (readonly)

Returns the value of attribute params.



61
62
63
# File 'lib/ledger_sync/domains/operation.rb', line 61

def params
  @params
end

#resultObject (readonly)

Returns the value of attribute result.



61
62
63
# File 'lib/ledger_sync/domains/operation.rb', line 61

def result
  @result
end

Class Method Details

.included(base) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/ledger_sync/domains/operation.rb', line 47

def self.included(base)
  base.include SimplySerializable::Mixin
  base.include Fingerprintable::Mixin
  base.include LedgerSync::Error::HelpersMixin
  base.extend ClassMethods

  base.class_eval do
    simply_serialize only: %i[
      params
      result
    ]
  end
end

Instance Method Details

#==(other) ⇒ Object

Comparison



200
201
202
203
204
205
# File 'lib/ledger_sync/domains/operation.rb', line 200

def ==(other)
  return false unless self.class == other.class
  return false unless params == other.params

  true
end

#allowed?Boolean

Returns:

  • (Boolean)


101
102
103
104
105
# File 'lib/ledger_sync/domains/operation.rb', line 101

def allowed?
  return true unless self.class.internal?

  local_domain == @domain
end

#deep_serialize(value) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/ledger_sync/domains/operation.rb', line 158

def deep_serialize(value)
  case value
  when ActiveRecord::Base # , LedgerSync::Resource
    serialize(resource: value)
  when Hash
    value.transform_values { deep_serialize(_1) }
  when Array
    value.map { deep_serialize(_1) }
  else
    value
  end
end

#domainObject



134
135
136
# File 'lib/ledger_sync/domains/operation.rb', line 134

def domain
  LedgerSync::Domains.domains.module_for(domain: @domain)
end

#errorsObject



194
195
196
# File 'lib/ledger_sync/domains/operation.rb', line 194

def errors
  validate.validator.errors
end

#failure(error) ⇒ Object

Results



146
147
148
# File 'lib/ledger_sync/domains/operation.rb', line 146

def failure(error)
  @result = OperationResult.Failure(error)
end

#failure?Boolean

Returns:

  • (Boolean)


150
151
152
# File 'lib/ledger_sync/domains/operation.rb', line 150

def failure?
  result.failure?
end

#inferred_validation_contract_classObject



190
191
192
# File 'lib/ledger_sync/domains/operation.rb', line 190

def inferred_validation_contract_class
  self.class.const_get('Contract')
end

#initialize(domain:, serializer: nil, **params) ⇒ Object



63
64
65
66
67
68
# File 'lib/ledger_sync/domains/operation.rb', line 63

def initialize(domain:, serializer: nil, **params)
  @domain = domain
  @serializer = serializer
  @params = params
  @result = nil
end

#local_domainObject



138
139
140
141
142
# File 'lib/ledger_sync/domains/operation.rb', line 138

def local_domain
  LedgerSync::Domains.domains.domain_for(
    base_module: self.class.to_s.split('::').first.constantize
  )
end

#performObject

rubocop:disable Metrics/MethodLength



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/ledger_sync/domains/operation.rb', line 70

def perform # rubocop:disable Metrics/MethodLength
  unless allowed?
    return failure(
      LedgerSync::Domains::InternalOperationError.new(
        operation: self,
        message: 'Cross-domain operation execution is not allowed'
      )
    )
  end

  if performed?
    return failure(
      LedgerSync::Error::OperationError::PerformedOperationError.new(
        operation: self
      )
    )
  end

  return failure(errors) unless valid?

  @result = begin
    operate
  rescue LedgerSync::Error => e
    failure(e)
  rescue StandardError => e
    failure(e)
  ensure
    @performed = true
  end
end

#performed?Boolean

Returns:

  • (Boolean)


107
108
109
# File 'lib/ledger_sync/domains/operation.rb', line 107

def performed?
  @performed == true
end

#serialize(resource:) ⇒ Object



111
112
113
# File 'lib/ledger_sync/domains/operation.rb', line 111

def serialize(resource:)
  serializer_for(resource: resource).serialize(resource: resource)
end

#serializer_class_for(resource:) ⇒ Object



119
120
121
122
123
124
125
126
# File 'lib/ledger_sync/domains/operation.rb', line 119

def serializer_class_for(resource:)
  Object.const_get(
    [
      serializer_module_for(resource: resource),
      "#{domain}Serializer"
    ].join('::')
  )
end

#serializer_for(resource:) ⇒ Object



115
116
117
# File 'lib/ledger_sync/domains/operation.rb', line 115

def serializer_for(resource:)
  @serializer || serializer_class_for(resource: resource).new
end

#serializer_module_for(resource:) ⇒ Object



128
129
130
131
132
# File 'lib/ledger_sync/domains/operation.rb', line 128

def serializer_module_for(resource:)
  (
    resource.class.try(:serializer_module) || resource.class
  ).to_s.pluralize
end

#success(value, meta: nil) ⇒ Object



154
155
156
# File 'lib/ledger_sync/domains/operation.rb', line 154

def success(value, meta: nil)
  @result = OperationResult.Success(deep_serialize(value), meta: meta)
end

#success?Boolean

Returns:

  • (Boolean)


171
172
173
# File 'lib/ledger_sync/domains/operation.rb', line 171

def success?
  result.success?
end

#valid?Boolean

Returns:

  • (Boolean)


175
176
177
# File 'lib/ledger_sync/domains/operation.rb', line 175

def valid?
  validate.success?
end

#validateObject



179
180
181
182
183
184
# File 'lib/ledger_sync/domains/operation.rb', line 179

def validate
  LedgerSync::Util::Validator.new(
    contract: validation_contract_class,
    data: params
  ).validate
end

#validation_contract_classObject



186
187
188
# File 'lib/ledger_sync/domains/operation.rb', line 186

def validation_contract_class
  @validation_contract_class ||= inferred_validation_contract_class
end