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

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

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.



44
45
46
# File 'lib/ledger_sync/ledgers/operation.rb', line 44

def client
  @client
end

#resourceObject (readonly)

Returns the value of attribute resource.



44
45
46
# File 'lib/ledger_sync/ledgers/operation.rb', line 44

def resource
  @resource
end

#resource_before_performObject (readonly)

Returns the value of attribute resource_before_perform.



44
45
46
# File 'lib/ledger_sync/ledgers/operation.rb', line 44

def resource_before_perform
  @resource_before_perform
end

#responseObject (readonly)

Returns the value of attribute response.



44
45
46
# File 'lib/ledger_sync/ledgers/operation.rb', line 44

def response
  @response
end

#resultObject (readonly)

Returns the value of attribute result.



44
45
46
# File 'lib/ledger_sync/ledgers/operation.rb', line 44

def result
  @result
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
# File 'lib/ledger_sync/ledgers/operation.rb', line 25

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



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

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

  true
end

#deserializerObject



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

def deserializer
  @deserializer ||= deserializer_class.new
end

#deserializer_classObject



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

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

#errorsObject



152
153
154
# File 'lib/ledger_sync/ledgers/operation.rb', line 152

def errors
  validate.validator.errors
end

#failure(error, resource: nil) ⇒ Object

Results



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

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

#failure?Boolean

Returns:

  • (Boolean)


112
113
114
# File 'lib/ledger_sync/ledgers/operation.rb', line 112

def failure?
  result.failure?
end

#initialize(args = {}) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/ledger_sync/ledgers/operation.rb', line 50

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)
  self.class.raise_if_unexpected_class(expected: LedgerSync::Ledgers::Contract, given: validation_contract) unless @validation_contract.nil?
end

#performObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/ledger_sync/ledgers/operation.rb', line 63

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)


80
81
82
# File 'lib/ledger_sync/ledgers/operation.rb', line 80

def performed?
  @performed == true
end

#serializerObject



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

def serializer
  @serializer ||= deserializer_class.new
end

#serializer_classObject



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

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

#success(resource:, response:) ⇒ Object



116
117
118
119
120
121
122
123
124
# File 'lib/ledger_sync/ledgers/operation.rb', line 116

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

#success?Boolean

Returns:

  • (Boolean)


126
127
128
# File 'lib/ledger_sync/ledgers/operation.rb', line 126

def success?
  result.success?
end

#valid?Boolean

Returns:

  • (Boolean)


130
131
132
# File 'lib/ledger_sync/ledgers/operation.rb', line 130

def valid?
  validate.success?
end

#validateObject



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

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

#validation_contractObject



141
142
143
# File 'lib/ledger_sync/ledgers/operation.rb', line 141

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

#validation_dataObject



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

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