Module: Bj::Table::ClassMethods

Defined in:
lib/bj/table.rb

Instance Method Summary collapse

Instance Method Details

#content_column_namesObject



61
62
63
# File 'lib/bj/table.rb', line 61

def content_column_names
  @content_column_names = content_columns.map{|column| column.name}
end

#create_hash_for(options) ⇒ Object



65
66
67
68
69
70
71
72
73
# File 'lib/bj/table.rb', line 65

def create_hash_for options
  options.to_options!
  hash = {}
  content_column_names.each do |key|
    key = key.to_s.to_sym
    hash[key] = options[key]
  end
  hash
end

#downObject



44
45
46
# File 'lib/bj/table.rb', line 44

def down
  migration_class.down
end

#each(*a, &b) ⇒ Object



75
76
77
# File 'lib/bj/table.rb', line 75

def each *a, &b
  list.each *a, &b
end

#generate_migrationObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/bj/table.rb', line 10

def generate_migration
  before = Dir.glob "./db/migrate/*"
  Util.spawn "./script/generate migration BjMigration"
  after = Dir.glob "./db/migrate/*"
  candidates = after - before
  case candidates.size
    when 0
      false
    when 1
      generated = candidates.first
      open(generated, "w"){|fd| fd.puts migration_code}
      generated
    else
      raise "ambiguous migration <#{ candidates.inspect }>"
  end
end

#migration_classObject



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/bj/table.rb', line 48

def migration_class
  table = self
  @migration_class ||=
    Class.new(ActiveRecord::Migration) do
      sc =
        class << self
          self
        end
      sc.module_eval{ attribute :table => table }
      sc.module_eval &table.migration
    end
end

#migration_codeObject



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/bj/table.rb', line 27

def migration_code
  <<-code
    class BjMigration < ActiveRecord::Migration
      def self.up
        Bj::Table.each{|table| table.up}
      end
      def self.down
        Bj::Table.reverse_each{|table| table.down}
      end
    end
  code
end

#reverse_each(*a, &b) ⇒ Object



79
80
81
# File 'lib/bj/table.rb', line 79

def reverse_each *a, &b
  list.reverse.each *a, &b
end

#upObject



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

def up
  migration_class.up
end