Module: ApacheAge::GeneratorEntityHelpers

Included in:
EdgeGenerator, NodeGenerator
Defined in:
lib/generators/apache_age/generator_entity_helpers.rb

Instance Method Summary collapse

Instance Method Details

#add_type_configObject



43
44
45
46
47
48
49
50
# File 'lib/generators/apache_age/generator_entity_helpers.rb', line 43

def add_type_config
  return unless File.exist?(types_config_file)

  types_content = File.read(types_config_file)
  types_content.sub!(/^end\s*$/, "#{new_type_content}end")
  File.open(types_config_file, 'w') { |file| file.write(types_content) }
  puts "      modified: config/initializers/types.rb"
end

#attributes_listObject



19
20
21
# File 'lib/generators/apache_age/generator_entity_helpers.rb', line 19

def attributes_list
  attributes.map { |attr| { name: attr.name, type: attr.type } }
end

#destroy_age_entity(age_type) ⇒ Object



13
14
15
16
17
# File 'lib/generators/apache_age/generator_entity_helpers.rb', line 13

def destroy_age_entity(age_type)
  file_path = File.join(Rails.root, "app/#{age_type}s", class_path, "#{file_name}.rb")
  File.delete(file_path) if File.exist?(file_path)
  remove_type_config
end

#full_class_nameObject



31
32
33
# File 'lib/generators/apache_age/generator_entity_helpers.rb', line 31

def full_class_name
  parent_module.empty? ? class_name : "#{parent_module}::#{class_name}"
end

#generate_age_entity(age_type) ⇒ Object



5
6
7
8
9
10
11
# File 'lib/generators/apache_age/generator_entity_helpers.rb', line 5

def generate_age_entity(age_type)
  template(
    "#{age_type}.rb.tt",
    File.join(Rails.root, "app/#{age_type}s", class_path, "#{file_name}.rb")
  )
  add_type_config
end

#indented_namespaceObject



35
36
37
38
39
40
41
# File 'lib/generators/apache_age/generator_entity_helpers.rb', line 35

def indented_namespace
  return '' if parent_module.empty?

  parent_module.split('::').map.with_index do |namespace, index|
    "#{'  ' * index}module #{namespace}"
  end.join("\n") + "\n"
end

#new_type_contentObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/generators/apache_age/generator_entity_helpers.rb', line 64

def new_type_content
  file_path = [class_path, file_name].reject(&:blank?).join('/').downcase
  entity_namespace = class_path.map(&:capitalize).join('::')
  entity_class_name = file_name.split('_').map(&:capitalize).join
  entity_namespaced_class = [entity_namespace, entity_class_name].reject(&:blank?).join('::')
  type_name = [class_path.join('_'), file_name].reject(&:blank?).join('_')
  content =
<<-RUBY
  require_dependency '#{file_path}'
  ActiveModel::Type.register(
:#{type_name}, ApacheAge::Types::Factory.type_for(#{entity_namespaced_class})
  )
RUBY
end

#parent_moduleObject



23
24
25
# File 'lib/generators/apache_age/generator_entity_helpers.rb', line 23

def parent_module
  class_path.map(&:camelize).join('::')
end

#remove_type_configObject



52
53
54
55
56
57
58
59
60
# File 'lib/generators/apache_age/generator_entity_helpers.rb', line 52

def remove_type_config
  return unless File.exist?(types_config_file)

  type_to_remove = new_type_content

  types_content = File.read(types_config_file)
  types_content.gsub!(type_to_remove, '')
  File.open(types_config_file, 'w') { |file| file.write(types_content) }
end

#types_config_fileObject



62
# File 'lib/generators/apache_age/generator_entity_helpers.rb', line 62

def types_config_file = File.join(Rails.root, 'config/initializers/types.rb')

#unique_attributesObject



27
28
29
# File 'lib/generators/apache_age/generator_entity_helpers.rb', line 27

def unique_attributes
  attributes_list.map { |attr| attr[:name].to_sym }
end