Class: AtCoderFriends::Generator::Main
- Inherits:
-
Object
- Object
- AtCoderFriends::Generator::Main
- Defined in:
- lib/at_coder_friends/generator/main.rb
Overview
entry point of source generation
Instance Attribute Summary collapse
-
#ctx ⇒ Object
readonly
Returns the value of attribute ctx.
Instance Method Summary collapse
- #config_for(gen_name) ⇒ Object
-
#initialize(ctx) ⇒ Main
constructor
A new instance of Main.
- #load_class(gen_name) ⇒ Object
- #load_obj(gen_name) ⇒ Object
- #process(pbm) ⇒ Object
- #to_snake(str) ⇒ Object
Constructor Details
#initialize(ctx) ⇒ Main
Returns a new instance of Main.
9 10 11 12 |
# File 'lib/at_coder_friends/generator/main.rb', line 9 def initialize(ctx) @ctx = ctx @cache = {} end |
Instance Attribute Details
#ctx ⇒ Object (readonly)
Returns the value of attribute ctx.
7 8 9 |
# File 'lib/at_coder_friends/generator/main.rb', line 7 def ctx @ctx end |
Instance Method Details
#config_for(gen_name) ⇒ Object
51 52 53 |
# File 'lib/at_coder_friends/generator/main.rb', line 51 def config_for(gen_name) ctx.config.dig('generator_settings', gen_name) || {} end |
#load_class(gen_name) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/at_coder_friends/generator/main.rb', line 35 def load_class(gen_name) snake_gen_name = to_snake(gen_name) require "at_coder_friends/generator/#{snake_gen_name}" unless AtCoderFriends::Generator.const_defined?(gen_name) AtCoderFriends::Generator.const_get(gen_name) rescue LoadError raise AppError, <<~MSG Error: Failed to load plugin. The '#{gen_name}' plugin could not be found. To use this plugin, please install the required gem by following these steps: 1. Open a terminal or command prompt. 2. Run the following command: gem install at_coder_friends-generator-#{snake_gen_name} 3. Once the above command completes, please run the program again. MSG end |
#load_obj(gen_name) ⇒ Object
26 27 28 29 30 31 32 33 |
# File 'lib/at_coder_friends/generator/main.rb', line 26 def load_obj(gen_name) @cache[gen_name] ||= begin cls_name = gen_name.split('_')[0] gen_class = load_class(cls_name) gen_cnf = config_for(gen_name) gen_class.new(gen_cnf) end end |
#process(pbm) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/at_coder_friends/generator/main.rb', line 14 def process(pbm) generators = ctx.config['generators'] || [] generators.each do |gen_name| gen_obj = load_obj(gen_name) gen_obj.process(pbm) rescue StandardError => e puts "an error occurred in generator:#{gen_name}." puts e puts e.backtrace end end |
#to_snake(str) ⇒ Object
55 56 57 58 59 60 61 62 |
# File 'lib/at_coder_friends/generator/main.rb', line 55 def to_snake(str) str .gsub(/::/, '/') .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2') .gsub(/([a-z\d])([A-Z])/, '\1_\2') .tr('-', '_') .downcase end |