Class: Ast::Merge::Runtime::DelegationRegistry

Inherits:
Object
  • Object
show all
Defined in:
lib/ast/merge/runtime/delegation_registry.rb

Overview

Registry for selecting a runtime delegate for a surface.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(delegates: [], metadata: {}) ⇒ DelegationRegistry

Returns a new instance of DelegationRegistry.



10
11
12
13
14
# File 'lib/ast/merge/runtime/delegation_registry.rb', line 10

def initialize(delegates: [], metadata: {})
  @delegates = []
  @metadata = .dup.freeze
  Array(delegates).each { |delegate| register(delegate) }
end

Instance Attribute Details

#metadataObject (readonly)

Returns the value of attribute metadata.



8
9
10
# File 'lib/ast/merge/runtime/delegation_registry.rb', line 8

def 
  @metadata
end

Instance Method Details

#delegatesObject



26
27
28
# File 'lib/ast/merge/runtime/delegation_registry.rb', line 26

def delegates
  @delegates.sort_by { |delegate| [-delegate.priority, delegate.name] }
end

#fetch(name) ⇒ Object



22
23
24
# File 'lib/ast/merge/runtime/delegation_registry.rb', line 22

def fetch(name)
  @delegates.find { |delegate| delegate.name == name.to_s }
end

#matching_delegates(surface, capability: nil) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/ast/merge/runtime/delegation_registry.rb', line 30

def matching_delegates(surface, capability: nil)
  delegates.select do |delegate|
    next false unless delegate.supports?(surface)
    next true if capability.nil?

    delegate.capability_supported?(capability, surface)
  end
end

#register(delegate) ⇒ Object



16
17
18
19
20
# File 'lib/ast/merge/runtime/delegation_registry.rb', line 16

def register(delegate)
  @delegates.reject! { |existing| existing.name == delegate.name }
  @delegates << delegate
  delegate
end

#resolve(surface, capability: nil) ⇒ Object



39
40
41
# File 'lib/ast/merge/runtime/delegation_registry.rb', line 39

def resolve(surface, capability: nil)
  matching_delegates(surface, capability: capability).first
end

#to_hObject



43
44
45
46
47
48
# File 'lib/ast/merge/runtime/delegation_registry.rb', line 43

def to_h
  {
    delegates: delegates.map(&:to_h),
    metadata: 
  }
end