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.



50
51
52
# File 'lib/ledger_sync/adaptors/operation.rb', line 50

def adaptor
  @adaptor
end

#after_operationsObject (readonly)

Returns the value of attribute after_operations.



50
51
52
# File 'lib/ledger_sync/adaptors/operation.rb', line 50

def after_operations
  @after_operations
end

#before_operationsObject (readonly)

Returns the value of attribute before_operations.



50
51
52
# File 'lib/ledger_sync/adaptors/operation.rb', line 50

def before_operations
  @before_operations
end

#operationsObject (readonly)

Returns the value of attribute operations.



50
51
52
# File 'lib/ledger_sync/adaptors/operation.rb', line 50

def operations
  @operations
end

#originalObject (readonly)

Returns the value of attribute original.



50
51
52
# File 'lib/ledger_sync/adaptors/operation.rb', line 50

def original
  @original
end

#resourceObject (readonly)

Returns the value of attribute resource.



50
51
52
# File 'lib/ledger_sync/adaptors/operation.rb', line 50

def resource
  @resource
end

#resource_before_performObject (readonly)

Returns the value of attribute resource_before_perform.



50
51
52
# File 'lib/ledger_sync/adaptors/operation.rb', line 50

def resource_before_perform
  @resource_before_perform
end

#responseObject (readonly)

Returns the value of attribute response.



50
51
52
# File 'lib/ledger_sync/adaptors/operation.rb', line 50

def response
  @response
end

#resultObject (readonly)

Returns the value of attribute result.



50
51
52
# File 'lib/ledger_sync/adaptors/operation.rb', line 50

def result
  @result
end

#root_operationObject (readonly)

Returns the value of attribute root_operation.



50
51
52
# File 'lib/ledger_sync/adaptors/operation.rb', line 50

def root_operation
  @root_operation
end

Class Method Details

.included(base) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/ledger_sync/adaptors/operation.rb', line 29

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

  base.class_eval do
    serialize only: i[
                adaptor
                after_operations
                before_operations
                operations
                resource
                root_operation
                result
                response
                original
              ]
  end
end

Instance Method Details

#==(other) ⇒ Object

Comparison



173
174
175
176
177
178
# File 'lib/ledger_sync/adaptors/operation.rb', line 173

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

  true
end

#add_after_operation(operation) ⇒ Object



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

def add_after_operation(operation)
  @operations << operation
  @after_operations << operation
end

#add_before_operation(operation) ⇒ Object



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

def add_before_operation(operation)
  @operations << operation
  @before_operations << operation
end

#add_root_operation(operation) ⇒ Object



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

def add_root_operation(operation)
  @operations << operation
  @root_operation = operation
end

#errorsObject



167
168
169
# File 'lib/ledger_sync/adaptors/operation.rb', line 167

def errors
  validate.validator.errors
end

#failure(error, resource: nil) ⇒ Object

Results



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

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

#failure?Boolean

Returns:

  • (Boolean)


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

def failure?
  result.failure?
end

#initialize(adaptor:, resource:) ⇒ Object



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

def initialize(adaptor:, resource:)
  raise 'Missing adaptor' if adaptor.nil?
  raise 'Missing resource' if resource.nil?

  raise "#{resource.class.name} is not a valid resource type.  Expected #{self.class.resource_klass.name}" unless resource.is_a?(self.class.resource_klass)

  @adaptor = adaptor
  @after_operations = []
  @before_operations = []
  @operations = []
  @resource = resource
  @resource_before_perform = resource.dup
  @result = nil
  @root_operation = nil
end

#ledger_serializerObject



113
114
115
# File 'lib/ledger_sync/adaptors/operation.rb', line 113

def ledger_serializer
  self.class.inferred_ledger_serializer(resource: resource)
end

#performObject



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/ledger_sync/adaptors/operation.rb', line 92

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

  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)


109
110
111
# File 'lib/ledger_sync/adaptors/operation.rb', line 109

def performed?
  @performed == true
end

#success(resource:, response:) ⇒ Object



133
134
135
136
137
138
139
140
141
# File 'lib/ledger_sync/adaptors/operation.rb', line 133

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

#success?Boolean

Returns:

  • (Boolean)


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

def success?
  result.success?
end

#valid?Boolean

Returns:

  • (Boolean)


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

def valid?
  validate.success?
end

#validateObject



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

def validate
  raise "#{self.class.name}::Contract must be defined to validate." unless self.class.const_defined?('Contract')

  Util::Validator.new(
    contract: self.class::Contract,
    data: validation_data
  ).validate
end

#validation_dataObject



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

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