Class: RailsDbViews::DatabaseSymbol

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_db_views/database_symbol.rb

Direct Known Subclasses

Function, View

Defined Under Namespace

Modules: Status Classes: CircularReferenceError, SymbolNotFound

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_path) ⇒ DatabaseSymbol

Returns a new instance of DatabaseSymbol.



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/rails_db_views/database_symbol.rb', line 14

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_requiredObject

Returns the value of attribute inverse_of_required.



5
6
7
# File 'lib/rails_db_views/database_symbol.rb', line 5

def inverse_of_required
  @inverse_of_required
end

#marked_as_deletedObject Also known as: marked_as_deleted?

Returns the value of attribute marked_as_deleted.



5
6
7
# File 'lib/rails_db_views/database_symbol.rb', line 5

def marked_as_deleted
  @marked_as_deleted
end

#nameObject

Returns the value of attribute name.



5
6
7
# File 'lib/rails_db_views/database_symbol.rb', line 5

def name
  @name
end

#pathObject

Returns the value of attribute path.



5
6
7
# File 'lib/rails_db_views/database_symbol.rb', line 5

def path
  @path
end

#requiredObject

Returns the value of attribute required.



5
6
7
# File 'lib/rails_db_views/database_symbol.rb', line 5

def required
  @required
end

#sql_contentObject

Returns the value of attribute sql_content.



5
6
7
# File 'lib/rails_db_views/database_symbol.rb', line 5

def sql_content
  @sql_content
end

#statusObject

Returns the value of attribute status.



5
6
7
# File 'lib/rails_db_views/database_symbol.rb', line 5

def status
  @status
end

Instance Method Details

#create!Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/rails_db_views/database_symbol.rb', line 51

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_sqlObject

Raises:

  • (NotImplementedError)


97
98
99
# File 'lib/rails_db_views/database_symbol.rb', line 97

def create_sql
  raise NotImplementedError, "DatabaseSymbol should not be instanciated"
end

#drop!Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/rails_db_views/database_symbol.rb', line 69

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_sqlObject

Theses methods should be implemented in children objects.

Raises:

  • (NotImplementedError)


93
94
95
# File 'lib/rails_db_views/database_symbol.rb', line 93

def drop_sql
  raise NotImplementedError, "DatabaseSymbol should not be instanciated"
end

#handle_error_on_dropObject

Raises:

  • (NotImplementedError)


101
102
103
# File 'lib/rails_db_views/database_symbol.rb', line 101

def handle_error_on_drop
  raise NotImplementedError, "DatabaseSymbol should not be instanciated"
end

#in_progress?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/rails_db_views/database_symbol.rb', line 43

def in_progress?
  status == Status::IN_PROGRESS
end

#loaded?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/rails_db_views/database_symbol.rb', line 39

def loaded?
  status == Status::LOADED
end

#mark_as_delete!Object



35
36
37
# File 'lib/rails_db_views/database_symbol.rb', line 35

def mark_as_delete!
  @marked_as_deleted = true
end

#process_inverse_of_required!Object



27
28
29
30
31
32
33
# File 'lib/rails_db_views/database_symbol.rb', line 27

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

#unloaded?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/rails_db_views/database_symbol.rb', line 47

def unloaded?
  status == Status::UNLOADED
end