Sha256: d1045d6b2edb596799602ba594dd01f7eb512deb45d14fb580064f224e28c821

Contents?: true

Size: 1.5 KB

Versions: 2

Compression:

Stored size: 1.5 KB

Contents

begin
  require 'active_record'
rescue LoadError => e
  raise MissingLibrary, "ActiveRecord could not be loaded (is it installed?): #{e.message}"
end

module Capcode
  # This module contains the resources needed in a model
  module Resource
  end
  
  # This class allow you to define models
  Base = ActiveRecord::Base
  
  # Schema info
  class SchemaInfo < Base
  end
  
  class << self
    # This class allow you to define models
    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
    
    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
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
Capcode-0.8.9 lib/capcode/base/ar.rb
Capcode-0.8.8 lib/capcode/base/ar.rb