Class: RailsLens::AnnotationPipeline

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_lens/annotation_pipeline.rb

Overview

Manages the pipeline of content providers for generating annotations

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAnnotationPipeline

Returns a new instance of AnnotationPipeline.



8
9
10
11
# File 'lib/rails_lens/annotation_pipeline.rb', line 8

def initialize
  @providers = []
  register_default_providers
end

Instance Attribute Details

#providersObject (readonly)

Returns the value of attribute providers.



6
7
8
# File 'lib/rails_lens/annotation_pipeline.rb', line 6

def providers
  @providers
end

Class Method Details

.accumulate_result(results, provider, result) ⇒ Object

Route a single provider’s result into the accumulator hash based on the provider’s declared type. Shared by the pipeline and AnnotationManager.



25
26
27
28
29
30
31
32
33
34
# File 'lib/rails_lens/annotation_pipeline.rb', line 25

def self.accumulate_result(results, provider, result)
  case provider.type
  when :schema
    results[:schema] = result
  when :section
    results[:sections] << result if result
  when :notes
    results[:notes].concat(Array(result))
  end
end

Instance Method Details

#process(model_class) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/rails_lens/annotation_pipeline.rb', line 36

def process(model_class)
  results = {
    schema: nil,
    sections: [],
    notes: []
  }

  # Use the model's connection pool to manage a single connection for all providers
  model_class.connection_pool.with_connection do |connection|
    @providers.each do |provider|
      next unless provider.applicable?(model_class)

      begin
        result = provider.process(model_class, connection)

        self.class.accumulate_result(results, provider, result)
      rescue ActiveRecord::StatementInvalid => e
        warn "Provider #{provider.class} database error for #{model_class}: #{e.message}"
      rescue ActiveRecord::ConnectionNotDefined => e
        warn "Provider #{provider.class} connection error for #{model_class}: #{e.message}"
      rescue NameError, NoMethodError => e
        warn "Provider #{provider.class} method error for #{model_class}: #{e.message}"
      rescue RailsLens::Error => e
        warn "Provider #{provider.class} rails_lens error for #{model_class}: #{e.message}"
      rescue StandardError => e
        warn "Provider #{provider.class} unexpected error for #{model_class}: #{e.message}"
      end
    end
  end

  results
end

#register(provider) ⇒ Object



13
14
15
# File 'lib/rails_lens/annotation_pipeline.rb', line 13

def register(provider)
  @providers << provider
end

#unregister(provider_class) ⇒ Object



17
18
19
# File 'lib/rails_lens/annotation_pipeline.rb', line 17

def unregister(provider_class)
  @providers.reject! { |p| p.is_a?(provider_class) }
end