Module: LedgerSync::Ledgers::Operation::Mixin

Defined in:
lib/ledger_sync/ledgers/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

#clientObject (readonly)

Returns the value of attribute client.



46
47
48
# File 'lib/ledger_sync/ledgers/operation.rb', line 46

def client
  @client
end

#resourceObject (readonly)

Returns the value of attribute resource.



46
47
48
# File 'lib/ledger_sync/ledgers/operation.rb', line 46

def resource
  @resource
end

#resource_before_performObject (readonly)

Returns the value of attribute resource_before_perform.



46
47
48
# File 'lib/ledger_sync/ledgers/operation.rb', line 46

def resource_before_perform
  @resource_before_perform
end

#responseObject (readonly)

Returns the value of attribute response.



46
47
48
# File 'lib/ledger_sync/ledgers/operation.rb', line 46

def response
  @response
end

#resultObject (readonly)

Returns the value of attribute result.



46
47
48
# File 'lib/ledger_sync/ledgers/operation.rb', line 46

def result
  @result
end

Class Method Details

.included(base) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/ledger_sync/ledgers/operation.rb', line 27

def self.included(base)
  base.include SimplySerializable::Mixin
  base.include Fingerprintable::Mixin
  base.include Error::HelpersMixin
  base.include Ledgers::Mixins::InferSerializerMixin
  base.include Ledgers::Mixins::SerializationMixin
  base.include Ledgers::Mixins::InferValidationContractMixin
  base.extend ClassMethods

  base.class_eval do
    simply_serialize only: %i[
      client
      resource
      result
      response
    ]
  end
end

Instance Method Details

#==(other) ⇒ Object

Comparison



160
161
162
163
164
165
# File 'lib/ledger_sync/ledgers/operation.rb', line 160

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

  true
end

#deserializerObject



88
89
90
# File 'lib/ledger_sync/ledgers/operation.rb', line 88

def deserializer
  @deserializer ||= deserializer_class.new
end

#deserializer_classObject



92
93
94
# File 'lib/ledger_sync/ledgers/operation.rb', line 92

def deserializer_class
  @deserializer_class ||= self.class.inferred_deserializer_class
end

#errorsObject



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

def errors
  validate.validator.errors
end

#failure(error, resource: nil, response: nil) ⇒ Object

Results



106
107
108
109
110
111
112
113
# File 'lib/ledger_sync/ledgers/operation.rb', line 106

def failure(error, resource: nil, response: nil)
  @result = LedgerSync::OperationResult.Failure(
    error,
    operation: self,
    resource: resource,
    response: response
  )
end

#failure?Boolean

Returns:

  • (Boolean)


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

def failure?
  result.failure?
end

#initialize(args = {}) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/ledger_sync/ledgers/operation.rb', line 52

def initialize(args = {})
  @client = args.fetch(:client)
  @deserializer = args.fetch(:deserializer, nil)
  @resource = args.fetch(:resource)
  @serializer = args.fetch(:serializer, nil)
  @resource_before_perform = resource.dup
  @result = nil
  @validation_contract = args.fetch(:validation_contract, nil)

  # self.class.raise_if_unexpected_class(expected: self.class.inferred_resource_class, given: @resource.class)
  return if @validation_contract.nil?

  self.class.raise_if_unexpected_class(expected: LedgerSync::Ledgers::Contract, given: validation_contract)
end

#performObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/ledger_sync/ledgers/operation.rb', line 67

def perform
  failure(LedgerSync::Error::OperationError::PerformedOperationError.new(operation: self)) if @performed

  @result = begin
    operate
  rescue LedgerSync::Error => e
    failure(e)
  rescue StandardError => e
    parsed_error = client.parse_operation_error(error: e, operation: self)
    raise e unless parsed_error

    failure(parsed_error)
  ensure
    @performed = true
  end
end

#performed?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/ledger_sync/ledgers/operation.rb', line 84

def performed?
  @performed == true
end

#serializerObject



96
97
98
# File 'lib/ledger_sync/ledgers/operation.rb', line 96

def serializer
  @serializer ||= deserializer_class.new
end

#serializer_classObject



100
101
102
# File 'lib/ledger_sync/ledgers/operation.rb', line 100

def serializer_class
  @serializer_class ||= self.class.inferred_serializer_class
end

#success(resource:, response:) ⇒ Object



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

def success(resource:, response:)
  @result = LedgerSync::OperationResult.Success(
    self,
    operation: self,
    resource: resource,
    response: response
  )
end

#success?Boolean

Returns:

  • (Boolean)


128
129
130
# File 'lib/ledger_sync/ledgers/operation.rb', line 128

def success?
  result.success?
end

#valid?Boolean

Returns:

  • (Boolean)


132
133
134
# File 'lib/ledger_sync/ledgers/operation.rb', line 132

def valid?
  validate.success?
end

#validateObject



136
137
138
139
140
141
# File 'lib/ledger_sync/ledgers/operation.rb', line 136

def validate
  Util::Validator.new(
    contract: validation_contract,
    data: validation_data
  ).validate
end

#validation_contractObject



143
144
145
# File 'lib/ledger_sync/ledgers/operation.rb', line 143

def validation_contract
  @validation_contract ||= self.class.inferred_validation_contract_class
end

#validation_dataObject



147
148
149
150
151
152
# File 'lib/ledger_sync/ledgers/operation.rb', line 147

def validation_data
  simply_serializer_instance = resource.simply_serializer(
    do_not_serialize_if_class_is: Resource::PRIMITIVES
  )
  simply_serializer_instance.serialize[:objects][simply_serializer_instance.id][:data]
end