Module: Capcode

Defined in:
lib/capcode/base/ar.rb

Defined Under Namespace

Modules: Resource Classes: SchemaInfo

Constant Summary collapse

Base =

This class allow you to define models

ActiveRecord::Base

Class Method Summary collapse

Class Method Details

.db_connect(dbfile, logfile) ⇒ Object

:nodoc:



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/capcode/base/ar.rb', line 30

def db_connect( dbfile, logfile ) #:nodoc:
  dbconfig = YAML::load(File.open(dbfile)).keys_to_sym
  version = dbconfig.delete(:schema_version) { |_| @final }
  
  ActiveRecord::Base.establish_connection(dbconfig)
  ActiveRecord::Base.logger = Logger.new(logfile)
  
  if @migrations
    unless SchemaInfo.table_exists?
      ActiveRecord::Schema.define do
        create_table SchemaInfo.table_name do |t|
          t.column :version, :float
        end
      end
    end
    si = SchemaInfo.find(:first) || SchemaInfo.new(:version => 0)
    @migrations.each do |k|
      k.migrate(:up) if si.version < k.version and k.version <= version
      k.migrate(:down) if si.version >= k.version and k.version > version
    end
    si.update_attributes(:version => version)
  end
end

.Model(n) ⇒ Object

This class allow you to define models



21
22
23
24
25
26
27
28
# File 'lib/capcode/base/ar.rb', line 21

def Model( n )
  @final = [n, @final.to_f].max
  m = (@migrations ||= [])
  Class.new(ActiveRecord::Migration) do
    meta_def(:version) { n }
    meta_def(:inherited) { |k| m << k }
  end
end