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



56
57
58
59
60
61
62
63
# File 'lib/rails_db_objects/db_objects_creator.rb', line 56

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

#drop_objectsObject



47
48
49
50
51
52
53
54
# File 'lib/rails_db_objects/db_objects_creator.rb', line 47

def drop_objects
  reset_objects_status!
  @objects.keys.each do |object_type|
    @objects[object_type].each do |name, object|
      drop_object(object_type, name, object) unless object[:skip_pre]
    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
# 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 = {
      path: file,
      sql_content: sql_content,
      status: :none,
      requires: [],
      silent: false,
      keep: false,
      deleted: false,
      dbschema: Rails.configuration.rails_db_objects[:objects_dbschema],
      dropsql: [],
      createsql: [],
      vanilla: false,
      condition: []
    }

    # 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