Class: Reeve::ScopeResult

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

Overview

What the scoper hands back to the envelope: the decision, the value the caller may actually receive, and the identifiers the ledger records.

The envelope never reads records out of the tool's own return value — only out of this object — which is how invariant 3 ("no path returns records without scoping") holds by construction rather than by review.

Constant Summary collapse

SCOPED =
"scoped"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(decision:, records: nil, record_type: nil, record_ids: nil, record_count: nil, truncated: false, derived: false) ⇒ ScopeResult

Returns a new instance of ScopeResult.



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/reeve/scope_result.rb', line 63

def initialize(decision:, records: nil, record_type: nil, record_ids: nil,
               record_count: nil, truncated: false, derived: false)
  @decision    = decision
  @derived     = derived ? true : false
  @records     = records
  @record_type = @derived ? nil : record_type
  @record_ids  = (@derived ? [] : Array(record_ids)).map(&:to_s).freeze
  @record_count = record_count || @record_ids.size
  @truncated = truncated ? true : false
  freeze
end

Instance Attribute Details

#decisionObject (readonly)

Returns the value of attribute decision.



13
14
15
# File 'lib/reeve/scope_result.rb', line 13

def decision
  @decision
end

#record_countObject (readonly)

Returns the value of attribute record_count.



13
14
15
# File 'lib/reeve/scope_result.rb', line 13

def record_count
  @record_count
end

#record_idsObject (readonly)

Returns the value of attribute record_ids.



13
14
15
# File 'lib/reeve/scope_result.rb', line 13

def record_ids
  @record_ids
end

#record_typeObject (readonly)

Returns the value of attribute record_type.



13
14
15
# File 'lib/reeve/scope_result.rb', line 13

def record_type
  @record_type
end

#recordsObject (readonly)

Returns the value of attribute records.



13
14
15
# File 'lib/reeve/scope_result.rb', line 13

def records
  @records
end

Class Method Details

.allow(records:, record_type: nil, record_ids: nil, record_count: nil, truncated: false, derived: false, rule: SCOPED) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/reeve/scope_result.rb', line 15

def self.allow(records:, record_type: nil, record_ids: nil, record_count: nil,
               truncated: false, derived: false, rule: SCOPED)
  new(
    decision: Decision.allow(rule: rule),
    records: records,
    record_type: record_type,
    record_ids: record_ids,
    record_count: record_count,
    truncated: truncated,
    derived: derived
  )
end

.deny(rule:, detail: nil) ⇒ Object



28
29
30
# File 'lib/reeve/scope_result.rb', line 28

def self.deny(rule:, detail: nil)
  new(decision: Decision.deny(rule: rule, detail: detail), records: nil)
end

.identifiers_in(records) ⇒ Object



50
51
52
53
54
# File 'lib/reeve/scope_result.rb', line 50

def self.identifiers_in(records)
  Array(records).filter_map { |record| record.id.to_s if record.respond_to?(:id) }
rescue StandardError
  []
end

.type_of(records) ⇒ Object



56
57
58
59
60
61
# File 'lib/reeve/scope_result.rb', line 56

def self.type_of(records)
  first = Array(records).first
  first.respond_to?(:id) ? first.class.name : nil
rescue StandardError
  nil
end

.unscoped(records:, rule:) ⇒ Object

For a result nothing narrowed — the :allow_with_warning path. It still has to say what came back: an entry reading record_count: 0 for a call that returned the whole table is not merely incomplete but false, and this is the one path on which everything came back.



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/reeve/scope_result.rb', line 36

def self.unscoped(records:, rule:)
  identifiers = identifiers_in(records)
  limit = Reeve.config.max_recorded_ids

  new(
    decision: Decision.allow(rule: rule),
    records: records,
    record_type: type_of(records),
    record_ids: identifiers.first(limit),
    record_count: identifiers.size,
    truncated: identifiers.size > limit
  )
end

Instance Method Details

#allowed?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/reeve/scope_result.rb', line 75

def allowed?
  decision.allowed?
end

#denied?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/reeve/scope_result.rb', line 79

def denied?
  decision.denied?
end

#derived?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/reeve/scope_result.rb', line 83

def derived?
  @derived
end

#to_hObject

The ledger-facing half of the entry; Context#to_h supplies the other half.



92
93
94
95
96
97
98
99
100
101
102
# File 'lib/reeve/scope_result.rb', line 92

def to_h
  {
    outcome: decision.outcome.to_s,
    rule: decision.rule,
    record_type: record_type,
    record_ids: record_ids,
    record_count: record_count,
    truncated: truncated?,
    derived: derived?
  }
end

#truncated?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/reeve/scope_result.rb', line 87

def truncated?
  @truncated
end