Class: AtCoderFriends::Generator::Main

Inherits:
Object
  • Object
show all
Defined in:
lib/at_coder_friends/generator/main.rb

Overview

entry point of source generation

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#ctxObject (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



45
46
47
# File 'lib/at_coder_friends/generator/main.rb', line 45

def config_for(gen_name)
  ctx.config.dig('generator_settings', gen_name) || {}
end

#load_class(gen_name) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/at_coder_friends/generator/main.rb', line 36

def load_class(gen_name)
  unless AtCoderFriends::Generator.const_defined?(gen_name)
    require "at_coder_friends/generator/#{to_snake(gen_name)}"
  end
  AtCoderFriends::Generator.const_get(gen_name)
rescue LoadError
  raise AppError, "plugin load error : generator #{gen_name} not found."
end

#load_obj(gen_name) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/at_coder_friends/generator/main.rb', line 28

def load_obj(gen_name)
  @cache[gen_name] ||= begin
    gen_class = load_class(gen_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
25
26
# File 'lib/at_coder_friends/generator/main.rb', line 14

def process(pbm)
  generators = ctx.config.dig('generators') || []
  generators.each do |gen_name|
    begin
      gen_obj = load_obj(gen_name)
      gen_obj.process(pbm)
    rescue StandardError => e
      puts "an error occurred in generator:#{gen_name}."
      puts e.to_s
      puts e.backtrace
    end
  end
end

#to_snake(str) ⇒ Object



49
50
51
52
53
54
55
56
# File 'lib/at_coder_friends/generator/main.rb', line 49

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