Class: Textus::Application::Policy::Promotion

Inherits:
Object
  • Object
show all
Defined in:
lib/textus/application/policy/promotion.rb

Defined Under Namespace

Classes: Result

Constant Summary collapse

REGISTRY =
{
  "schema_valid" => -> { Predicates::SchemaValid.new },
  "accept_authority_signed" => -> { Predicates::AcceptAuthoritySigned.new },
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(predicates:) ⇒ Promotion

Returns a new instance of Promotion.



27
28
29
# File 'lib/textus/application/policy/promotion.rb', line 27

def initialize(predicates:)
  @predicates = predicates
end

Instance Attribute Details

#predicatesObject (readonly)

Returns the value of attribute predicates.



25
26
27
# File 'lib/textus/application/policy/promotion.rb', line 25

def predicates
  @predicates
end

Class Method Details

.from_names(names) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/textus/application/policy/promotion.rb', line 15

def self.from_names(names)
  predicates = Array(names).map do |n|
    ctor = REGISTRY[n.to_s] or raise Textus::UsageError.new(
      "unknown promotion predicate: '#{n}' (known: #{REGISTRY.keys.join(", ")})",
    )
    ctor.call
  end
  new(predicates: predicates)
end

Instance Method Details

#evaluate(entry:, schemas:, manifest:, role:) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/textus/application/policy/promotion.rb', line 35

def evaluate(entry:, schemas:, manifest:, role:)
  reasons = []
  @predicates.each do |pred|
    ok = invoke(pred, entry: entry, schemas: schemas, manifest: manifest, role: role)
    reasons << "#{pred.name}: #{pred.reason || "predicate failed"}" unless ok
  end
  Result.new(ok?: reasons.empty?, reasons: reasons)
end

#predicate_namesObject



31
32
33
# File 'lib/textus/application/policy/promotion.rb', line 31

def predicate_names
  @predicates.map(&:name)
end