Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

added ability to access klass definition in block

  • Loading branch information...
commit 99ae1ec40db8ef91cc63d7972c8e7fcd501685aa 1 parent 78b70c8
Pat Nakajima authored
15  README.textile
Source Rendered
@@ -24,6 +24,10 @@ Then in your specs:
24 24
         text :omg_omg_bio
25 25
         string :name
26 26
         string :favorite_scene
  27
+        
  28
+        def awesome?(show_name)
  29
+          show_name == "Gilmore Girls"
  30
+        end
27 31
       end
28 32
     end
29 33
     
@@ -33,15 +37,20 @@ Then in your specs:
33 37
       nerd.errors.on(:favorite_scene).should_not be_nil
34 38
     end
35 39
     
  40
+    it "should think gilmore girls is awesome"
  41
+      nerd = Nerd.new
  42
+      nerd.awesome?("Gilmore Girls").should be_true
  43
+    end
  44
+    
36 45
     it "has other stuff" do
37 46
       # ETC!
38 47
     end
39 48
   end
40 49
   
41 50
 The @build_model@ method allows you to build an ActiveRecord on
42  
-the fly. It takes a block where you can specify columns. The table
43  
-will be created in in an in-memory sqlite3 database. If you need
44  
-another adapter, this project ain't for you. Sorry.
  51
+the fly. It takes a block where you can specify columns and methods.
  52
+The table will be created in in an in-memory sqlite3 database. If
  53
+you need another adapter, this project ain't for you. Sorry.
45 54
 
46 55
 TODO
47 56
 
20  lib/acts_as_fu.rb
... ...
@@ -1,21 +1,7 @@
1 1
 $LOAD_PATH << File.dirname(__FILE__) + '/acts_as_fu'
2 2
 
3  
-%w(rubygems activerecord constants).each { |lib| require lib }
4  
-
5 3
 module ActsAsFu
6  
-  def build_model(name, &block)
7  
-    ActiveRecord::Base.establish_connection({
8  
-      :adapter => "sqlite3",
9  
-      :database => ":memory:"
10  
-    })
11  
-
12  
-    ActiveRecord::Base.connection.create_table name, :force => true do |table|
13  
-      table.instance_eval(&block)
14  
-    end
15  
-
16  
-    klass_name = name.to_s.classify
17  
-
18  
-    Object.send(:remove_const, klass_name) rescue nil
19  
-    Object.const_set(klass_name, Class.new(ActiveRecord::Base))
20  
-  end
  4
+  VERSION = '0.0.2'
21 5
 end
  6
+
  7
+%w(rubygems activerecord constants helper).each { |lib| require lib }
39  lib/acts_as_fu/helper.rb
... ...
@@ -0,0 +1,39 @@
  1
+module ActsAsFu
  2
+  def build_model(name, &block)
  3
+    ActiveRecord::Base.establish_connection({
  4
+      :adapter => "sqlite3",
  5
+      :database => ":memory:"
  6
+    })
  7
+
  8
+    ActiveRecord::Base.connection.create_table(name, :force => true) { }
  9
+    
  10
+    klass_name = name.to_s.classify
  11
+
  12
+    Object.send(:remove_const, klass_name) rescue nil
  13
+    Object.const_set(klass_name, Class.new(ActiveRecord::Base))
  14
+    
  15
+    klass = klass_name.constantize
  16
+
  17
+    model_eval(klass, &block)
  18
+  end
  19
+  
  20
+  private
  21
+  
  22
+  def model_eval(klass, &block)
  23
+    class << klass
  24
+      def method_missing_with_columns(sym, *args, &block)
  25
+        ActiveRecord::Base.connection.change_table(name.tableize) do |t|
  26
+          t.send(sym, *args)
  27
+        end
  28
+      end
  29
+      
  30
+      alias_method_chain :method_missing, :columns
  31
+    end
  32
+    
  33
+    klass.class_eval(&block)
  34
+    
  35
+    class << klass
  36
+      alias_method :method_missing, :method_missing_without_columns
  37
+    end
  38
+  end
  39
+end
6  spec/acts_as_fu_spec.rb
@@ -7,6 +7,8 @@ def build_foos
7 7
     build_model(:foos) do
8 8
       string :name
9 9
       integer :age
  10
+      
  11
+      def self.awesome?; true end
10 12
     end
11 13
   end
12 14
   
@@ -29,6 +31,10 @@ def build_foos
29 31
       }.should_not raise_error
30 32
     end
31 33
     
  34
+    it "allows access to class" do
  35
+      Foo.should be_awesome
  36
+    end
  37
+    
32 38
     describe "the class" do
33 39
       it "is a subclass of ActiveRecord::Base" do
34 40
         Foo.superclass.should == ActiveRecord::Base

0 notes on commit 99ae1ec

Please sign in to comment.
Something went wrong with that request. Please try again.