Class: RailsOpenapiGen::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/rails-openapi-gen/configuration.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Initializes configuration with default values



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/rails-openapi-gen/configuration.rb', line 8

def initialize
  @openapi_version = "3.0.0"
  @info = {
    title: default_app_name,
    version: "1.0.0",
    description: "API documentation generated by rails-openapi-gen"
  }
  @servers = [
    {
      url: "http://localhost:3000",
      description: "Development server"
    }
  ]
  @route_patterns = {
    include: [/.*/], # Include all routes by default
    exclude: []      # Exclude nothing by default
  }
  @output = {
    directory: "openapi",
    filename: "openapi.yaml",
    split_files: true
  }
  @view_paths = {
    api_prefix: nil,         # No API path prefix removal by default
    component_prefix: nil    # No component name prefix removal by default
  }
end

Instance Attribute Details

#infoObject

Returns the value of attribute info.



5
6
7
# File 'lib/rails-openapi-gen/configuration.rb', line 5

def info
  @info
end

#openapi_versionObject

Returns the value of attribute openapi_version.



5
6
7
# File 'lib/rails-openapi-gen/configuration.rb', line 5

def openapi_version
  @openapi_version
end

#outputObject

Returns the value of attribute output.



5
6
7
# File 'lib/rails-openapi-gen/configuration.rb', line 5

def output
  @output
end

#route_patternsObject

Returns the value of attribute route_patterns.



5
6
7
# File 'lib/rails-openapi-gen/configuration.rb', line 5

def route_patterns
  @route_patterns
end

#serversObject

Returns the value of attribute servers.



5
6
7
# File 'lib/rails-openapi-gen/configuration.rb', line 5

def servers
  @servers
end

#view_pathsObject

Returns the value of attribute view_paths.



5
6
7
# File 'lib/rails-openapi-gen/configuration.rb', line 5

def view_paths
  @view_paths
end

Instance Method Details

#load_from_file(file_path = nil) ⇒ void

This method returns an undefined value.

TODO: Loads configuration from Ruby file



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/rails-openapi-gen/configuration.rb', line 39

def load_from_file(file_path = nil)
  # If no file path provided, try to find configuration file automatically
  if file_path.nil?
    file_path = find_config_file
    return unless file_path
  end

  # Check if file exists
  unless File.exist?(file_path)
    puts "⚠️  Configuration file not found: #{file_path}" if ENV['RAILS_OPENAPI_DEBUG']
    return
  end

  puts "📁 Loading configuration from: #{file_path}" if ENV['RAILS_OPENAPI_DEBUG']

  # Load the configuration file
  if file_path.end_with?('.rb')
    load_ruby_config(file_path)
  elsif file_path.end_with?('.yml', '.yaml')
    load_yaml_config(file_path)
  else
    puts "❌ Unsupported configuration file format: #{file_path}"
  end
end

#output_directoryString

Returns the full path to the output directory



66
67
68
69
70
71
72
# File 'lib/rails-openapi-gen/configuration.rb', line 66

def output_directory
  if @output[:directory].start_with?('/')
    @output[:directory]
  else
    File.join(Rails.root.to_s, @output[:directory])
  end
end

#output_filenameString

Returns the output filename



76
77
78
# File 'lib/rails-openapi-gen/configuration.rb', line 76

def output_filename
  @output[:filename]
end

#remove_api_prefix(path) ⇒ String

Removes API prefix from path if configured



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/rails-openapi-gen/configuration.rb', line 112

def remove_api_prefix(path)
  # Ensure @view_paths is properly initialized
  @view_paths ||= { api_prefix: nil }

  api_prefix = @view_paths[:api_prefix]
  return path unless api_prefix

  # Normalize the prefix (ensure it starts and ends properly)
  normalized_prefix = "/#{api_prefix}".gsub(%r{/+}, '/').chomp('/')

  # Remove the prefix if the path starts with it
  if path.start_with?(normalized_prefix)
    remaining_path = path[normalized_prefix.length..-1]
    # Ensure the result starts with / or is empty
    remaining_path.start_with?('/') ? remaining_path : "/#{remaining_path}"
  else
    path
  end
end

#remove_component_prefix(component_name) ⇒ String

Removes component prefix from component name if configured



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/rails-openapi-gen/configuration.rb', line 135

def remove_component_prefix(component_name)
  # Ensure @view_paths is properly initialized
  @view_paths ||= { component_prefix: nil }

  component_prefix = @view_paths[:component_prefix]
  return component_name unless component_prefix

  # Convert to PascalCase for matching
  normalized_prefix = component_prefix.split(%r{[/\-_]}).map(&:capitalize).join('')

  # Remove the prefix if the component name starts with it
  if component_name.start_with?(normalized_prefix)
    remaining_name = component_name[normalized_prefix.length..-1]
    remaining_name.empty? ? component_name : remaining_name
  else
    component_name
  end
end

#route_included?(path) ⇒ Boolean

Checks if a route path should be included in the OpenAPI spec



96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/rails-openapi-gen/configuration.rb', line 96

def route_included?(path)
  # Ensure @route_patterns is properly initialized
  @route_patterns ||= { include: [/.*/], exclude: [] }

  # Check if path matches any include pattern
  included = (@route_patterns[:include] || []).any? { |pattern| path.match?(pattern) }
  return false unless included

  # Check if path matches any exclude pattern
  excluded = (@route_patterns[:exclude] || []).any? { |pattern| path.match?(pattern) }
  !excluded
end

#split_files?Boolean

Checks if output should be split into multiple files



82
83
84
# File 'lib/rails-openapi-gen/configuration.rb', line 82

def split_files?
  @output[:split_files]
end

#update_output_config(output_config) ⇒ void

This method returns an undefined value.

Updates output configuration



89
90
91
# File 'lib/rails-openapi-gen/configuration.rb', line 89

def update_output_config(output_config)
  load_output_config(output_config)
end