Class: RailsDbViews::DbViewsCreator
- Inherits:
-
Object
- Object
- RailsDbViews::DbViewsCreator
- Defined in:
- lib/rails_db_views/db_views_creator.rb
Instance Attribute Summary collapse
-
#views ⇒ Object
readonly
Returns the value of attribute views.
Instance Method Summary collapse
- #create_views ⇒ Object
- #drop_views ⇒ Object
-
#initialize ⇒ DbViewsCreator
constructor
A new instance of DbViewsCreator.
- #register_files(files) ⇒ Object
Constructor Details
#initialize ⇒ DbViewsCreator
Returns a new instance of DbViewsCreator.
4 5 6 |
# File 'lib/rails_db_views/db_views_creator.rb', line 4 def initialize @views = {} end |
Instance Attribute Details
#views ⇒ Object (readonly)
Returns the value of attribute views.
2 3 4 |
# File 'lib/rails_db_views/db_views_creator.rb', line 2 def views @views end |
Instance Method Details
#create_views ⇒ Object
46 47 48 49 50 51 |
# File 'lib/rails_db_views/db_views_creator.rb', line 46 def create_views reset_views_status! @views.each{ |name, view| create_view name, view } end |
#drop_views ⇒ Object
39 40 41 42 43 44 |
# File 'lib/rails_db_views/db_views_creator.rb', line 39 def drop_views reset_views_status! @views.each{ |name, view| drop_view name, view } 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 |
# File 'lib/rails_db_views/db_views_creator.rb', line 8 def register_files files files.each do |file| view_name = File.basename(file, File.extname(file)) 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: [] } # 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 =~ /^!/ } directives.each do |directive| if directive =~ /^!require / #Currently only the require directive exists. file_obj[:requires] += directive.split(" ")[1..-1] end end if @views[view_name] puts "WARNING: #{view_name} already defined in `#{@views[view_name][:path]}`. Will be ignored and we use `#{file_obj[:path]}`..." end @views[view_name] = file_obj end end |