Module: Mappd::ClassMethods

Defined in:
lib/mappd/mappd.rb

Instance Method Summary collapse

Instance Method Details

#associations!Object



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/mappd/mappd.rb', line 29

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



48
49
50
# File 'lib/mappd/mappd.rb', line 48

def commands
  @commands ||= []
end

#drop(name) ⇒ Object



17
18
19
# File 'lib/mappd/mappd.rb', line 17

def drop(name)
  commands << [:remove_column, [table_name, name]]
end

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



21
22
23
# File 'lib/mappd/mappd.rb', line 21

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

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



9
10
11
# File 'lib/mappd/mappd.rb', line 9

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

#migrate!Object



42
43
44
45
46
# File 'lib/mappd/mappd.rb', line 42

def migrate!
  create_table!
  associations!
  execute_commands!
end

#rename(from, to) ⇒ Object



13
14
15
# File 'lib/mappd/mappd.rb', line 13

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

#timestampsObject



25
26
27
# File 'lib/mappd/mappd.rb', line 25

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