Module: Mappd::ClassMethods

Defined in:
lib/mappd/mappd.rb

Instance Method Summary collapse

Instance Method Details

#associations!Object



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/mappd/mappd.rb', line 23

def associations!
  reflect_on_all_associations.each do |association|
    case association.macro
    when :belongs_to
      commands << [:add_reference, [table_name, association.name, association.options]]
    when :has_and_belongs_to_many
      commands << [:create_join_table,
                   [table_name.to_sym, association.name]]
    end
  end
end

#commandsObject



41
42
43
# File 'lib/mappd/mappd.rb', line 41

def commands
  @commands ||= []
end

#field(name, type = :string, options = {}) ⇒ Object



15
16
17
# File 'lib/mappd/mappd.rb', line 15

def field(name, type = :string, options = {})
  commands << [:add_column, [table_name, name, type, options]]
end

#index(name, options = {}) ⇒ Object



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

def index(name, options = {})
  commands << [:add_index, [table_name, name, options]]
end

#migrate!Object



35
36
37
38
39
# File 'lib/mappd/mappd.rb', line 35

def migrate!
  create_table!
  associations!
  execute_commands!
end

#rename(from, to) ⇒ Object



11
12
13
# File 'lib/mappd/mappd.rb', line 11

def rename(from, to)
  commands << [:rename_column, [table_name, from, to]]
end

#timestampsObject



19
20
21
# File 'lib/mappd/mappd.rb', line 19

def timestamps
  commands << [:add_timestamps, [table_name, null: true]]
end