Class: TableRenamable::DeprecatedTable

Inherits:
Object
  • Object
show all
Defined in:
lib/table_renamable/deprecated_table.rb

Overview

Class that handles the renaming of tables. This sets the table name of the class based on whether or not the table exists in the database

Author:

  • dlangevin

Defined Under Namespace

Classes: NoTableError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, old_name, new_name) ⇒ DeprecatedTable

Constructor - sets up the record and tries to connect to the correct database

Parameters:

  • klass (Class)

    Class whose table we are renaming

  • old_name (String, Symbol)

    The old table name

  • new_name (String, Symbol)

    The new table name



32
33
34
35
36
37
38
# File 'lib/table_renamable/deprecated_table.rb', line 32

def initialize(klass, old_name, new_name)
  @klass = klass
  @old_name = old_name
  @new_name = new_name
  # call to set the correct table name
  self.set_table_name
end

Instance Attribute Details

#klassObject

Returns the value of attribute klass.



14
15
16
# File 'lib/table_renamable/deprecated_table.rb', line 14

def klass
  @klass
end

#new_nameObject

Returns the value of attribute new_name.



22
23
24
# File 'lib/table_renamable/deprecated_table.rb', line 22

def new_name
  @new_name
end

#old_nameObject

Returns the value of attribute old_name.



18
19
20
# File 'lib/table_renamable/deprecated_table.rb', line 18

def old_name
  @old_name
end

Instance Method Details

#get_current_table_nameString

Returns the name of the table that currently exists of our two options (old or new)

Returns:

  • (String)

    The name of the existing table



45
46
47
48
49
50
51
# File 'lib/table_renamable/deprecated_table.rb', line 45

def get_current_table_name
  [self.old_name, self.new_name].each do |name|
    return name.to_s if self.table_exists?(name)
  end
  # raise exception if we don't have a valid table
  self.raise_no_table_error
end

#has_table?(table_name) ⇒ Boolean

Is this table name part of our DeprecatedTable definition

Parameters:

  • table_name (String, Symbol)

    Table name to chck

Returns:

  • (Boolean)

    Whether or not we have it in our definition



58
59
60
# File 'lib/table_renamable/deprecated_table.rb', line 58

def has_table?(table_name)
  [self.old_name, self.new_name].include?(table_name.to_sym)
end

#set_table_nameBoolean

Set the correct table name for the Class we are controlling

Returns:

  • (Boolean)

    True if we set the table name

Raises:

  • (TableRenamable::NoTableError)

    Error if neither name works



68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/table_renamable/deprecated_table.rb', line 68

def set_table_name
  [self.old_name, self.new_name].each do |name|
    # make sure this table exists
    if self.table_exists?(name)
      # return true if we are already using this table
      return true if self.klass.table_name == name.to_s
      # otherwise we can change the table name
      self.klass.table_name = name
      return true
    end
  end
  self.raise_no_table_error
end