Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Release candidate for 0.1, rough around the edges

  • Loading branch information...
commit 1e1aa8dc23d354ca449d4d5ce524a17867413466 1 parent 8834b07
Kenneth Kalmer authored
View
2  .gitignore
@@ -0,0 +1,2 @@
+nbproject
+*.gem
View
4 CHANGELOG
@@ -0,0 +1,4 @@
+ActiveRecord::Tableless CHANGELOG
+---------------------------------
+
+2008-05-01 - Initial code, rough around the edges.
View
6 README
@@ -0,0 +1,6 @@
+ActiveRecord Tableless Models
+-----------------------------
+
+A single implementation of the ActiveRecord Tableless Model pattern for any
+Rails project or other Ruby project that uses ActiveRecord.
+
View
14 Rakefile
@@ -0,0 +1,14 @@
+require 'rake/rdoctask'
+
+task :default do
+ puts
+ puts IO.read( File.dirname(__FILE__) + '/README' )
+ puts
+end
+
+Rake::RDocTask.new do |rd|
+ rd.main = 'README'
+ rd.rdoc_files.include( 'README', 'lib/*.rb', 'lib/**/*.rb' )
+ rd.rdoc_dir = 'rdoc'
+ rd.title = 'ActiveRecord Tableless Models'
+end
View
18 activerecord-tableless-models.gemspec
@@ -0,0 +1,18 @@
+require 'rubygems'
+
+spec = Gem::Specification.new do |spec|
+ spec.name = 'tablelessmodels'
+ spec.summary = %q{A library for implementing tableless ActiveRecord models}
+ spec.description = %q{ActiveRecord Tableless Models provides a simple mixin for creating models that are not bound to the database. This approach is mostly useful for capitalizing on the features ActiveRecord::Validation}
+ spec.author = "Kenneth Kalmer"
+ spec.email = "kenneth.kalmer@gmail.com"
+ spec.homepage = "http://www.opensourcery.co.za/activerecordtableless/"
+ spec.files = [ "Rakefile", "README", "CHANGELOG" ]
+ spec.files + Dir["lib/*.rb"]
+ spec.version = "0.1.0"
+ spec.add_dependency("activerecord")
+ spec.has_rdoc = true
+ spec.extra_rdoc_files = %w( README CHANGELOG )
+ spec.rdoc_options.concat ['--main', 'README']
+ spec.rubyforge_project = 'tablelessmodels'
+end
View
4 init.rb
@@ -0,0 +1,4 @@
+# For use by Railed and de-Railed applications alike
+
+require 'activerecord'
+require File.dirname(__FILE__) + '/lib/tableless'
View
74 lib/tableless.rb
@@ -0,0 +1,74 @@
+# See #ActiveRecord::Tableless
+
+module ActiveRecord
+
+ # = ActiveRecord::Tableless
+ #
+ # Allow classes to behave like ActiveRecord models, but without an associated
+ # database table. A great way to capitalize on validations. Based on the
+ # original post at http://www.railsweenie.com/forums/2/topics/724 (which seems
+ # to have disappeared from the face of the earth).
+ #
+ # = Example usage
+ #
+ # class ContactMessage < ActiveRecord::Base
+ #
+ # has_no_table
+ #
+ # column :name, :string
+ # column :email, :string
+ # column :message, :string
+ #
+ # end
+ #
+ # msg = ContactMessage.new( params[:msg] )
+ # if msg.valid?
+ # ContactMessageSender.deliver_message( msg )
+ # redirect_to :action => :sent
+ # end
+ #
+ module Tableless
+
+ def self.included( base ) #:nodoc:
+ base.send( :extend, ClassMethods )
+ end
+
+ module ClassMethods #:nodoc:
+
+ # A model that needs to be tableless will call this method to indicate
+ # it.
+ def has_no_table
+ # keep our options handy
+ write_inheritable_attribute(
+ :tableless_options,
+ :columns => []
+ )
+ class_inheritable_reader :tableless_options
+
+ # extend
+ extend ActiveRecord::Tableless::SingletonMethods
+
+ # setup columns
+ end
+
+ end
+
+ module SingletonMethods
+
+ # Return the list of columns registered for the model. Used internally by
+ # ActiveRecord
+ def columns
+ tableless_options[:columns]
+ end
+
+ # Register a new column.
+ def column(name, sql_type = nil, default = nil, null = true)
+ tableless_options[:columns] << ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, sql_type.to_s, null)
+ end
+
+ end
+
+ end
+end
+
+ActiveRecord::Base.send( :include, ActiveRecord::Tableless )
Please sign in to comment.
Something went wrong with that request. Please try again.