Class: RailsDbObjects::DbObjectsCreator
- Inherits:
-
Object
- Object
- RailsDbObjects::DbObjectsCreator
- Defined in:
- lib/rails_db_objects/db_objects_creator.rb
Instance Attribute Summary collapse
-
#objects ⇒ Object
readonly
Returns the value of attribute objects.
Instance Method Summary collapse
- #create_objects ⇒ Object
- #drop_objects ⇒ Object
-
#initialize ⇒ DbObjectsCreator
constructor
A new instance of DbObjectsCreator.
- #register_files(files) ⇒ Object
Constructor Details
#initialize ⇒ DbObjectsCreator
Returns a new instance of DbObjectsCreator.
4 5 6 |
# File 'lib/rails_db_objects/db_objects_creator.rb', line 4 def initialize @objects = {} end |
Instance Attribute Details
#objects ⇒ Object (readonly)
Returns the value of attribute objects.
2 3 4 |
# File 'lib/rails_db_objects/db_objects_creator.rb', line 2 def objects @objects end |
Instance Method Details
#create_objects ⇒ Object
85 86 87 88 89 90 91 92 |
# File 'lib/rails_db_objects/db_objects_creator.rb', line 85 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_objects ⇒ Object
76 77 78 79 80 81 82 83 |
# File 'lib/rails_db_objects/db_objects_creator.rb', line 76 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
8 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 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/rails_db_objects/db_objects_creator.rb', line 8 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 = content_lines.select{ |x| x.strip =~ /^--/ || x.strip =~ /^#/ }.map(&:strip).map{ |x| x =~ /^--/ ? x[2..-1] : x[1..-1] }.select{|x| x =~ /^!/ } #puts "directives: #{directives.inspect}" directives.each do |directive| if directive =~ /^!require / file_obj[:requires] += directive.split(" ")[1..-1] end if directive =~ /^!vanilla/ file_obj[:vanilla] = true end if directive =~ /^!deleted/ file_obj[:skip_post] = true end if directive =~ /^!silent/ file_obj[:silent] = true end if directive =~ /^!keep/ file_obj[:skip_pre] = true end if directive =~ /^!schema/ file_obj[:dbschema] = directive.split(" ")[1..-1] end if directive =~ /^!condition / file_obj[:condition] << directive.split(" ")[1..-1].join(" ") end if directive =~ /^!dropsql / file_obj[:dropsql] << directive.split(" ")[1..-1].join(" ") end if directive =~ /^!createsql / file_obj[:createsql] << directive.split(" ")[1..-1].join(" ") end end @objects[object_type][object_name] = file_obj end end |