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.
Instance Method Summary collapse
-
#declaration_to_hash(decl) ⇒ Hash
Converts a rubydex declaration to a serializable hash (summary).
-
#declaration_type(decl) ⇒ 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 |
Instance Method Details
#declaration_to_hash(decl) ⇒ Hash
Converts a rubydex declaration to a serializable hash (summary).
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.
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.
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.
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.
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 |