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.

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

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
22
23
24
25
# File 'lib/rails_ai_bridge/rubydex_adapter/serializer.rb', line 19

def declaration_to_hash(decl)
  {
    name: decl.name,
    unqualified_name: decl.respond_to?(:unqualified_name) ? decl.unqualified_name : nil,
    type: declaration_type(decl)
  }.compact
end

#declaration_type(decl) ⇒ String

Determines the type of a rubydex declaration.

Parameters:

  • decl (Object)

    rubydex declaration

Returns:

  • (String)


77
78
79
80
81
82
83
84
85
86
# File 'lib/rails_ai_bridge/rubydex_adapter/serializer.rb', line 77

def declaration_type(decl)
  klass = decl.class.name.to_s.split('::').last&.downcase
  case klass
  when /class/ then 'class'
  when /module/ then 'module'
  when /method/ then 'method'
  when /constant/ then 'constant'
  else 'declaration'
  end
end

#definition_to_hash(defn) ⇒ Hash

Converts a rubydex definition to a serializable hash.

Parameters:

  • defn (Object)

    rubydex definition

Returns:

  • (Hash)


52
53
54
55
56
57
58
# File 'lib/rails_ai_bridge/rubydex_adapter/serializer.rb', line 52

def definition_to_hash(defn)
  hash = { name: defn.name }
  hash[:location] = format_location(defn.location) if defn.respond_to?(:location)
  hash[:comments] = defn.comments if defn.respond_to?(:comments) && defn.comments.present?
  hash[:deprecated] = true if defn.respond_to?(:deprecated?) && defn.deprecated?
  hash.compact
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)


33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/rails_ai_bridge/rubydex_adapter/serializer.rb', line 33

def detailed_declaration_to_hash(decl)
  hash = {
    name: decl.name,
    unqualified_name: decl.respond_to?(:unqualified_name) ? decl.unqualified_name : nil,
    type: declaration_type(decl)
  }

  hash[:definitions] = decl.definitions.map { |d| definition_to_hash(d) } if decl.respond_to?(:definitions)
  hash[:ancestors] = decl.ancestors.map(&:name) if decl.respond_to?(:ancestors)
  hash[:descendants] = decl.descendants.map(&:name) if decl.respond_to?(:descendants)
  hash[:owner] = decl.owner.name if decl.respond_to?(:member) && decl.respond_to?(:owner) && decl.owner

  hash.compact
end

#format_location(location) ⇒ String?

Formats a rubydex location into a readable string.

Parameters:

  • location (Object)

    rubydex location object

Returns:

  • (String, nil)


64
65
66
67
68
69
70
71
# File 'lib/rails_ai_bridge/rubydex_adapter/serializer.rb', line 64

def format_location(location)
  return nil unless location
  return location.to_s unless location.respond_to?(:path)

  path = location.path
  path = path.sub("#{@root}/", '') if path&.start_with?(@root)
  path
end