Class: StandardLedger::Result

Inherits:
Object
  • Object
show all
Defined in:
lib/standard_ledger/result.rb

Overview

The gem’s default result type, returned by ‘StandardLedger.post` and `StandardLedger.rebuild!` when the host has not configured an adapter for its own Result class.

Hosts with their own Result type (e.g. ‘ApplicationOperation::Result`) register a translator via `StandardLedger.config.result_adapter` so the gem returns the host’s type instead — see ‘Config#result_adapter`.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(success:, value: nil, errors: [], entry: nil, idempotent: false, projections: {}) ⇒ Result

Returns a new instance of Result.

Parameters:

  • success (Boolean)
  • value (Object, nil) (defaults to: nil)

    typically the persisted entry, or whatever the host operation wishes to surface.

  • errors (Array<String>) (defaults to: [])

    human-readable error messages.

  • entry (ActiveRecord::Base, nil) (defaults to: nil)

    the persisted entry record.

  • idempotent (Boolean) (defaults to: false)

    true when the create was a no-op because an existing row already satisfied the idempotency key.

  • projections (Hash) (defaults to: {})

    split by mode: ‘{ inline: […], async: […], matview: […] }`.



20
21
22
23
24
25
26
27
# File 'lib/standard_ledger/result.rb', line 20

def initialize(success:, value: nil, errors: [], entry: nil, idempotent: false, projections: {})
  @success = success
  @value = value
  @errors = errors
  @entry = entry
  @idempotent = idempotent
  @projections = projections
end

Instance Attribute Details

#entryObject (readonly)

Returns the value of attribute entry.



10
11
12
# File 'lib/standard_ledger/result.rb', line 10

def entry
  @entry
end

#errorsObject (readonly)

Returns the value of attribute errors.



10
11
12
# File 'lib/standard_ledger/result.rb', line 10

def errors
  @errors
end

#projectionsObject (readonly)

Returns the value of attribute projections.



10
11
12
# File 'lib/standard_ledger/result.rb', line 10

def projections
  @projections
end

#valueObject (readonly)

Returns the value of attribute value.



10
11
12
# File 'lib/standard_ledger/result.rb', line 10

def value
  @value
end

Class Method Details

.failure(errors:, entry: nil, projections: {}) ⇒ Object



47
48
49
# File 'lib/standard_ledger/result.rb', line 47

def self.failure(errors:, entry: nil, projections: {})
  new(success: false, errors: Array(errors), entry: entry, projections: projections)
end

.success(entry:, idempotent: false, projections: {}) ⇒ Object

Build a successful result. Convenience for ‘StandardLedger.post` and internal callers; not intended as the host’s primary construction path.



43
44
45
# File 'lib/standard_ledger/result.rb', line 43

def self.success(entry:, idempotent: false, projections: {})
  new(success: true, value: entry, entry: entry, idempotent: idempotent, projections: projections)
end

Instance Method Details

#failure?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/standard_ledger/result.rb', line 33

def failure?
  !@success
end

#idempotent?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/standard_ledger/result.rb', line 37

def idempotent?
  @idempotent
end

#success?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/standard_ledger/result.rb', line 29

def success?
  @success
end