Class: PgReports::ModuleGenerator
- Inherits:
-
Object
- Object
- PgReports::ModuleGenerator
- Defined in:
- lib/pg_reports/module_generator.rb
Overview
Generates module methods dynamically from YAML report definitions
Class Method Summary collapse
- .define_report_method(module_class, report_name, definition) ⇒ Object
- .generate! ⇒ Object
- .get_module(module_name) ⇒ Object
Class Method Details
.define_report_method(module_class, report_name, definition) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/pg_reports/module_generator.rb', line 28 def self.define_report_method(module_class, report_name, definition) params_config = definition.config["parameters"] || {} # Extract default parameter values defaults = params_config.transform_values { |v| v["default"] } # Define the method on the module # We capture the definition in a local variable to avoid closure issues captured_definition = definition captured_defaults = defaults module_class.define_singleton_method(report_name) do |**params| merged_params = captured_defaults.merge(params) captured_definition.generate_report(**merged_params) end end |
.generate! ⇒ Object
6 7 8 9 10 11 12 13 14 15 |
# File 'lib/pg_reports/module_generator.rb', line 6 def self.generate! ReportLoader.load_all.each do |module_name, reports| module_class = get_module(module_name) next unless module_class reports.each do |report_name, definition| define_report_method(module_class, report_name, definition) end end end |
.get_module(module_name) ⇒ Object
19 20 21 22 23 24 25 26 |
# File 'lib/pg_reports/module_generator.rb', line 19 def self.get_module(module_name) const_name = module_name.to_s.split("_").map(&:capitalize).join PgReports::Modules.const_get(const_name) rescue NameError # Module doesn't exist, skip it # We don't auto-create modules to avoid conflicts nil end |