Class: Textus::Manifest::Rules

Inherits:
Object
  • Object
show all
Defined in:
lib/textus/manifest/rules.rb

Defined Under Namespace

Classes: Block, RuleSet

Constant Summary collapse

PICK_FIELDS =

Every structural member here derives from Schema::FIELD_REGISTRY (WS3), so a new rule field is added in one place. ‘in_pick` selects the fields that participate in the most-specific `for(key)` resolution.

Schema::FIELD_REGISTRY.select { |_, m| m[:in_pick] }.keys.freeze
EMPTY_SET =
RuleSet.new(**PICK_FIELDS.to_h { |f| [f, nil] })

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(blocks) ⇒ Rules

Returns a new instance of Rules.



16
17
18
# File 'lib/textus/manifest/rules.rb', line 16

def initialize(blocks)
  @blocks = blocks
end

Instance Attribute Details

#blocksObject (readonly)

Returns the value of attribute blocks.



20
21
22
# File 'lib/textus/manifest/rules.rb', line 20

def blocks
  @blocks
end

Class Method Details

.parse(raw) ⇒ Object



12
13
14
# File 'lib/textus/manifest/rules.rb', line 12

def self.parse(raw)
  new(Array(raw).map { |b| Block.new(b) })
end

Instance Method Details

#explain(key) ⇒ Object



32
33
34
# File 'lib/textus/manifest/rules.rb', line 32

def explain(key)
  @blocks.select { |b| Textus::Domain::Policy::Matcher.matches?(b.match, key) }
end

#for(key) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/textus/manifest/rules.rb', line 22

def for(key)
  slots = PICK_FIELDS.to_h { |f| [f, []] }
  @blocks.each do |b|
    next unless Textus::Domain::Policy::Matcher.matches?(b.match, key)

    slots.each_key { |slot| slots[slot] << b if b.public_send(slot) }
  end
  RuleSet.new(**slots.to_h { |slot, blocks| [slot, pick(blocks, slot, key)] })
end