Module: StandardAudit::Operation::ClassMethods

Defined in:
lib/standard_audit/operation.rb

Instance Method Summary collapse

Instance Method Details

#audit_abstract!Object

Declare that this class is a base or intermediate class rather than a real operation, so the meta-spec skips it.

Usually unnecessary: a class that declares nothing and HAS subclasses is treated as a base automatically, which is what makes a one-line include StandardAudit::Operation on a shared ApplicationOperation work with no further configuration. Use this when the automatic rule is not enough — most often an intermediate class that has no subclasses yet, or one you want to state the intent of explicitly.



137
138
139
# File 'lib/standard_audit/operation.rb', line 137

def audit_abstract!
  @audit_spec = :abstract
end

#audit_none!Object

Declare that this operation mutates state but intentionally records no audit. The accepted reasons, all of which should be left as a comment on the call: delegators that audit downstream, projections/re-indexes derived from already-audited state, outcome-only writes on a record whose creation is already audited, and high-volume telemetry ingestion.



124
125
126
# File 'lib/standard_audit/operation.rb', line 124

def audit_none!
  @audit_spec = :none
end

#audit_specnil, ...

The audit action(s) this operation is expected to emit.

Returns:

  • (nil, :none, :abstract, Array<String>)

    nil — undeclared (the meta-spec forbids this) :none — intentionally records no audit :abstract — not a real operation; excluded from the meta-spec Array — the catalogue action strings it may write



99
100
101
# File 'lib/standard_audit/operation.rb', line 99

def audit_spec
  @audit_spec
end

#audits(*actions) ⇒ Object

Declare the audit action(s) this operation emits (primary first; an operation may emit several — conditional or per-item — and all must be listed). The matching write happens via #audit! inside the operation.

audits "order.created"
audits "order.created", "order.line_item_added"

Values are coerced to Strings, so Symbols and frozen catalogue constants both work. Calling it twice REPLACES the declaration.

Raises:

  • (ArgumentError)


112
113
114
115
116
117
# File 'lib/standard_audit/operation.rb', line 112

def audits(*actions)
  actions = actions.flatten.map(&:to_s)
  raise ArgumentError, "`audits` needs at least one action" if actions.empty?

  @audit_spec = actions
end

#inherited(subclass) ⇒ Object

Subclasses of an adopting base class are the real operations, so they register too — and they do NOT inherit @audit_spec, by design: each leaf states its own intent. A leaf reading nil through a declared parent would be the wrong (and silently passing) answer.



87
88
89
90
# File 'lib/standard_audit/operation.rb', line 87

def inherited(subclass)
  super
  StandardAudit::Operation::Audit.register(subclass)
end