Class: RailsDbViews::DatabaseSymbol
- Inherits:
-
Object
- Object
- RailsDbViews::DatabaseSymbol
- Defined in:
- lib/rails_db_views/database_symbol.rb
Defined Under Namespace
Modules: Status
Constant Summary collapse
- STRING_INTERPOLATION =
/((.?)\#\{([^\}]*)\})/
Instance Attribute Summary collapse
-
#inverse_of_required ⇒ Object
Returns the value of attribute inverse_of_required.
-
#marked_as_deleted ⇒ Object
(also: #marked_as_deleted?)
Returns the value of attribute marked_as_deleted.
-
#name ⇒ Object
Returns the value of attribute name.
-
#path ⇒ Object
Returns the value of attribute path.
-
#required ⇒ Object
Returns the value of attribute required.
-
#sql_content ⇒ Object
Returns the value of attribute sql_content.
-
#status ⇒ Object
Returns the value of attribute status.
Instance Method Summary collapse
- #create! ⇒ Object
- #create_sql ⇒ Object
- #drop! ⇒ Object
-
#drop_sql ⇒ Object
Theses methods should be implemented in children objects.
- #handle_error_on_drop ⇒ Object
- #in_progress? ⇒ Boolean
-
#initialize(file_path) ⇒ DatabaseSymbol
constructor
A new instance of DatabaseSymbol.
- #loaded? ⇒ Boolean
- #mark_as_delete! ⇒ Object
- #process_inverse_of_required! ⇒ Object
- #process_string_interpolation(str) ⇒ Object
- #uncommented_sql_content ⇒ Object
- #unloaded? ⇒ Boolean
Constructor Details
#initialize(file_path) ⇒ DatabaseSymbol
Returns a new instance of DatabaseSymbol.
13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/rails_db_views/database_symbol.rb', line 13 def initialize file_path @path = file_path @name = File.basename(file_path, ".*") @status = :none @required = [] @marked_as_deleted = false @sql_content = File.read(@path) @inverse_of_required = [] load_directives end |
Instance Attribute Details
#inverse_of_required ⇒ Object
Returns the value of attribute inverse_of_required.
2 3 4 |
# File 'lib/rails_db_views/database_symbol.rb', line 2 def inverse_of_required @inverse_of_required end |
#marked_as_deleted ⇒ Object Also known as: marked_as_deleted?
Returns the value of attribute marked_as_deleted.
2 3 4 |
# File 'lib/rails_db_views/database_symbol.rb', line 2 def marked_as_deleted @marked_as_deleted end |
#name ⇒ Object
Returns the value of attribute name.
2 3 4 |
# File 'lib/rails_db_views/database_symbol.rb', line 2 def name @name end |
#path ⇒ Object
Returns the value of attribute path.
2 3 4 |
# File 'lib/rails_db_views/database_symbol.rb', line 2 def path @path end |
#required ⇒ Object
Returns the value of attribute required.
2 3 4 |
# File 'lib/rails_db_views/database_symbol.rb', line 2 def required @required end |
#sql_content ⇒ Object
Returns the value of attribute sql_content.
2 3 4 |
# File 'lib/rails_db_views/database_symbol.rb', line 2 def sql_content @sql_content end |
#status ⇒ Object
Returns the value of attribute status.
2 3 4 |
# File 'lib/rails_db_views/database_symbol.rb', line 2 def status @status end |
Instance Method Details
#create! ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/rails_db_views/database_symbol.rb', line 65 def create! return if marked_as_deleted? || loaded? circular_reference_error if in_progress? self.status = Status::IN_PROGRESS required.each do |symbol_name| symbol = RailsDbViews::Factory.get(self.class, symbol_name) not_found_error(symbol_name) if symbol.nil? symbol.create! end ActiveRecord::Base.connection.execute(create_sql) self.status = Status::LOADED end |
#create_sql ⇒ Object
111 112 113 |
# File 'lib/rails_db_views/database_symbol.rb', line 111 def create_sql raise NotImplementedError, "DatabaseSymbol should not be instanciated" end |
#drop! ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/rails_db_views/database_symbol.rb', line 83 def drop! return if loaded? circular_reference_error if in_progress? self.status = Status::IN_PROGRESS # We start by the required one to delete first. inverse_of_required.each do |symbol_name| symbol = RailsDbViews::Factory.get(self.class, symbol_name) not_found_error(symbol_name) if symbol.nil? symbol.drop! end begin ActiveRecord::Base.connection.execute(drop_sql) rescue ActiveRecord::ActiveRecordError => e #Probably because the symbol doesn't exists yet. handle_error_on_drop end self.status = Status::LOADED end |
#drop_sql ⇒ Object
Theses methods should be implemented in children objects.
107 108 109 |
# File 'lib/rails_db_views/database_symbol.rb', line 107 def drop_sql raise NotImplementedError, "DatabaseSymbol should not be instanciated" end |
#handle_error_on_drop ⇒ Object
115 116 117 |
# File 'lib/rails_db_views/database_symbol.rb', line 115 def handle_error_on_drop raise NotImplementedError, "DatabaseSymbol should not be instanciated" end |
#in_progress? ⇒ Boolean
42 43 44 |
# File 'lib/rails_db_views/database_symbol.rb', line 42 def in_progress? status == Status::IN_PROGRESS end |
#loaded? ⇒ Boolean
38 39 40 |
# File 'lib/rails_db_views/database_symbol.rb', line 38 def loaded? status == Status::LOADED end |
#mark_as_delete! ⇒ Object
34 35 36 |
# File 'lib/rails_db_views/database_symbol.rb', line 34 def mark_as_delete! @marked_as_deleted = true end |
#process_inverse_of_required! ⇒ Object
26 27 28 29 30 31 32 |
# File 'lib/rails_db_views/database_symbol.rb', line 26 def process_inverse_of_required! @required.each do |name| required = RailsDbViews::Factory.get(self.class, name) not_found_error if required.nil? required.inverse_of_required << self.name end end |
#process_string_interpolation(str) ⇒ Object
51 52 53 54 55 56 57 58 59 |
# File 'lib/rails_db_views/database_symbol.rb', line 51 def process_string_interpolation str str.gsub(STRING_INTERPOLATION) do |x| if $2 == '\\' $1[1..-1] #Rendering the whole expression because of escape char. else $2 + (TOPLEVEL_BINDING.eval($3)).to_s end end end |
#uncommented_sql_content ⇒ Object
61 62 63 |
# File 'lib/rails_db_views/database_symbol.rb', line 61 def uncommented_sql_content process_string_interpolation(sql_content.split("\n").reject{|x| x=~ COMMENTS }.join("\n")) end |