Class: Adjective::CreateAdjectiveClass

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

Instance Method Summary collapse

Constructor Details

#initialize(class_name, modules = []) ⇒ CreateAdjectiveClass

Returns a new instance of CreateAdjectiveClass.



3
4
5
6
7
# File 'lib/templates/create_adjective_class.rb', line 3

def initialize(class_name, modules = [])
  @class_name = class_name
  @modules = modules.map{|m| m&.downcase}
  @module_classes = given_modules
end

Instance Method Details

#base_templateObject



32
33
34
35
36
37
38
# File 'lib/templates/create_adjective_class.rb', line 32

def base_template
  <<~TEMPLATE
  class {{class_name}} {{active_record_inheritance}}
  {{modules_string}}
  end
  TEMPLATE
end

#file_class_nameObject



9
10
11
# File 'lib/templates/create_adjective_class.rb', line 9

def file_class_name
  @class_name.capitalize
end

#file_nameObject



13
14
15
# File 'lib/templates/create_adjective_class.rb', line 13

def file_name
  @class_name.downcase + ".rb"
end

#given_modulesObject



17
18
19
20
21
# File 'lib/templates/create_adjective_class.rb', line 17

def given_modules
  @modules.map do |mod|
    klass = "Adjective::#{mod.capitalize}".constantize
  end      
end

#inherited_classesObject



27
28
29
30
# File 'lib/templates/create_adjective_class.rb', line 27

def inherited_classes
  return "< ApplicationRecord" if Adjective.configuration.use_active_record
  return ""
end

#modules_stringObject



23
24
25
# File 'lib/templates/create_adjective_class.rb', line 23

def modules_string
  @modules.map {|mod| "  include Adjective::#{mod.capitalize}"}.join("\n")
end

#renderObject



40
41
42
43
44
45
46
# File 'lib/templates/create_adjective_class.rb', line 40

def render
  base = base_template
  base
    .gsub("{{class_name}}", file_class_name.lstrip)
    .gsub("{{active_record_inheritance}}", inherited_classes.lstrip)
    .gsub("{{modules_string}}", modules_string)
end