Class: ReversibleData::Table

Inherits:
Object
  • Object
show all
Defined in:
lib/reversible_data/table.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, opts = {}, &blk) ⇒ Table

Returns a new instance of Table.



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/reversible_data/table.rb', line 9

def initialize(name, opts = {}, &blk)
  @name             = name.to_s.underscore.to_sym
  @table_name       = (opts[:table_name] || @name.to_s.tableize).to_s
  @model_name       = (opts[:class_name] || @name.to_s.classify).to_s
  @migrator         = blk
  @model_definition = nil
  default_options   = {:skip_model => Object.const_defined?(@model_name),
                       :skip_table => (connected? && connection.table_exists?(@table_name))}
  @options          = opts.reverse_merge(default_options)
  @blueprint        = nil
  self.known_models[@name] = self
end

Instance Attribute Details

#model_name ⇒ Object

Returns the value of attribute model_name.



7
8
9
# File 'lib/reversible_data/table.rb', line 7

def model_name
  @model_name
end

#name ⇒ Object

Returns the value of attribute name.



7
8
9
# File 'lib/reversible_data/table.rb', line 7

def name
  @name
end

#options ⇒ Object

Returns the value of attribute options.



7
8
9
# File 'lib/reversible_data/table.rb', line 7

def options
  @options
end

#table_name ⇒ Object

Returns the value of attribute table_name.



7
8
9
# File 'lib/reversible_data/table.rb', line 7

def table_name
  @table_name
end

Instance Method Details

#blueprint(&blk) ⇒ Object



73
74
75
# File 'lib/reversible_data/table.rb', line 73

def blueprint(&blk)
  @blueprint = blk unless blk.nil?
end

#clear_model_definition! ⇒ Object



77
78
79
# File 'lib/reversible_data/table.rb', line 77

def clear_model_definition!
  @model_definition = nil
end

#create_model(autoremove = true) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/reversible_data/table.rb', line 59

def create_model(autoremove = true)
  return if skip_model?
  remove_model if autoremove
  return if Object.const_defined?(@model_name)
  @model = Class.new(ActiveRecord::Base)
  @model.class_eval(&@model_definition) unless @model_definition.nil?
  @model.blueprint(&@blueprint) unless @blueprint.nil? || !@model.respond_to?(:blueprint)
  Object.const_set(@model_name, @model)
end

#create_table(autodrop = true) ⇒ Object



45
46
47
48
49
50
# File 'lib/reversible_data/table.rb', line 45

def create_table(autodrop = true)
  return if skip_table? || !connected?
  drop_table if autodrop
  return if connection.table_exists?(@table_name)
  connection.create_table(table_name, &@migrator)
end

#define_model(&blk) ⇒ Object



69
70
71
# File 'lib/reversible_data/table.rb', line 69

def define_model(&blk)
  @model_definition = blk unless blk.nil?
end

#destroy ⇒ Object



81
82
83
# File 'lib/reversible_data/table.rb', line 81

def destroy
  self.known_models.delete(@name)
end

#down! ⇒ Object



35
36
37
38
# File 'lib/reversible_data/table.rb', line 35

def down!
  remove_model
  drop_table
end

#drop_table ⇒ Object



40
41
42
43
# File 'lib/reversible_data/table.rb', line 40

def drop_table
  return if skip_table? || !connected?
  connection.drop_table(@table_name) if connection.table_exists?(@table_name)
end

#remove_model ⇒ Object



52
53
54
55
56
57
# File 'lib/reversible_data/table.rb', line 52

def remove_model
  return if skip_model?
  if Object.const_defined?(@model_name)
    silence_warnings { Object.send(:remove_const, @model_name) }
  end
end

#skip_model? ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/reversible_data/table.rb', line 22

def skip_model?
  !!@options[:skip_model]
end

#skip_table? ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/reversible_data/table.rb', line 26

def skip_table?
  !!@options[:skip_table]
end

#up! ⇒ Object



30
31
32
33
# File 'lib/reversible_data/table.rb', line 30

def up!
  create_table
  create_model
end