Class: RailsAiBridge::RubydexAdapter::Serializer
- Inherits:
-
Object
- Object
- RailsAiBridge::RubydexAdapter::Serializer
- 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
- .base_declaration_hash(decl) ⇒ Object
- .class_name(decl) ⇒ Object
- .declaration_type(decl) ⇒ Object
- .definition_to_hash(defn, root) ⇒ Object
- .format_location(location, root) ⇒ Object
- .relativize_path(path, root) ⇒ Object
Instance Method Summary collapse
-
#declaration_to_hash(decl) ⇒ Hash
Converts a rubydex declaration to a serializable hash (summary).
-
#declaration_type ⇒ String
Determines the type of a rubydex declaration.
-
#definition_to_hash(defn) ⇒ Hash
Converts a rubydex definition to a serializable hash.
-
#detailed_declaration_to_hash(decl) ⇒ Hash
Converts a rubydex declaration to a serializable hash with full details.
-
#format_location(location) ⇒ String?
Formats a rubydex location into a readable string.
-
#initialize(root) ⇒ Serializer
constructor
A new instance of Serializer.
Constructor Details
#initialize(root) ⇒ Serializer
Returns a new instance of Serializer.
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 |
Instance Method Details
#declaration_to_hash(decl) ⇒ Hash
Converts a rubydex declaration to a serializable hash (summary).
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_type ⇒ String
Determines the type of a rubydex declaration.
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.
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.
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.
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 |