Module: LedgerSync::Adaptors::Operation::Mixin

Defined in:
lib/ledger_sync/adaptors/operation.rb

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#adaptorObject (readonly)

Returns the value of attribute adaptor.



35
36
37
# File 'lib/ledger_sync/adaptors/operation.rb', line 35

def adaptor
  @adaptor
end

#resourceObject (readonly)

Returns the value of attribute resource.



35
36
37
# File 'lib/ledger_sync/adaptors/operation.rb', line 35

def resource
  @resource
end

#resource_before_performObject (readonly)

Returns the value of attribute resource_before_perform.



35
36
37
# File 'lib/ledger_sync/adaptors/operation.rb', line 35

def resource_before_perform
  @resource_before_perform
end

#responseObject (readonly)

Returns the value of attribute response.



35
36
37
# File 'lib/ledger_sync/adaptors/operation.rb', line 35

def response
  @response
end

#resultObject (readonly)

Returns the value of attribute result.



35
36
37
# File 'lib/ledger_sync/adaptors/operation.rb', line 35

def result
  @result
end

Class Method Details

.included(base) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/ledger_sync/adaptors/operation.rb', line 17

def self.included(base)
  base.include SimplySerializable::Mixin
  base.include Fingerprintable::Mixin
  base.include Error::HelpersMixin
  base.include Adaptors::Mixins::InferLedgerSerializerMixin
  base.include Adaptors::Mixins::InferValidationContractMixin
  base.extend ClassMethods

  base.class_eval do
    serialize only: %i[
      adaptor
      resource
      result
      response
    ]
  end
end

Instance Method Details

#==(other) ⇒ Object

Comparison



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

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

  true
end

#errorsObject



147
148
149
# File 'lib/ledger_sync/adaptors/operation.rb', line 147

def errors
  validate.validator.errors
end

#failure(error, resource: nil) ⇒ Object

Results



97
98
99
100
101
102
103
104
105
# File 'lib/ledger_sync/adaptors/operation.rb', line 97

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

#failure?Boolean

Returns:

  • (Boolean)


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

def failure?
  result.failure?
end

#initialize(**keywords) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/ledger_sync/adaptors/operation.rb', line 41

def initialize(
  **keywords
)
  @adaptor = keywords.fetch(:adaptor)
  @ledger_deserializer_class = keywords.fetch(:ledger_deserializer_class, nil)
  @ledger_serializer_class = keywords.fetch(:ledger_serializer_class, nil)
  @resource = keywords.fetch(:resource)
  @resource_before_perform = resource.dup
  @result = nil
  @validation_contract = keywords.fetch(:validation_contract, nil)

  self.class.raise_if_unexpected_class(expected: self.class.inferred_resource_class, given: @resource.class)
  self.class.raise_if_unexpected_class(expected: LedgerSync::Adaptors::LedgerSerializer, given: ledger_deserializer_class) unless @ledger_deserializer_class.nil?
  self.class.raise_if_unexpected_class(expected: LedgerSync::Adaptors::LedgerSerializer, given: ledger_serializer_class) unless @ledger_serializer_class.nil?
  self.class.raise_if_unexpected_class(expected: LedgerSync::Adaptors::Contract, given: validation_contract) unless @validation_contract.nil?
end

#ledger_deserializerObject



79
80
81
# File 'lib/ledger_sync/adaptors/operation.rb', line 79

def ledger_deserializer
  ledger_deserializer_class.new(resource: resource)
end

#ledger_deserializer_classObject



83
84
85
# File 'lib/ledger_sync/adaptors/operation.rb', line 83

def ledger_deserializer_class
  @ledger_deserializer_class ||= self.class.inferred_ledger_deserializer_class
end

#ledger_serializerObject



87
88
89
# File 'lib/ledger_sync/adaptors/operation.rb', line 87

def ledger_serializer
  ledger_serializer_class.new(resource: resource)
end

#ledger_serializer_classObject



91
92
93
# File 'lib/ledger_sync/adaptors/operation.rb', line 91

def ledger_serializer_class
  @ledger_serializer_class ||= self.class.inferred_ledger_serializer_class
end

#performObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/ledger_sync/adaptors/operation.rb', line 58

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 = adaptor.parse_operation_error(error: e, operation: self)
              raise e unless parsed_error

              failure(parsed_error)
            ensure
              @performed = true
  end
end

#performed?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/ledger_sync/adaptors/operation.rb', line 75

def performed?
  @performed == true
end

#success(resource:, response:) ⇒ Object



111
112
113
114
115
116
117
118
119
# File 'lib/ledger_sync/adaptors/operation.rb', line 111

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

#success?Boolean

Returns:

  • (Boolean)


121
122
123
# File 'lib/ledger_sync/adaptors/operation.rb', line 121

def success?
  result.success?
end

#valid?Boolean

Returns:

  • (Boolean)


125
126
127
# File 'lib/ledger_sync/adaptors/operation.rb', line 125

def valid?
  validate.success?
end

#validateObject



129
130
131
132
133
134
# File 'lib/ledger_sync/adaptors/operation.rb', line 129

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

#validation_contractObject



136
137
138
# File 'lib/ledger_sync/adaptors/operation.rb', line 136

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

#validation_dataObject



140
141
142
143
144
145
# File 'lib/ledger_sync/adaptors/operation.rb', line 140

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