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.



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

def adaptor
  @adaptor
end

#after_operationsObject (readonly)

Returns the value of attribute after_operations.



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

def after_operations
  @after_operations
end

#before_operationsObject (readonly)

Returns the value of attribute before_operations.



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

def before_operations
  @before_operations
end

#operationsObject (readonly)

Returns the value of attribute operations.



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

def operations
  @operations
end

#originalObject (readonly)

Returns the value of attribute original.



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

def original
  @original
end

#resourceObject (readonly)

Returns the value of attribute resource.



46
47
48
# File 'lib/ledger_sync/adaptors/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/adaptors/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/adaptors/operation.rb', line 46

def response
  @response
end

#resultObject (readonly)

Returns the value of attribute result.



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

def result
  @result
end

#root_operationObject (readonly)

Returns the value of attribute root_operation.



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

def root_operation
  @root_operation
end

Class Method Details

.included(base) ⇒ Object



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

def self.included(base)
  base.include SimplySerializable::Mixin
  base.include Fingerprintable::Mixin
  # base.include Validatable
  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



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

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

  true
end

#add_after_operation(operation) ⇒ Object



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

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

#add_before_operation(operation) ⇒ Object



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

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

#add_root_operation(operation) ⇒ Object



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

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

#errorsObject



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

def errors
  validate.validator.errors
end

#failure(error, resource: nil) ⇒ Object

Results



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

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

#failure?Boolean

Returns:

  • (Boolean)


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

def failure?
  result.failure?
end

#initialize(adaptor:, resource:) ⇒ Object



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

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



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

def ledger_serializer
  @ledger_serializer ||= begin
    modules = self.class.name.split('::Operations::').first
    Object.const_get("#{modules}::LedgerSerializer").new(resource: resource)
  end
end

#performObject



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/ledger_sync/adaptors/operation.rb', line 88

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)


105
106
107
# File 'lib/ledger_sync/adaptors/operation.rb', line 105

def performed?
  @performed == true
end

#success(resource:, response:) ⇒ Object



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

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

#success?Boolean

Returns:

  • (Boolean)


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

def success?
  result.success?
end

#valid?Boolean

Returns:

  • (Boolean)


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

def valid?
  validate.success?
end

#validateObject



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

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



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

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