Class: TalentedTools

Inherits:
Object
  • Object
show all
Defined in:
lib/talented_tools.rb

Instance Method Summary collapse

Constructor Details

#initializeTalentedTools

Returns a new instance of TalentedTools.



2
3
# File 'lib/talented_tools.rb', line 2

def initialize
end

Instance Method Details

#create_controllersObject



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/talented_tools.rb', line 70

def create_controllers
  constroller_base = File.open("resources/templates/controllers_base.rb")
  content = constroller_base.read

  replacements = { 
    "NameController" => "#{@formated_camel_case}Controller", 
    "NameService" => "#{@formated_camel_case.singularize}Service",
    "NamePresenter" => "#{@formated_camel_case.singularize}Presenter",
    "singular" => "#{@singular_format}",
    "plural" => @plural_format,
  }
  result = content.gsub(/#{Regexp.union(replacements.keys)}/, replacements)

  file_in = File.open("tmp.rb", "a+")
  file_in.write(result)
  file_in.close

  file_content = File.read("tmp.rb")

  File.write("app/controllers/#{@plural_format}_controller.rb", file_content)
  
  system("rm -rf tmp.rb")
end

#create_end_point(name) ⇒ Object



5
6
7
8
9
10
11
12
13
14
# File 'lib/talented_tools.rb', line 5

def create_end_point(name)
  format_name(name)
  create_route
  create_controllers
  create_services
  create_models
  create_presenters

  puts "Successfully created #{name} endpoint!"
end

#create_modelsObject



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/talented_tools.rb', line 117

def create_models
  
  models_base = File.open("resources/templates/models_base.rb")
  content = models_base.read

  replacements = { 
    "ClassName" => @formated_camel_case.singularize,
    "single" => @singular_format,
    "plural" => @plural_format
   }
  result = content.gsub(/#{Regexp.union(replacements.keys)}/, replacements)

  file_in = File.open("tmp.rb", "a+")
  file_in.write(result)
  file_in.close

  file_content = File.read("tmp.rb")

  File.write("app/models/#{@singular_format}.rb", file_content)
  
  system("rm -rf tmp.rb")
end

#create_presentersObject



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/talented_tools.rb', line 140

def create_presenters
  
  presenters_base = File.open("resources/templates/presenters_base.rb")
  content = presenters_base.read

  replacements = { 
    "ClassName" => @formated_camel_case.singularize,
    "single" => @singular_format,
    "plural" => @plural_format
   }
  result = content.gsub(/#{Regexp.union(replacements.keys)}/, replacements)

  file_in = File.open("tmp.rb", "a+")
  file_in.write(result)
  file_in.close

  file_content = File.read("tmp.rb")

  File.write("app/presenters/#{@singular_format}_presenter.rb", file_content)
  
  system("rm -rf tmp.rb")
end

#create_routeObject



42
43
44
45
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/talented_tools.rb', line 42

def create_route
  file_in = File.open("resources/templates/routes_base.rb", "r")
  content = file_in.read

  replacements = {
    "singular" => "#{@singular_format}",
    "plural" => "#{@plural_format}",
    "route_format" => "#{@route_format}"
  }
  result = content.gsub(/#{Regexp.union(replacements.keys)}/, replacements)
  file_in = File.open("tmp.txt", "a+")
  file_in.write(result)
  file_in.close

  file_origin = File.open("tmp.txt", "r")
  content = file_origin.read
  file_origin.close

  last_line = 0
  file = File.open("config/routes.rb", "r+")
  file.each {  last_line = file.pos unless file.eof? }
  file.seek(last_line, IO::SEEK_SET)
  file.write("\n#{content}\n \n end")
  file.close

  system("rm -rf tmp.txt")
end

#create_servicesObject



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/talented_tools.rb', line 94

def create_services
  name_class = @formated_camel_case.singularize

  services_base = File.open("resources/templates/services_base.rb")
  content = services_base.read

  replacements = { 
    "Name" => name_class,
    "singular" => @singular_format
  }
  result = content.gsub(/#{Regexp.union(replacements.keys)}/, replacements)

  file_in = File.open("tmp.rb", "a+")
  file_in.write(result)
  file_in.close

  file_content = File.read("tmp.rb")

  File.write("app/services/#{@singular_format}_service.rb", file_content)
  
  system("rm -rf tmp.rb")
end

#find_all_controller(end_point, attribute) ⇒ Object



266
267
268
269
270
271
272
273
274
275
276
# File 'lib/talented_tools.rb', line 266

def find_all_controller(end_point, attribute)
  lines = File.readlines("app/controllers/#{end_point}_controller.rb")

  target_index = lines.find_index { |line| line.include?("params.permit(") }

  new_line = "      :#{attribute},\n"
  lines.insert(target_index + 1, new_line)
  File.open("app/controllers/#{end_point}_controller.rb", 'w') do |file|
    file.puts lines
  end
end

#find_all_method(end_point, attribute, greater_lower) ⇒ Object

IMPLEMENT ATTRIBUTE



255
256
257
258
259
260
261
262
263
264
# File 'lib/talented_tools.rb', line 255

def find_all_method( end_point, attribute, greater_lower )
  find_all_controller(end_point, attribute)
  find_all_model(end_point, attribute)
  find_methods(end_point, attribute)
  if greater_lower.present?
    greater_methods(end_point, attribute, greater_lower)
    lower_methods(end_point, attribute, greater_lower)

  end
end

#find_all_model(end_point, attribute) ⇒ Object



278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# File 'lib/talented_tools.rb', line 278

def find_all_model(end_point, attribute)
  file_contents = File.read("app/models/#{end_point.singularize}.rb")

  target_word = " .by_not_deleted_at"
  target_index = file_contents.index(target_word, file_contents.index(target_word))

  new_line = "\n       .by_#{attribute}(params[:""#{attribute}""])"
  file_contents.insert(target_index + target_word.length, new_line)
  File.write("app/models/#{end_point.singularize}.rb", file_contents)

  target_word = "                .by_not_deleted_at"
  target_index = file_contents.index(target_word, file_contents.index(target_word))

  new_line = "\n                 .by_#{attribute}(params[:""#{attribute}""])"
  file_contents.insert(target_index + target_word.length, new_line)
  File.write("app/models/#{end_point.singularize}.rb", file_contents)
end

#find_methods(end_point, attribute) ⇒ Object



296
297
298
299
300
301
302
303
304
305
306
307
308
# File 'lib/talented_tools.rb', line 296

def find_methods(end_point, attribute)
  method = "  def self.by_#{attribute}(#{attribute})\n    return all unless #{attribute}.present?\n\n    where(#{attribute}: #{attribute})\n  end"

  file = File.open("app/models/#{end_point.singularize}.rb", "r")
  content = file.read
  last_end_index = content.rindex(/\bend\b/)
  new_content = content.insert(last_end_index -1, "\n#{method}\n")
  file.close

  File.open("app/models/#{end_point.singularize}.rb", "w") do |f|
    f.write(new_content)
  end
end

#format_camel_case(name) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/talented_tools.rb', line 23

def format_camel_case(name)
  camel_case_names = []
  
  split_name = name.split('_')
  split_name.each do |t| 
    camel_case_names.push(t.capitalize)
  end
  result = camel_case_names.join(", ").gsub(", ", "")

  result
end

#format_name(name) ⇒ Object



16
17
18
19
20
21
# File 'lib/talented_tools.rb', line 16

def format_name(name)
  @formated_camel_case = format_camel_case(name)
  @singular_format = name.singularize
  @plural_format = name.pluralize
  @route_format = format_route_name(name)
end

#format_natural_end_point_name(end_point_name) ⇒ Object



217
218
219
220
# File 'lib/talented_tools.rb', line 217

def format_natural_end_point_name(end_point_name)
  @end_point_name = end_point_name
  @singular_end_point_name = end_point_name.singularize
end

#format_natural_idempotency(idempotency_name) ⇒ Object



222
223
224
# File 'lib/talented_tools.rb', line 222

def format_natural_idempotency(idempotency_name)
  @idempotency_name = idempotency_name
end

#format_route_name(name) ⇒ Object



35
36
37
38
39
40
# File 'lib/talented_tools.rb', line 35

def format_route_name(name)
  return name unless name.include?("_")
  result = name.gsub("_", "-")

  result
end

#format_unnatural_end_point_name(end_point_name) ⇒ Object



173
174
175
176
# File 'lib/talented_tools.rb', line 173

def format_unnatural_end_point_name(end_point_name)
  @end_point_name = end_point_name
  @singular_end_point_name = end_point_name.singularize
end

#greater_methods(end_point, attribute, greater_lower) ⇒ Object



310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
# File 'lib/talented_tools.rb', line 310

def greater_methods(end_point, attribute, greater_lower)
  string_variable = "#{attribute} <= \#{payload}"

  method = "  def self.by_#{attribute}_gt(payload)\n    return all unless payload.present?\n\n    where(\""'VARIABLE'"\")\n  end" 

  file = File.open("app/models/#{end_point.singularize}.rb", "r")
  content = file.read
  last_end_index = content.rindex(/\bend\b/)
  new_content = content.insert(last_end_index -1, "\n#{method}\n")
  file.close

  File.open("app/models/#{end_point.singularize}.rb", "w") do |f|
    f.write(new_content)
  end
  
  new_content = content.gsub("VARIABLE", string_variable)
  File.write("app/models/#{end_point.singularize}.rb", new_content)

  attribute_gt = "#{attribute}_gt"
  find_all_controller(end_point, attribute_gt)
end

#implement_natural_key(end_point_name, idempotency_name) ⇒ Object

NATURAL IDEMPOTENCY_KEY



209
210
211
212
213
214
215
# File 'lib/talented_tools.rb', line 209

def implement_natural_key(end_point_name, idempotency_name)
  format_natural_end_point_name(end_point_name)
  format_natural_idempotency(idempotency_name)

  natural_implement_controller
  natural_implement_services
end

#implement_unnatural_key(end_point_name) ⇒ Object

UNNATURAL IDEMPOTENCY_KEY



164
165
166
167
168
169
170
171
# File 'lib/talented_tools.rb', line 164

def implement_unnatural_key(end_point_name)
  format_unnatural_end_point_name(end_point_name)

  unnatural_implement_controller
  unnatural_implement_services

  puts "Successfully created idempotency_key"
end

#lower_methods(end_point, attribute, greater_lower) ⇒ Object



332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
# File 'lib/talented_tools.rb', line 332

def lower_methods(end_point, attribute, greater_lower)
  string_variable = "#{attribute} >= \#{payload}"

  method = "  def self.by_#{attribute}_lt(payload)\n    return all unless payload.present?\n\n    where(\""'VARIABLE'"\")\n  end" 
  file = File.open("app/models/#{end_point.singularize}.rb", "r+")
  content = file.read
  last_end_index = content.rindex(/\bend\b/)
  new_content = content.insert(last_end_index -1, "\n#{method}\n")
  file.close

  File.open("app/models/#{end_point.singularize}.rb", "w") do |f|
    f.write(new_content)
  end

  new_content = content.gsub("VARIABLE", string_variable)
  File.write("app/models/#{end_point.singularize}.rb", new_content)

  attribute_lt = "#{attribute}_lt"
  find_all_controller(end_point, attribute_lt)
end

#natural_implement_controllerObject



226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/talented_tools.rb', line 226

def natural_implement_controller
  constroller_base = File.open("app/controllers/#{@end_point_name}_controller.rb")
  content = constroller_base.read

  replacements = {
  "#name_idempotency_unnatural_controller" => " ",
  "name_create_controller_params" => "valid_params"  
  }

  result = content.gsub(/#{Regexp.union(replacements.keys)}/, replacements)

  File.write("app/controllers/#{@end_point_name}_controller.rb", result)
end

#natural_implement_servicesObject



240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/talented_tools.rb', line 240

def natural_implement_services
  service_base = File.open("app/services/#{@singular_end_point_name}_service.rb")
  content = service_base.read

  replacements = {
    "#unnatural_services_create_nil_validate" => " ",
    "idempotency_name" => "#{@idempotency_name}"
  }

  result = content.gsub(/#{Regexp.union(replacements.keys)}/, replacements)

  File.write("app/services/#{@singular_end_point_name}_service.rb", result)
end

#unnatural_implement_controllerObject



178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/talented_tools.rb', line 178

def unnatural_implement_controller
  constroller_base = File.open("app/controllers/#{@end_point_name}_controller.rb")
  content = constroller_base.read

  replacements = {
  "#name_idempotency_unnatural_controller" => "idempotency_key = headers['x-idempotency-key']",
  "name_create_controller_params" => "valid_params, idempotency_key: idempotency_key"  
  }

  result = content.gsub(/#{Regexp.union(replacements.keys)}/, replacements)

  File.write("app/controllers/#{@end_point_name}_controller.rb", result)

end

#unnatural_implement_servicesObject



193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/talented_tools.rb', line 193

def unnatural_implement_services
  service_base = File.open("app/services/#{@singular_end_point_name}_service.rb")
  content = service_base.read

  replacements = {
    "#unnatural_services_create_nil_validate" => "return [@model, :idempotency_key_not_present] if idempotency_key.nil?",
    "@model.idempotency_name" => "@idempotency_key"
  }

  result = content.gsub(/#{Regexp.union(replacements.keys)}/, replacements)

  File.write("app/services/#{@singular_end_point_name}_service.rb", result)

end