Class: Roda::Project::Bin::Generators::Service
Instance Attribute Summary
Attributes inherited from Generator
#pastel
Instance Method Summary
collapse
camelize, plural?, pluralize, singular?, singularize, underscore
#templates_root
Methods inherited from Generator
#initialize
#api_id, #fullstack_id, #id_to_string, #minimal_id, #minitest_id, #mysql_id, #postgresql_id, #rspec_id, #sqlite_id
#cp_dir, #cp_file, #ensure_and_get_path, #erb_cp_dir, #erb_cp_file, #templates_root
Instance Method Details
#call ⇒ Object
8
9
10
11
12
13
14
15
16
17
|
# File 'lib/roda/project/bin/generators/service.rb', line 8
def call
unless valid_args?
puts "Usage: bin/roda g service <module|class> <name>"
exit 1
end
puts "* creating service"
create_service
create_service_spec
end
|
#code ⇒ Object
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
# File 'lib/roda/project/bin/generators/service.rb', line 46
def code
leaf_keyword = service_type
if name_segments.length == 1
<<~RUBY
#{leaf_keyword} #{camelize(name_segments.last)}
end
RUBY
else
depth = name_segments.length - 1
leaf_indent = " " * depth
lines = [
"#{leaf_indent}#{leaf_keyword} #{camelize(name_segments.last)}",
"#{leaf_indent}end"
]
name_segments[0..-2].map { |s| camelize(s) }.reverse.each_with_index do |mod, idx|
mod_indent = " " * (depth - 1 - idx)
lines = ["#{mod_indent}module #{mod}"] + lines + ["#{mod_indent}end"]
end
lines.join("\n") + "\n"
end
end
|
#create_service ⇒ Object
19
20
21
22
23
24
25
|
# File 'lib/roda/project/bin/generators/service.rb', line 19
def create_service
ensure_and_get_path("app/services", service_relative_path)
filename = File.join("app/services", *name_segments[0..-2], "#{service_file_basename}.rb")
File.write(filename, code)
action_success_message(filename)
end
|
#create_service_spec ⇒ Object
27
28
29
30
31
32
33
|
# File 'lib/roda/project/bin/generators/service.rb', line 27
def create_service_spec
ensure_and_get_path("spec/app/services", service_relative_path)
filename = File.join("spec/app/services", *name_segments[0..-2], "#{service_file_basename}_spec.rb")
File.write(filename, spec_code)
action_success_message(filename)
end
|
#name_segments ⇒ Object
70
71
72
|
# File 'lib/roda/project/bin/generators/service.rb', line 70
def name_segments
@name_segments ||= service_name.split("/")
end
|
#parse_args ⇒ Object
98
99
100
101
102
103
104
105
106
|
# File 'lib/roda/project/bin/generators/service.rb', line 98
def parse_args
@parse_args ||= begin
if %w[module class].include?(@args[0].to_s.downcase)
{ type: @args[0].to_s.downcase, name: @args[1].to_s }
else
{ type: "class", name: @args[0].to_s }
end
end
end
|
#qualified_class_name ⇒ Object
74
75
76
|
# File 'lib/roda/project/bin/generators/service.rb', line 74
def qualified_class_name
@qualified_class_name ||= name_segments.map { |s| camelize(s) }.join("::")
end
|
#service_file_basename ⇒ Object
86
87
88
|
# File 'lib/roda/project/bin/generators/service.rb', line 86
def service_file_basename
@service_file_basename ||= underscore(name_segments.last)
end
|
#service_name ⇒ Object
94
95
96
|
# File 'lib/roda/project/bin/generators/service.rb', line 94
def service_name
@service_name ||= parse_args[:name]
end
|
#service_relative_path ⇒ Object
78
79
80
81
82
83
84
|
# File 'lib/roda/project/bin/generators/service.rb', line 78
def service_relative_path
@service_relative_path ||= if name_segments.length > 1
File.join(*name_segments[0..-2], service_file_basename)
else
service_file_basename
end
end
|
#service_type ⇒ Object
90
91
92
|
# File 'lib/roda/project/bin/generators/service.rb', line 90
def service_type
@service_type ||= parse_args[:type]
end
|
#spec_code ⇒ Object
35
36
37
38
39
40
41
42
43
44
|
# File 'lib/roda/project/bin/generators/service.rb', line 35
def spec_code
depth = 2 + name_segments.length - 1
relative_prefix = "../" * depth
<<~RUBY
require_relative "#{relative_prefix}spec_helper"
describe #{qualified_class_name} do
end
RUBY
end
|
#valid_args? ⇒ Boolean
108
109
110
111
112
113
114
115
|
# File 'lib/roda/project/bin/generators/service.rb', line 108
def valid_args?
return false unless %w[module class].include?(service_type)
return false if service_name.empty?
return false if service_name.include?(" ")
return false if service_name.match?(/\A\d/)
true
end
|