Class: RailsDbObjects::DbObjectsCreator

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDbObjectsCreator

Returns a new instance of DbObjectsCreator.



5
6
7
# File 'lib/rails_db_objects/db_objects_creator.rb', line 5

def initialize
  @objects = {}
end

Instance Attribute Details

#objectsObject (readonly)

Returns the value of attribute objects.



3
4
5
# File 'lib/rails_db_objects/db_objects_creator.rb', line 3

def objects
  @objects
end

Instance Method Details

#create_objectsObject



72
73
74
75
76
77
78
79
# File 'lib/rails_db_objects/db_objects_creator.rb', line 72

def create_objects
  reset_objects_status!
  @objects.keys.each do |object_type|
    @objects[object_type].each do |_name, object|
      create_object(object)
    end
  end
end

#drop_objectsObject



63
64
65
66
67
68
69
70
# File 'lib/rails_db_objects/db_objects_creator.rb', line 63

def drop_objects
  reset_objects_status!
  @objects.keys.each do |object_type|
    @objects[object_type].each do |_name, object|
      drop_object(object)
    end
  end
end

#register_files(files) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/rails_db_objects/db_objects_creator.rb', line 9

def register_files(files)
  files.each do |file|
    object_name = File.basename(file, File.extname(file))
    object_type = File.dirname(file.to_s).to_s.split('/').last.upcase

    @objects[object_type] ||= {}

    content = File.read(file)
    content_lines = content.split("\n")

    # Reject the commented lines from the file
    sql_content = content_lines.reject { |x| x.strip =~ /^--/ || x.strip =~ /^#/ }.join("\n")

    file_obj = {
      name: object_name,
      type: object_type,
      debug: false,
      directives: [],
      path: file,
      sql_content: sql_content,
      status: :none,
      requires: [],
      silent: false,
      keep: false,
      nodrop: false,
      deleted: false,
      nocreate: false,
      dbschema: Rails.configuration.rails_db_objects[:objects_dbschema],
      dropsql: [],
      createsql: [],
      vanilla: false,
      condition: [],
      dropconditionruby: [],
      createconditionruby: [],
      beforedropruby: [],
      beforedropsql: [],
      afterdropruby: [],
      afterdropsql: [],
      beforecreateruby: [],
      beforecreatesql: [],
      aftercreateruby: [],
      aftercreatesql: []
    }

    # Detect directives in commentary
    directives = extract_from_comments(content_lines)

    # puts "directives: #{directives.inspect}"
    directives.each { |directive| prepare_directive(file_obj, directive) }

    @objects[object_type][object_name] = file_obj
  end
end