Class: Ast::Merge::Runtime::Session

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

Overview

Aggregates operations and frames for one top-level merge session.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(policy_context: {}, metadata: {}, delegation_registry: DelegationRegistry.new) ⇒ Session

Returns a new instance of Session.



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

def initialize(policy_context: {}, metadata: {}, delegation_registry: DelegationRegistry.new)
  @policy_context = policy_context.dup.freeze
  @metadata = .dup.freeze
  @delegation_registry = delegation_registry
  @operations_by_id = {}
  @frames_by_operation_id = {}
end

Instance Attribute Details

#delegation_registryObject (readonly)

Returns the value of attribute delegation_registry.



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

def delegation_registry
  @delegation_registry
end

#metadataObject (readonly)

Returns the value of attribute metadata.



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

def 
  @metadata
end

#policy_contextObject (readonly)

Returns the value of attribute policy_context.



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

def policy_context
  @policy_context
end

Instance Method Details

#diagnosticsObject



37
38
39
# File 'lib/ast/merge/runtime/session.rb', line 37

def diagnostics
  operations.flat_map(&:diagnostics)
end

#frame_for(operation_id) ⇒ Object



29
30
31
# File 'lib/ast/merge/runtime/session.rb', line 29

def frame_for(operation_id)
  @frames_by_operation_id[operation_id]
end

#operation(operation_id) ⇒ Object



25
26
27
# File 'lib/ast/merge/runtime/session.rb', line 25

def operation(operation_id)
  @operations_by_id[operation_id]
end

#operation_tree(operation_or_id) ⇒ Object



52
53
54
55
56
57
58
59
60
# File 'lib/ast/merge/runtime/session.rb', line 52

def operation_tree(operation_or_id)
  operation = resolve_operation(operation_or_id)
  return if operation.nil?

  operation.to_h.merge(
    frame: frame_for(operation.operation_id)&.to_h,
    children: operation.children.filter_map { |child| operation_tree(child) }
  )
end

#operation_treesObject



48
49
50
# File 'lib/ast/merge/runtime/session.rb', line 48

def operation_trees
  root_operations.map { |operation| operation_tree(operation) }
end

#operationsObject



33
34
35
# File 'lib/ast/merge/runtime/session.rb', line 33

def operations
  @operations_by_id.values
end

#register(operation, frame:, delegate: nil) ⇒ Object



18
19
20
21
22
23
# File 'lib/ast/merge/runtime/session.rb', line 18

def register(operation, frame:, delegate: nil)
  @operations_by_id[operation.operation_id] = operation
  @frames_by_operation_id[operation.operation_id] = frame
  operation.assign_delegate!(delegate) if delegate
  operation
end

#resolve_delegate_for(surface, capability: nil) ⇒ Object



62
63
64
# File 'lib/ast/merge/runtime/session.rb', line 62

def resolve_delegate_for(surface, capability: nil)
  delegation_registry.resolve(surface, capability: capability)
end

#root_operationsObject



41
42
43
44
45
46
# File 'lib/ast/merge/runtime/session.rb', line 41

def root_operations
  operations.select do |operation|
    frame = frame_for(operation.operation_id)
    frame&.root?
  end
end

#summaryObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/ast/merge/runtime/session.rb', line 66

def summary
  {
    operation_count: operations.size,
    root_operation_count: root_operations.size,
    status_counts: tally_by(operations, &:status),
    diagnostic_count: diagnostics.size,
    diagnostic_severity_counts: tally_by(diagnostics, &:severity),
    delegate_names: normalized_values(operations.map(&:delegate_name)),
    surface_kinds: normalized_values(operations.filter_map { |operation| operation.surface&.surface_kind }),
    effective_languages: normalized_values(operations.filter_map do |operation|
      operation.surface&.effective_language
    end),
    capabilities_used: normalized_values(operations.filter_map do |operation|
      operation.result&.capabilities_used
    end.flatten),
    capabilities_missing: normalized_values(operations.filter_map do |operation|
      operation.result&.capabilities_missing
    end.flatten),
    unresolved_operation_count: operations.count(&:unresolved?),
    unresolved_case_count: operations.sum { |operation| operation.result&.unresolved_cases&.length.to_i }
  }
end

#to_hObject



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

def to_h
  {
    policy_context: policy_context,
    metadata: ,
    summary: summary,
    delegation_registry: delegation_registry.to_h,
    operations: operations.map(&:to_h),
    operation_trees: operation_trees,
    diagnostics: diagnostics.map(&:to_h)
  }
end