Class: Roda::Project::Bin::Generators::Model
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
18
|
# File 'lib/roda/project/bin/generators/model.rb', line 8
def call
if model_name.nil? || field_args == []
puts "Usage: bin/roda g model name field1 field2"
exit 1
end
puts "* creating model"
create_model
create_model_test
Migration.new(args: [migration_name].concat(field_args)).call
end
|
#code ⇒ Object
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/model.rb', line 47
def code
if name_segments.length == 1
<<~RUBY
class #{qualified_class_name}
end
RUBY
else
depth = name_segments.length - 1
class_indent = " " * depth
lines = [
"#{class_indent}class #{camelize(name_segments.last)} < Sequel::Model(:#{table_name})",
"#{class_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}class #{mod}"] + lines + ["#{mod_indent}end"]
end
lines.join("\n") + "\n"
end
end
|
#create_model ⇒ Object
28
29
30
31
32
33
34
|
# File 'lib/roda/project/bin/generators/model.rb', line 28
def create_model
ensure_and_get_path("app/models", model_relative_path)
filename = File.join("app/models", *name_segments[0..-2], "#{model_file_basename}.rb")
File.write(filename, code)
puts_create_message(filename)
end
|
#create_model_test ⇒ Object
20
21
22
23
24
25
26
|
# File 'lib/roda/project/bin/generators/model.rb', line 20
def create_model_test
ensure_and_get_path("spec/app/models", model_relative_path)
filename = File.join("spec/app/models", *name_segments[0..-2], "#{model_file_basename}_spec.rb")
File.write(filename, spec_code)
puts_create_message(filename)
end
|
#field_args ⇒ Object
112
113
114
|
# File 'lib/roda/project/bin/generators/model.rb', line 112
def field_args
@field_args ||= @args[1..] || []
end
|
#flat_class_name ⇒ Object
84
85
86
|
# File 'lib/roda/project/bin/generators/model.rb', line 84
def flat_class_name
@flat_class_name ||= name_segments.map { |s| camelize(s) }.join
end
|
#migration_name ⇒ Object
70
71
72
73
74
|
# File 'lib/roda/project/bin/generators/model.rb', line 70
def migration_name
segs = name_segments.map { |s| camelize(s) }
segs[-1] = pluralize(segs[-1])
"Create" + segs.join("_")
end
|
#model_file_basename ⇒ Object
104
105
106
|
# File 'lib/roda/project/bin/generators/model.rb', line 104
def model_file_basename
@model_file_basename ||= underscore(name_segments.last)
end
|
#model_name ⇒ Object
108
109
110
|
# File 'lib/roda/project/bin/generators/model.rb', line 108
def model_name
@model_name ||= @args[0].nil? ? nil : qualified_class_name
end
|
#model_relative_path ⇒ Object
96
97
98
99
100
101
102
|
# File 'lib/roda/project/bin/generators/model.rb', line 96
def model_relative_path
@model_relative_path ||= if name_segments.length > 1
File.join(*name_segments[0..-2], model_file_basename)
else
model_file_basename
end
end
|
#name_segments ⇒ Object
76
77
78
|
# File 'lib/roda/project/bin/generators/model.rb', line 76
def name_segments
@name_segments ||= @args[0].to_s.split("/")
end
|
#qualified_class_name ⇒ Object
80
81
82
|
# File 'lib/roda/project/bin/generators/model.rb', line 80
def qualified_class_name
@qualified_class_name ||= name_segments.map { |s| camelize(s) }.join("::")
end
|
#spec_code ⇒ Object
36
37
38
39
40
41
42
43
44
45
|
# File 'lib/roda/project/bin/generators/model.rb', line 36
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
|
#table_name ⇒ Object
88
89
90
91
92
93
94
|
# File 'lib/roda/project/bin/generators/model.rb', line 88
def table_name
@table_name ||= begin
segs = name_segments.map { |s| underscore(camelize(s)) }
segs[-1] = pluralize(segs[-1])
segs.join("_")
end
end
|