Sha256: 49b0ae9e39909b851388249429c5946b471247b2e7c1baccd6cfa9383b098f9e

Contents?: true

Size: 1.26 KB

Versions: 2

Compression:

Stored size: 1.26 KB

Contents

require_relative 'get_finite.rb'
require_relative 'external_includes.rb'

module MasterTable
  def self.table_name
    :active_finites
  end
  def self.column_name
    default_column_name self.table_name
  end
  def self.table
    get_finite self.table_name, :do_not_redefine_accessor
  end
  def self.all
    if self.table.table_exists?
      self.table
      .all
      .collect {|x| x.send self.column_name.to_s}
      .collect {|x| get_finite x}
    else
      []
    end
  end
  def self.add table_name
    if not self.table.table_exists?
      ActiveRecord::Schema.define do
        create_table MasterTable.table_name do |t|
          t.string MasterTable.column_name, :null => false
        end
        add_index MasterTable.table_name, MasterTable.column_name, :unique => true
      end
    end
    if self.table.where(self.column_name => table_name).empty?
      new_finite = self.table.new
      new_finite.send self.column_name.to_s + '=', table_name
      new_finite.save
    end
  end
  def self.delete table_name
    matches = self.table.where self.column_name => table_name
    matches.each do |m|
      self.table.destroy m
    end
    if self.table.count.eql? 0
      ActiveRecord::Schema.define do
        drop_table MasterTable.table_name
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
active_finite-0.4.2 lib/master_table.rb
active_finite-0.4.1 lib/master_table.rb