Class: Ast::Merge::Runtime::Delegate

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

Overview

Runtime-facing adapter describing one merge implementation that can own and potentially execute merge work for one or more surface families.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, priority: 0, surface_kinds: nil, languages: nil, feature_profile: nil, capabilities: {}, supports_surface: nil, discover_child_surfaces: nil, merge: nil, metadata: {}) ⇒ Delegate

Returns a new instance of Delegate.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/ast/merge/runtime/delegate.rb', line 17

def initialize(
  name:,
  priority: 0,
  surface_kinds: nil,
  languages: nil,
  feature_profile: nil,
  capabilities: {},
  supports_surface: nil,
  discover_child_surfaces: nil,
  merge: nil,
  metadata: {}
)
  @name = name.to_s
  @priority = Integer(priority)
  @surface_kinds = normalize_symbols(surface_kinds)
  @languages = normalize_symbols(languages)
  @feature_profile = feature_profile
  @capabilities = normalize_capabilities(capabilities)
  @supports_surface = supports_surface
  @discover_child_surfaces = discover_child_surfaces
  @merge = merge
  @metadata = .dup.freeze
end

Instance Attribute Details

#capabilitiesObject (readonly)

Returns the value of attribute capabilities.



9
10
11
# File 'lib/ast/merge/runtime/delegate.rb', line 9

def capabilities
  @capabilities
end

#feature_profileObject (readonly)

Returns the value of attribute feature_profile.



9
10
11
# File 'lib/ast/merge/runtime/delegate.rb', line 9

def feature_profile
  @feature_profile
end

#languagesObject (readonly)

Returns the value of attribute languages.



9
10
11
# File 'lib/ast/merge/runtime/delegate.rb', line 9

def languages
  @languages
end

#metadataObject (readonly)

Returns the value of attribute metadata.



9
10
11
# File 'lib/ast/merge/runtime/delegate.rb', line 9

def 
  @metadata
end

#nameObject (readonly)

Returns the value of attribute name.



9
10
11
# File 'lib/ast/merge/runtime/delegate.rb', line 9

def name
  @name
end

#priorityObject (readonly)

Returns the value of attribute priority.



9
10
11
# File 'lib/ast/merge/runtime/delegate.rb', line 9

def priority
  @priority
end

#surface_kindsObject (readonly)

Returns the value of attribute surface_kinds.



9
10
11
# File 'lib/ast/merge/runtime/delegate.rb', line 9

def surface_kinds
  @surface_kinds
end

Instance Method Details

#capability_supported?(capability, surface = nil) ⇒ Boolean

Returns:

  • (Boolean)


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/ast/merge/runtime/delegate.rb', line 53

def capability_supported?(capability, surface = nil)
  normalized_capability = capability.to_sym
  rule = capabilities[normalized_capability]

  if rule.nil?
    return !@merge.nil? if normalized_capability == :merge
    return !@discover_child_surfaces.nil? if normalized_capability == :discover_child_surfaces

    return false
  end

  return rule if [true, false].include?(rule)
  return true unless surface

  Array(rule).include?(surface.surface_kind)
end

#discover_child_surfaces(operation:, session:) ⇒ Object



70
71
72
73
74
75
# File 'lib/ast/merge/runtime/delegate.rb', line 70

def discover_child_surfaces(operation:, session:)
  return [] unless capability_supported?(:discover_child_surfaces, operation.surface)
  return [] unless @discover_child_surfaces

  Array(@discover_child_surfaces.call(operation: operation, session: session))
end

#merge(operation:, session:) ⇒ Object

Raises:

  • (NotImplementedError)


77
78
79
80
81
82
83
84
85
86
87
# File 'lib/ast/merge/runtime/delegate.rb', line 77

def merge(operation:, session:)
  raise NotImplementedError, "Delegate #{name.inspect} does not expose merge execution" unless @merge
  unless capability_supported?(
    :merge, operation.surface
  )
    raise NotImplementedError,
          "Delegate #{name.inspect} does not support merge for #{operation.surface.surface_kind}"
  end

  @merge.call(operation: operation, session: session)
end

#supports?(surface) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
44
45
46
47
48
49
50
51
# File 'lib/ast/merge/runtime/delegate.rb', line 41

def supports?(surface)
  return false unless surface
  return false if surface_kinds.any? && !surface_kinds.include?(surface.surface_kind)

  surface_languages = [surface.effective_language, surface.declared_language].compact
  return false if languages.any? && (surface_languages & languages).empty?

  return true unless @supports_surface

  @supports_surface.call(surface)
end

#to_hObject



89
90
91
92
93
94
95
96
97
98
99
# File 'lib/ast/merge/runtime/delegate.rb', line 89

def to_h
  {
    name: name,
    priority: priority,
    surface_kinds: surface_kinds,
    languages: languages,
    feature_profile: feature_profile.respond_to?(:to_h) ? feature_profile.to_h : feature_profile,
    capabilities: capabilities.transform_values { |value| value.is_a?(Array) ? value.dup : value },
    metadata: 
  }
end