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.



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

def params
  @params
end

#resultObject (readonly)

Returns the value of attribute result.



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

def result
  @result
end

Class Method Details

.included(base) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/ledger_sync/domains/operation.rb', line 87

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



264
265
266
267
268
269
# File 'lib/ledger_sync/domains/operation.rb', line 264

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

  true
end

#allowed?Boolean

Returns:

  • (Boolean)


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

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

  local_domain == @domain
end

#deep_serialize(value) ⇒ Object



230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/ledger_sync/domains/operation.rb', line 230

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



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

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

#errorsObject



258
259
260
# File 'lib/ledger_sync/domains/operation.rb', line 258

def errors
  validate.validator.errors
end

#failure(error) ⇒ Object



216
217
218
219
220
221
222
223
224
# File 'lib/ledger_sync/domains/operation.rb', line 216

def failure(error)
  unless error.is_a?(Exception)
    error = LedgerSync::Domains::UnspecifiedError.new(operation: self, error: error)
  end

  @result = LedgerSync::Domains::OperationResult.Failure(error, operation: self)
  failure_callback
  @result
end

#failure?Boolean

Returns:

  • (Boolean)


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

def failure?
  result.failure?
end

#failure_callbackObject



228
# File 'lib/ledger_sync/domains/operation.rb', line 228

def failure_callback; end

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



103
104
105
106
107
108
109
110
111
# File 'lib/ledger_sync/domains/operation.rb', line 103

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

  validation_contract_class.new.schema.key_map.each do |key|
    define_singleton_method(key.name) { params[key.name.to_sym] }
  end
end

#local_domainObject



191
192
193
194
195
# File 'lib/ledger_sync/domains/operation.rb', line 191

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

#performObject

rubocop:disable Metrics/MethodLength



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/ledger_sync/domains/operation.rb', line 113

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::Domains::PerformedOperationError.new(
        operation: self
      )
    )
  end

  unless valid?
    return failure(
      LedgerSync::Domains::ValidationError.new(
        operation: self,
        errors: errors
      )
    )
  end

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

  @result
end

#performed?Boolean

Returns:

  • (Boolean)


159
160
161
# File 'lib/ledger_sync/domains/operation.rb', line 159

def performed?
  @performed == true
end

#serialize(resource:) ⇒ Object



163
164
165
166
167
168
# File 'lib/ledger_sync/domains/operation.rb', line 163

def serialize(resource:)
  serializer = serializer_for(resource: resource)
  return resource unless serializer

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

#serializer_class_name_for(resource:) ⇒ Object



176
177
178
179
180
181
# File 'lib/ledger_sync/domains/operation.rb', line 176

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

#serializer_for(resource:) ⇒ Object



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

def serializer_for(resource:)
  return unless Object.const_defined?(serializer_class_name_for(resource: resource))

  Object.const_get(serializer_class_name_for(resource: resource))
end

#serializer_module_for(resource:) ⇒ Object



183
184
185
# File 'lib/ledger_sync/domains/operation.rb', line 183

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

#success(value, meta: nil) ⇒ Object



207
208
209
210
211
212
213
214
# File 'lib/ledger_sync/domains/operation.rb', line 207

def success(value, meta: nil)
  @result = LedgerSync::Domains::OperationResult.Success(
    deep_serialize(value),
    operation: self, meta: meta
  )
  success_callback
  @result
end

#success?Boolean

Results

Returns:

  • (Boolean)


199
200
201
# File 'lib/ledger_sync/domains/operation.rb', line 199

def success?
  result.success?
end

#success_callbackObject



226
# File 'lib/ledger_sync/domains/operation.rb', line 226

def success_callback; end

#valid?Boolean

Returns:

  • (Boolean)


243
244
245
# File 'lib/ledger_sync/domains/operation.rb', line 243

def valid?
  validate.success?
end

#validateObject



247
248
249
250
251
252
# File 'lib/ledger_sync/domains/operation.rb', line 247

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

#validation_contract_classObject



254
255
256
# File 'lib/ledger_sync/domains/operation.rb', line 254

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