Class: RailsAiBridge::RubydexAdapter::Serializer

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_ai_bridge/rubydex_adapter/serializer.rb

Overview

Converts rubydex API objects to serializable Ruby hashes.

All methods are idempotent, handle missing attributes gracefully, and never raise.

Defined Under Namespace

Classes: DetailedDeclarationMapper

Constant Summary collapse

TYPE_PATTERNS =
{
  'class' => /class/,
  'module' => /module/,
  'method' => /method/,
  'constant' => /constant/
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root) ⇒ Serializer

Returns a new instance of Serializer.

Parameters:

  • root (String)

    the project root directory path



11
12
13
# File 'lib/rails_ai_bridge/rubydex_adapter/serializer.rb', line 11

def initialize(root)
  @root = root
end

Class Method Details

.base_declaration_hash(decl) ⇒ Object



82
83
84
85
86
87
88
# File 'lib/rails_ai_bridge/rubydex_adapter/serializer.rb', line 82

def self.base_declaration_hash(decl)
  {
    name: decl.name,
    unqualified_name: decl.try(:unqualified_name),
    type: declaration_type(decl)
  }
end

.class_name(decl) ⇒ Object



123
124
125
# File 'lib/rails_ai_bridge/rubydex_adapter/serializer.rb', line 123

def self.class_name(decl)
  decl.class.name.to_s.split('::').last&.downcase
end

.declaration_type(decl) ⇒ Object



108
109
110
111
112
113
114
# File 'lib/rails_ai_bridge/rubydex_adapter/serializer.rb', line 108

def self.declaration_type(decl)
  klass = class_name(decl)
  TYPE_PATTERNS.each do |type, pattern|
    return type if klass.match?(pattern)
  end
  'declaration'
end

.definition_to_hash(defn, root) ⇒ Object



90
91
92
93
94
95
96
97
# File 'lib/rails_ai_bridge/rubydex_adapter/serializer.rb', line 90

def self.definition_to_hash(defn, root)
  {
    name: defn.name,
    location: format_location(defn.try(:location), root),
    comments: defn.try(:comments).presence,
    deprecated: defn.try(:deprecated?) || nil
  }.compact
end

.format_location(location, root) ⇒ Object



99
100
101
102
103
104
105
106
# File 'lib/rails_ai_bridge/rubydex_adapter/serializer.rb', line 99

def self.format_location(location, root)
  return nil unless location

  path = location.try(:path)
  return location.to_s unless path

  relativize_path(path, root)
end

.relativize_path(path, root) ⇒ Object



127
128
129
130
131
# File 'lib/rails_ai_bridge/rubydex_adapter/serializer.rb', line 127

def self.relativize_path(path, root)
  return path unless root && path&.start_with?(root)

  path.sub("#{root}/", '')
end

Instance Method Details

#declaration_to_hash(decl) ⇒ Hash

Converts a rubydex declaration to a serializable hash (summary).

Parameters:

  • decl (Object)

    rubydex declaration

Returns:

  • (Hash)


19
20
21
# File 'lib/rails_ai_bridge/rubydex_adapter/serializer.rb', line 19

def declaration_to_hash(decl)
  self.class.base_declaration_hash(decl).compact
end

#declaration_typeString

Determines the type of a rubydex declaration.

Parameters:

  • decl (Object)

    rubydex declaration

Returns:

  • (String)


80
# File 'lib/rails_ai_bridge/rubydex_adapter/serializer.rb', line 80

delegate :declaration_type, to: :class

#definition_to_hash(defn) ⇒ Hash

Converts a rubydex definition to a serializable hash.

Parameters:

  • defn (Object)

    rubydex definition

Returns:

  • (Hash)


64
65
66
# File 'lib/rails_ai_bridge/rubydex_adapter/serializer.rb', line 64

def definition_to_hash(defn)
  self.class.definition_to_hash(defn, @root)
end

#detailed_declaration_to_hash(decl) ⇒ Hash

Converts a rubydex declaration to a serializable hash with full details.

Includes definitions, ancestors, descendants, and owner.

Parameters:

  • decl (Object)

    rubydex declaration

Returns:

  • (Hash)


29
30
31
# File 'lib/rails_ai_bridge/rubydex_adapter/serializer.rb', line 29

def detailed_declaration_to_hash(decl)
  DetailedDeclarationMapper.new(decl, @root).to_h
end

#format_location(location) ⇒ String?

Formats a rubydex location into a readable string.

Parameters:

  • location (Object)

    rubydex location object

Returns:

  • (String, nil)


72
73
74
# File 'lib/rails_ai_bridge/rubydex_adapter/serializer.rb', line 72

def format_location(location)
  self.class.format_location(location, @root)
end