Skip to content

Commit 49e9656

Browse files
committed
Refactor generators
remove inheritance and add composition for a more flexible code management
1 parent 53bf766 commit 49e9656

27 files changed

+420
-182
lines changed

bin/pico_api

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
21
#!/usr/bin/env ruby
32

43
require 'optparse'
54
require 'pico_api'
65

76
options = {}
87
OptionParser.new do |opts|
9-
opts.banner = "Usage: pico_api [options]"
8+
opts.banner = 'Usage: pico_api [options]'
109

1110
opts.on('-n', '--new project_name') { |o| options[:project_name] = o }
1211
end.parse!

bin/setup

100644100755
File mode changed.

lib/pico_api/generators/commands/base.rb

Lines changed: 0 additions & 45 deletions
This file was deleted.

lib/pico_api/generators/commands/copy_template.rb

Lines changed: 0 additions & 19 deletions
This file was deleted.

lib/pico_api/generators/commands/create_base.rb

Lines changed: 0 additions & 29 deletions
This file was deleted.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# frozen_string_literal: true
2+
3+
require 'fileutils'
4+
5+
module PicoApi
6+
module Generators
7+
module Commands
8+
class CreateBasicStructure
9+
def self.call(project_name)
10+
project_name_converter =
11+
::PicoApi::Generators::ProjectNameConverter.new(project_name)
12+
13+
new(project_name_converter).call
14+
end
15+
16+
def initialize(project_name_converter)
17+
@project_name_converter = project_name_converter
18+
end
19+
20+
def call
21+
create_bin_folder
22+
create_lib_folder
23+
create_config_folder
24+
end
25+
26+
private
27+
28+
attr_reader :project_name_converter
29+
30+
def create_bin_folder
31+
FileUtils.mkdir_p("#{snakecased_name}/bin")
32+
end
33+
34+
def create_lib_folder
35+
FileUtils.mkdir_p("#{snakecased_name}/lib/#{snakecased_name}")
36+
end
37+
38+
def create_config_folder
39+
FileUtils.mkdir_p("#{snakecased_name}/config")
40+
end
41+
42+
def snakecased_name
43+
project_name_converter.snakecased
44+
end
45+
end
46+
end
47+
end
48+
end

lib/pico_api/generators/commands/create_config_application.rb

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,32 @@
33
module PicoApi
44
module Generators
55
module Commands
6-
class CreateConfigApplication < CreateTemplate
7-
private
6+
class CreateConfigApplication
7+
def self.call(project_name)
8+
file_creator = ::PicoApi::Generators::FileCreator.build(project_name)
89

9-
def destination_path
10-
'/config/application.rb'
10+
new(file_creator).call
11+
end
12+
13+
def initialize(file_creator)
14+
@file_creator = file_creator
15+
end
16+
17+
def call
18+
file_creator.create(template_file_path, destination_path)
1119
end
1220

13-
def template_relative_path
21+
private
22+
23+
attr_reader :file_creator
24+
25+
def template_file_path
1426
'/generators/templates/config/application.erb'
1527
end
28+
29+
def destination_path
30+
'/config/application.rb'
31+
end
1632
end
1733
end
1834
end

lib/pico_api/generators/commands/create_config_boot.rb

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,32 @@
33
module PicoApi
44
module Generators
55
module Commands
6-
class CreateConfigBoot < CopyTemplate
7-
private
6+
class CreateConfigBoot
7+
def self.call(project_name)
8+
file_copier = ::PicoApi::Generators::FileCopier.build(project_name)
89

9-
def destination_path
10-
'/config/boot.rb'
10+
new(file_copier).call
11+
end
12+
13+
def initialize(file_copier)
14+
@file_copier = file_copier
15+
end
16+
17+
def call
18+
file_copier.copy(template_file_path, destination_path)
1119
end
1220

13-
def template_relative_path
21+
private
22+
23+
attr_reader :file_copier
24+
25+
def template_file_path
1426
'/generators/templates/config/boot.erb'
1527
end
28+
29+
def destination_path
30+
'/config/boot.rb'
31+
end
1632
end
1733
end
1834
end

lib/pico_api/generators/commands/create_config_configuration.rb

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,32 @@
33
module PicoApi
44
module Generators
55
module Commands
6-
class CreateConfigConfiguration < CreateTemplate
7-
private
6+
class CreateConfigConfiguration
7+
def self.call(project_name)
8+
file_creator = ::PicoApi::Generators::FileCreator.build(project_name)
89

9-
def destination_path
10-
'/config/configuration.rb'
10+
new(file_creator).call
11+
end
12+
13+
def initialize(file_creator)
14+
@file_creator = file_creator
15+
end
16+
17+
def call
18+
file_creator.create(template_file_path, destination_path)
1119
end
1220

13-
def template_relative_path
21+
private
22+
23+
attr_reader :file_creator
24+
25+
def template_file_path
1426
'/generators/templates/config/configuration.erb'
1527
end
28+
29+
def destination_path
30+
'/config/configuration.rb'
31+
end
1632
end
1733
end
1834
end

lib/pico_api/generators/commands/create_config_database_setup.rb

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,32 @@
33
module PicoApi
44
module Generators
55
module Commands
6-
class CreateConfigDatabaseSetup < CopyTemplate
7-
private
6+
class CreateConfigDatabaseSetup
7+
def self.call(project_name)
8+
file_copier = ::PicoApi::Generators::FileCopier.build(project_name)
89

9-
def destination_path
10-
'/config/database_setup.rb'
10+
new(file_copier).call
11+
end
12+
13+
def initialize(file_copier)
14+
@file_copier = file_copier
15+
end
16+
17+
def call
18+
file_copier.copy(template_file_path, destination_path)
1119
end
1220

13-
def template_relative_path
21+
private
22+
23+
attr_reader :file_copier
24+
25+
def template_file_path
1426
'/generators/templates/config/database_setup.erb'
1527
end
28+
29+
def destination_path
30+
'/config/database_setup.rb'
31+
end
1632
end
1733
end
1834
end

0 commit comments

Comments
 (0)