Class: Textus::Domain::Policy::Promotion

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

Overview

Promotion evaluates a list of named predicates against a pending-proposal entry and returns a Result indicating whether all requirements are met.

Defined Under Namespace

Classes: Result

Constant Summary collapse

REGISTRY =
{
  "schema_valid" => -> { Predicates::SchemaValid.new },
  "human_accept" => -> { Predicates::HumanAccept.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.



26
27
28
# File 'lib/textus/domain/policy/promotion.rb', line 26

def initialize(predicates:)
  @predicates = predicates
end

Instance Attribute Details

#predicatesObject (readonly)

Returns the value of attribute predicates.



24
25
26
# File 'lib/textus/domain/policy/promotion.rb', line 24

def predicates
  @predicates
end

Class Method Details

.from_names(names) ⇒ Object



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

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:, store:) ⇒ Object



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

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

#predicate_namesObject



30
31
32
# File 'lib/textus/domain/policy/promotion.rb', line 30

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