Class: RailsAiBridge::AssistantFormatsPreference
- Inherits:
-
Object
- Object
- RailsAiBridge::AssistantFormatsPreference
- Defined in:
- lib/rails_ai_bridge/assistant_formats_preference.rb
Overview
Reads and writes which assistant formats +rails ai:bridge+ regenerates by default.
The install generator creates RELATIVE_PATH with a YAML +formats+ list (a subset of FORMAT_KEYS). When the file is missing, invalid, or lists no recognized formats, callers treat the result as +nil+ and fall back to generating all formats.
Constant Summary collapse
- RELATIVE_PATH =
Path of the preference file relative to +Rails.root+.
'config/rails_ai_bridge/install.yml'- FORMAT_KEYS =
All recognized format keys (order is not significant).
%i[claude codex cursor windsurf copilot json gemini].freeze
Class Method Summary collapse
-
.config_path ⇒ Pathname?
Absolute path to the preference file for the current Rails application.
-
.formats_for_default_bridge_task ⇒ Array<Symbol>?
Returns the subset of formats requested for the default +rails ai:bridge+ task.
-
.write!(formats:) ⇒ void
Writes +install.yml+ with the requested formats filtered against FORMAT_KEYS.
Class Method Details
.config_path ⇒ Pathname?
Absolute path to the preference file for the current Rails application.
29 30 31 32 33 |
# File 'lib/rails_ai_bridge/assistant_formats_preference.rb', line 29 def config_path return nil unless defined?(Rails) && Rails.application Rails.root.join(RELATIVE_PATH) end |
.formats_for_default_bridge_task ⇒ Array<Symbol>?
Returns the subset of formats requested for the default +rails ai:bridge+ task. Returns +nil+ when the file is absent, invalid, or contains no recognized formats (callers should interpret +nil+ as "generate all formats").
40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/rails_ai_bridge/assistant_formats_preference.rb', line 40 def formats_for_default_bridge_task path = config_path return nil unless path&.file? data = load_yaml(path) return nil unless data.is_a?(Hash) raw = data['formats'] || data[:formats] return nil if raw.nil? fmts = Array(raw).map { |f| f.to_s.downcase.to_sym } & FORMAT_KEYS fmts.empty? ? nil : fmts.uniq end |
.write!(formats:) ⇒ void
This method returns an undefined value.
Writes +install.yml+ with the requested formats filtered against FORMAT_KEYS.
59 60 61 62 63 64 65 66 |
# File 'lib/rails_ai_bridge/assistant_formats_preference.rb', line 59 def write!(formats:) path = config_path raise Error, 'Rails app not available' unless path path.dirname.mkpath list = Array(formats).map { |f| f.to_s.downcase }.uniq & FORMAT_KEYS.map(&:to_s) File.write(path.to_s, YAML.dump({ 'formats' => list })) end |