Class: Observ::IndexFileGenerator
- Inherits:
-
Object
- Object
- Observ::IndexFileGenerator
- Defined in:
- lib/observ/index_file_generator.rb
Overview
Service class for generating index files for Stimulus controllers
Instance Attribute Summary collapse
-
#app_root ⇒ Object
readonly
Returns the value of attribute app_root.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
Instance Method Summary collapse
-
#check_main_controllers_registration ⇒ Hash
Check if Observ controllers are registered in the main controllers index.
-
#generate_controllers_index(controllers_path) ⇒ Hash
Generate or update the Observ controllers index file.
-
#generate_import_statement(relative_path = "./observ") ⇒ String
Generate import statement for main controllers index.
-
#initialize(app_root:, logger: $stdout) ⇒ IndexFileGenerator
constructor
A new instance of IndexFileGenerator.
Constructor Details
#initialize(app_root:, logger: $stdout) ⇒ IndexFileGenerator
Returns a new instance of IndexFileGenerator.
10 11 12 13 |
# File 'lib/observ/index_file_generator.rb', line 10 def initialize(app_root:, logger: $stdout) @app_root = Pathname.new(app_root) @logger = logger end |
Instance Attribute Details
#app_root ⇒ Object (readonly)
Returns the value of attribute app_root.
6 7 8 |
# File 'lib/observ/index_file_generator.rb', line 6 def app_root @app_root end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
6 7 8 |
# File 'lib/observ/index_file_generator.rb', line 6 def logger @logger end |
Instance Method Details
#check_main_controllers_registration ⇒ Hash
Check if Observ controllers are registered in the main controllers index
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/observ/index_file_generator.rb', line 51 def check_main_controllers_registration main_index = app_root.join("app", "javascript", "controllers", "index.js") unless main_index.exist? return { registered: false, main_index_exists: false, suggestions: [ "Main controllers index file not found at: #{main_index.relative_path_from(app_root)}", "You may need to manually import Observ controllers in your application" ] } end content = File.read(main_index) registered = content.include?("observ") if registered { registered: true, main_index_exists: true, path: main_index } else { registered: false, main_index_exists: true, path: main_index, suggestions: [ "Add to #{main_index.relative_path_from(app_root)}:", " import './observ'" ] } end end |
#generate_controllers_index(controllers_path) ⇒ Hash
Generate or update the Observ controllers index file
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/observ/index_file_generator.rb', line 18 def generate_controllers_index(controllers_path) controllers_path = Pathname.new(controllers_path) index_file = controllers_path.join("index.js") file_existed = index_file.exist? # Find all controller files controller_files = Dir.glob(controllers_path.join("*_controller.js")).sort if controller_files.empty? log " ⚠ No controller files found in #{controllers_path}" return { created: false, error: "No controller files found" } end content = generate_index_content(controller_files, controllers_path) if file_existed existing_content = File.read(index_file) if existing_content == content log " - Index file already up to date: #{index_file.relative_path_from(app_root)}" return { created: false, updated: false, path: index_file } else log " ✓ Updated index file: #{index_file.relative_path_from(app_root)}" end else log " ✓ Created index file: #{index_file.relative_path_from(app_root)}" end File.write(index_file, content) { created: !file_existed, updated: file_existed, path: index_file } end |
#generate_import_statement(relative_path = "./observ") ⇒ String
Generate import statement for main controllers index
86 87 88 |
# File 'lib/observ/index_file_generator.rb', line 86 def generate_import_statement(relative_path = "./observ") "\nimport '#{relative_path}'\n" end |