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

Constant Summary collapse

STRING_INTERPOLATION =
/((.?)\#\{([^\}]*)\})/

Instance Attribute Summary collapse

Instance Method Summary collapse

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_requiredObject

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_deletedObject 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

#nameObject

Returns the value of attribute name.



2
3
4
# File 'lib/rails_db_views/database_symbol.rb', line 2

def name
  @name
end

#pathObject

Returns the value of attribute path.



2
3
4
# File 'lib/rails_db_views/database_symbol.rb', line 2

def path
  @path
end

#requiredObject

Returns the value of attribute required.



2
3
4
# File 'lib/rails_db_views/database_symbol.rb', line 2

def required
  @required
end

#sql_contentObject

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

#statusObject

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_sqlObject

Raises:

  • (NotImplementedError)


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_sqlObject

Theses methods should be implemented in children objects.

Raises:

  • (NotImplementedError)


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_dropObject

Raises:

  • (NotImplementedError)


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

Returns:

  • (Boolean)


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

def in_progress?
  status == Status::IN_PROGRESS
end

#loaded?Boolean

Returns:

  • (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_contentObject



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

#unloaded?Boolean

Returns:

  • (Boolean)


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

def unloaded?
  status == Status::UNLOADED
end