Class: ActiveCypher::RailsLensExt::Annotator

Inherits:
Object
  • Object
show all
Defined in:
lib/active_cypher/rails_lens_ext/annotator.rb

Overview

Annotator for ActiveCypher graph models Discovers and annotates Node and Relationship classes Uses RailsLens-compatible TOML format and markers

Constant Summary collapse

MARKER_FORMAT =

Use RailsLens-compatible marker format

'rails-lens:graph'
ANNOTATION_BEGIN =
"# <#{MARKER_FORMAT}:begin>".freeze
ANNOTATION_END =
"# <#{MARKER_FORMAT}:end>".freeze

Class Method Summary collapse

Class Method Details

.annotate_all(options = {}) ⇒ Hash

Annotate all ActiveCypher models

Parameters:

  • options (Hash) (defaults to: {})

    Options for annotation

Options Hash (options):

  • :include_abstract (Boolean)

    Include abstract classes

  • :only (Array<String>)

    Only annotate these models

  • :except (Array<String>)

    Skip these models

Returns:

  • (Hash)

    Results with :annotated, :skipped, :failed keys



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/active_cypher/rails_lens_ext/annotator.rb', line 23

def annotate_all(options = {})
  results = { annotated: [], skipped: [], failed: [] }

  models = discover_models(options)

  models.each do |model|
    result = annotate_model(model, options)
    case result[:status]
    when :annotated
      results[:annotated] << result
    when :skipped
      results[:skipped] << result
    when :failed
      results[:failed] << result
    end
  end

  results
end

.annotate_model(model, _options = {}) ⇒ Hash

Annotate a single model

Parameters:

  • model (Class)

    The model class to annotate

  • options (Hash)

    Options

Returns:

  • (Hash)

    Result with :status, :model, :file, :message keys



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/active_cypher/rails_lens_ext/annotator.rb', line 67

def annotate_model(model, _options = {})
  file_path = model_file_path(model)

  return { status: :skipped, model: model.name, file: nil, message: 'File not found' } unless file_path && File.exist?(file_path)

  extension = Extension.new(model)
  annotation = extension.annotate

  return { status: :skipped, model: model.name, file: file_path, message: 'No annotation generated' } unless annotation

  begin
    write_annotation(file_path, model, annotation)
    { status: :annotated, model: model.name, file: file_path, message: 'Annotated successfully' }
  rescue StandardError => e
    { status: :failed, model: model.name, file: file_path, message: e.message }
  end
end

.remove_all(options = {}) ⇒ Hash

Remove annotations from all ActiveCypher models

Parameters:

  • options (Hash) (defaults to: {})

    Options for removal

Returns:

  • (Hash)

    Results with :removed, :skipped keys



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/active_cypher/rails_lens_ext/annotator.rb', line 46

def remove_all(options = {})
  results = { removed: [], skipped: [] }

  models = discover_models(options.merge(include_abstract: true))

  models.each do |model|
    result = remove_annotation(model)
    if result[:status] == :removed
      results[:removed] << result
    else
      results[:skipped] << result
    end
  end

  results
end

.remove_annotation(model) ⇒ Hash

Remove annotation from a single model

Parameters:

  • model (Class)

    The model class

Returns:

  • (Hash)

    Result with :status, :model, :file keys



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/active_cypher/rails_lens_ext/annotator.rb', line 88

def remove_annotation(model)
  file_path = model_file_path(model)

  return { status: :skipped, model: model.name, file: nil } unless file_path && File.exist?(file_path)

  content = File.read(file_path)

  if content.include?(ANNOTATION_BEGIN)
    new_content = ::RailsLens::FileInsertionHelper.remove_after_frozen_string_literal(
      content, '<rails-lens:graph:begin>', '<rails-lens:graph:end>'
    )
    new_content = new_content.gsub(/\n{3,}/, "\n\n")

    File.write(file_path, new_content)
    { status: :removed, model: model.name, file: file_path }
  else
    { status: :skipped, model: model.name, file: file_path }
  end
end