Permalink
Browse files

added ability to access klass definition in block

  • Loading branch information...
1 parent 78b70c8 commit 99ae1ec40db8ef91cc63d7972c8e7fcd501685aa @nakajima committed Oct 26, 2008
Showing with 60 additions and 20 deletions.
  1. +12 −3 README.textile
  2. +3 −17 lib/acts_as_fu.rb
  3. +39 −0 lib/acts_as_fu/helper.rb
  4. +6 −0 spec/acts_as_fu_spec.rb
View
15 README.textile
@@ -24,6 +24,10 @@ Then in your specs:
text :omg_omg_bio
string :name
string :favorite_scene
+
+ def awesome?(show_name)
+ show_name == "Gilmore Girls"
+ end
end
end
@@ -33,15 +37,20 @@ Then in your specs:
nerd.errors.on(:favorite_scene).should_not be_nil
end
+ it "should think gilmore girls is awesome"
+ nerd = Nerd.new
+ nerd.awesome?("Gilmore Girls").should be_true
+ end
+
it "has other stuff" do
# ETC!
end
end
The @build_model@ method allows you to build an ActiveRecord on
-the fly. It takes a block where you can specify columns. The table
-will be created in in an in-memory sqlite3 database. If you need
-another adapter, this project ain't for you. Sorry.
+the fly. It takes a block where you can specify columns and methods.
+The table will be created in in an in-memory sqlite3 database. If
+you need another adapter, this project ain't for you. Sorry.
TODO
View
20 lib/acts_as_fu.rb
@@ -1,21 +1,7 @@
$LOAD_PATH << File.dirname(__FILE__) + '/acts_as_fu'
-%w(rubygems activerecord constants).each { |lib| require lib }
-
module ActsAsFu
- def build_model(name, &block)
- ActiveRecord::Base.establish_connection({
- :adapter => "sqlite3",
- :database => ":memory:"
- })
-
- ActiveRecord::Base.connection.create_table name, :force => true do |table|
- table.instance_eval(&block)
- end
-
- klass_name = name.to_s.classify
-
- Object.send(:remove_const, klass_name) rescue nil
- Object.const_set(klass_name, Class.new(ActiveRecord::Base))
- end
+ VERSION = '0.0.2'
end
+
+%w(rubygems activerecord constants helper).each { |lib| require lib }
View
39 lib/acts_as_fu/helper.rb
@@ -0,0 +1,39 @@
+module ActsAsFu
+ def build_model(name, &block)
+ ActiveRecord::Base.establish_connection({
+ :adapter => "sqlite3",
+ :database => ":memory:"
+ })
+
+ ActiveRecord::Base.connection.create_table(name, :force => true) { }
+
+ klass_name = name.to_s.classify
+
+ Object.send(:remove_const, klass_name) rescue nil
+ Object.const_set(klass_name, Class.new(ActiveRecord::Base))
+
+ klass = klass_name.constantize
+
+ model_eval(klass, &block)
+ end
+
+ private
+
+ def model_eval(klass, &block)
+ class << klass
+ def method_missing_with_columns(sym, *args, &block)
+ ActiveRecord::Base.connection.change_table(name.tableize) do |t|
+ t.send(sym, *args)
+ end
+ end
+
+ alias_method_chain :method_missing, :columns
+ end
+
+ klass.class_eval(&block)
+
+ class << klass
+ alias_method :method_missing, :method_missing_without_columns
+ end
+ end
+end
View
6 spec/acts_as_fu_spec.rb
@@ -7,6 +7,8 @@ def build_foos
build_model(:foos) do
string :name
integer :age
+
+ def self.awesome?; true end
end
end
@@ -29,6 +31,10 @@ def build_foos
}.should_not raise_error
end
+ it "allows access to class" do
+ Foo.should be_awesome
+ end
+
describe "the class" do
it "is a subclass of ActiveRecord::Base" do
Foo.superclass.should == ActiveRecord::Base

0 comments on commit 99ae1ec

Please sign in to comment.