Module: LedgerSync::Adaptors::Operation::Mixin
- Defined in:
- lib/ledger_sync/adaptors/operation.rb
Defined Under Namespace
Modules: ClassMethods
Instance Attribute Summary collapse
-
#adaptor ⇒ Object
readonly
Returns the value of attribute adaptor.
-
#after_operations ⇒ Object
readonly
Returns the value of attribute after_operations.
-
#before_operations ⇒ Object
readonly
Returns the value of attribute before_operations.
-
#operations ⇒ Object
readonly
Returns the value of attribute operations.
-
#original ⇒ Object
readonly
Returns the value of attribute original.
-
#resource ⇒ Object
readonly
Returns the value of attribute resource.
-
#resource_before_perform ⇒ Object
readonly
Returns the value of attribute resource_before_perform.
-
#response ⇒ Object
readonly
Returns the value of attribute response.
-
#result ⇒ Object
readonly
Returns the value of attribute result.
-
#root_operation ⇒ Object
readonly
Returns the value of attribute root_operation.
Class Method Summary collapse
Instance Method Summary collapse
-
#==(other) ⇒ Object
Comparison.
- #add_after_operation(operation) ⇒ Object
- #add_before_operation(operation) ⇒ Object
- #add_root_operation(operation) ⇒ Object
- #errors ⇒ Object
-
#failure(error, resource: nil) ⇒ Object
Results.
- #failure? ⇒ Boolean
- #initialize(adaptor:, resource:) ⇒ Object
- #ledger_serializer ⇒ Object
- #perform ⇒ Object
- #performed? ⇒ Boolean
- #success(resource:, response:) ⇒ Object
- #success? ⇒ Boolean
- #valid? ⇒ Boolean
- #validate ⇒ Object
- #validation_data ⇒ Object
Instance Attribute Details
#adaptor ⇒ Object (readonly)
Returns the value of attribute adaptor.
50 51 52 |
# File 'lib/ledger_sync/adaptors/operation.rb', line 50 def adaptor @adaptor end |
#after_operations ⇒ Object (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_operations ⇒ Object (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 |
#operations ⇒ Object (readonly)
Returns the value of attribute operations.
50 51 52 |
# File 'lib/ledger_sync/adaptors/operation.rb', line 50 def operations @operations end |
#original ⇒ Object (readonly)
Returns the value of attribute original.
50 51 52 |
# File 'lib/ledger_sync/adaptors/operation.rb', line 50 def original @original end |
#resource ⇒ Object (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_perform ⇒ Object (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 |
#response ⇒ Object (readonly)
Returns the value of attribute response.
50 51 52 |
# File 'lib/ledger_sync/adaptors/operation.rb', line 50 def response @response end |
#result ⇒ Object (readonly)
Returns the value of attribute result.
50 51 52 |
# File 'lib/ledger_sync/adaptors/operation.rb', line 50 def result @result end |
#root_operation ⇒ Object (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 |
#errors ⇒ Object
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
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_serializer ⇒ Object
113 114 115 |
# File 'lib/ledger_sync/adaptors/operation.rb', line 113 def ledger_serializer self.class.inferred_ledger_serializer(resource: resource) end |
#perform ⇒ Object
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
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
143 144 145 |
# File 'lib/ledger_sync/adaptors/operation.rb', line 143 def success? result.success? end |
#valid? ⇒ Boolean
147 148 149 |
# File 'lib/ledger_sync/adaptors/operation.rb', line 147 def valid? validate.success? end |
#validate ⇒ Object
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_data ⇒ Object
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 |