Class: Reeve::Testing::Ledger

Inherits:
Object
  • Object
show all
Defined in:
lib/reeve/testing/ledger.rb

Overview

The checks' read-only window onto the audit ledger.

It exists so that require "reeve/testing" stays loadable with no ActiveRecord in the process (FR-020, SC-008): nothing here mentions Reeve::Audit::Entry until a check actually asks a question, and every question has an answer for the case where the ledger is absent.

A host with its own non-ActiveRecord recorder can pass any object with these six methods as ledger: to any ledger-reading check.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.defaultObject



15
16
17
# File 'lib/reeve/testing/ledger.rb', line 15

def self.default
  new
end

Instance Method Details

#available?Boolean

Returns:

  • (Boolean)


19
20
21
22
23
24
25
# File 'lib/reeve/testing/ledger.rb', line 19

def available?
  return false if entry_class.nil?

  entry_class.table_exists?
rescue StandardError
  false
end

#columnsObject



64
65
66
67
68
# File 'lib/reeve/testing/ledger.rb', line 64

def columns
  return [] unless available?

  entry_class.column_names.map(&:to_s)
end

#contract_versionObject



58
59
60
61
62
# File 'lib/reeve/testing/ledger.rb', line 58

def contract_version
  return nil unless entry_class.respond_to?(:contract_version)

  entry_class.contract_version
end

#countObject



54
55
56
# File 'lib/reeve/testing/ledger.rb', line 54

def count
  available? ? entry_class.count : 0
end

#entries_after(marker) ⇒ Object



47
48
49
50
51
52
# File 'lib/reeve/testing/ledger.rb', line 47

def entries_after(marker)
  return [] unless available?

  scope = entry_class.order(:id)
  marker.nil? ? scope.to_a : scope.where("id > ?", marker).to_a
end

#markerObject

A position in the ledger, taken before an invocation so the entries that invocation wrote can be told apart from everything already there.



41
42
43
44
45
# File 'lib/reeve/testing/ledger.rb', line 41

def marker
  return nil unless available?

  entry_class.maximum(:id)
end

#unavailable_reasonObject

Why not, in the words a developer can act on.



28
29
30
31
32
33
34
35
36
37
# File 'lib/reeve/testing/ledger.rb', line 28

def unavailable_reason
  return nil if available?
  if entry_class.nil?
    return "the audit ledger is not loaded — add `require \"reeve/audit\"` to your " \
           "test helper, or pass a `ledger:` of your own"
  end

  "the #{entry_class.table_name} table does not exist — run `rails g reeve:install` " \
    "and migrate"
end