Skip to content
Browse files

A class can be namespaced by modules and classes.

* renamed the ActiveRecord module to ProxyMethods 
  to make it more general.
  • Loading branch information...
1 parent de73475 commit ae2865240adb5a3b0305a369dbb3603c2d5a6686 @unders committed
View
4 README.md
@@ -62,7 +62,7 @@ Examples
6. It adds to the end of file (test|spec)/blueprint.rb
- class Comment < Blueprint(ActiveRecord)
+ class Comment < Blueprint(ProxyMethods)
default do
post_id { 1 }
body { "MyText" }
@@ -71,7 +71,7 @@ Examples
7. Modify the generated blueprint according to your preferences
- class Comment < Blueprint(ActiveRecord)
+ class Comment < Blueprint(ProxyMethods)
default do
post { Post.make.save }
body { "MyText" }
View
4 lib/generators/jay_z/install/templates/blueprint.rb
@@ -3,7 +3,7 @@ module JayZ
# Add your blueprints here.
#
# e.g.
- # class User < Blueprint(ActiveRecord)
+ # class User < Blueprint(ProxyMethods)
# default do
# name { "Anders" }
# admin { false }
@@ -14,7 +14,7 @@ module JayZ
# end
# end
#
- # class Post < Blueprint(ActiveRecord)
+ # class Post < Blueprint(ProxyMethods)
# default do
# title { "Post #{sn}" }
# body { "Lorem ipsum...#" }
View
2 lib/generators/jay_z/model/model_generator.rb
@@ -18,7 +18,7 @@ def create_blueprint
def code
%Q{\n
- class #{class_name} < Blueprint(ActiveRecord)
+ class #{class_name} < Blueprint(ProxyMethods)
default do
#{attributes.map { |a| "#{a.name} { #{a.default.inspect} }" }.join("\n ")}
end
View
8 lib/jay_z.rb
@@ -10,15 +10,17 @@ def self.Blueprint(jayz_module)
end
def make(*args)
- JayZ.const_get(name).make(*args)
+ name.split('::').inject(JayZ) do |klass_level, name|
+ klass_level.const_get(name)
+ end.make(*args)
end
- module ActiveRecord
+ module ProxyMethods
def new
if @object.valid?
@object
else
- fail ::ActiveRecord::RecordInvalid.new(@object)
+ fail ActiveRecord::RecordInvalid.new(@object)
end
end
View
5 lib/jay_z/blueprint.rb
@@ -56,7 +56,10 @@ class Blueprint
def initialize(*args)
symbol = args.first.is_a?(Symbol) ? args.first : :default
options = args.last.is_a?(Hash) ? args.pop : {}
- @object = Kernel.const_get(self.class.name.sub('JayZ::', '')).new
+ klass_names = self.class.name.sub('JayZ::', '').split('::')
+ @object = klass_names.inject(Kernel) do |klass_level, name|
+ klass_level.const_get(name)
+ end.new
self.class.send(symbol, options).each do |key, value|
@object.send("#{key}=", value)
View
2 lib/jay_z/rails.rb
@@ -2,5 +2,3 @@
require 'jay_z/railtie'
ActiveRecord::Base.extend(JayZ)
-JayZ::ActiveRecord::Base = ActiveRecord::Base
-JayZ::ActiveRecord::IdentityMap = ActiveRecord::IdentityMap
View
24 spec/jay_z/blueprint_spec.rb
@@ -3,6 +3,9 @@
require 'minitest/autorun'
module JayZ
+ module Admin
+ class User < Blueprint; end
+ end
class User < Blueprint; end
class Comment < Blueprint; end
end
@@ -22,6 +25,13 @@ class Comment
attr_accessor :body
end
+module Admin
+ class User
+ extend JayZ
+ attr_accessor :name
+ end
+end
+
describe JayZ::Blueprint do
describe ".make" do
it "creates an instance" do
@@ -155,4 +165,18 @@ class Comment
User.counter.must_equal 1
end
end
+
+ describe "can handle classes within a module namespace" do
+ before do
+ JayZ::Admin::User.default do
+ name { "Anders is admin" }
+ end
+ end
+
+ it "returns the correct object populated with the default values" do
+ user = Admin::User.make.new
+ user.must_be_instance_of(Admin::User)
+ user.name.must_equal "Anders is admin"
+ end
+ end
end
View
72 spec/jay_z/rails_spec.rb
@@ -17,6 +17,12 @@
create_table :authors do |t|
t.string :name
end
+ create_table :author_users do |t|
+ t.string :name
+ end
+ create_table :bars do |t|
+ t.string :name
+ end
end
class Post < ActiveRecord::Base
@@ -28,26 +34,49 @@ class Post < ActiveRecord::Base
class Author < ActiveRecord::Base
validates_presence_of :name
+ class User < ActiveRecord::Base
+ validates_presence_of :name
+ end
+end
+
+module Foo
+ class Bar < ActiveRecord::Base
+ validates_presence_of :name
+ end
end
# blueprint.rb
module JayZ
- class Author < Blueprint(ActiveRecord)
+ class Author < Blueprint(ProxyMethods)
default do
name { "Anders" }
end
+
+ class User < JayZ::Blueprint(ProxyMethods)
+ default do
+ name { "Anders is the user" }
+ end
+ end
end
- class Post < Blueprint(ActiveRecord)
+ class Post < Blueprint(ProxyMethods)
default do
title { "Post #{sn}" }
body { "Lorem ipsum..." }
author { Author.make.save }
end
end
+
+ module Foo
+ class Bar < JayZ::Blueprint(ProxyMethods)
+ default do
+ name { 'I am Foo::Bar' }
+ end
+ end
+ end
end
-describe ActiveRecord do
+describe "ProxyMethods" do
describe ".make" do
it "returns a proxy object" do
Post.make.must_be_instance_of(JayZ::Post)
@@ -137,6 +166,43 @@ class Post < Blueprint(ActiveRecord)
after do
Post.delete_all
end
+ end
+
+ describe "can handle classes namespaced by other classes" do
+ before do
+ @user = Author::User.make.save
+ end
+
+ it "returns a populated record" do
+ @user.must_be_instance_of(Author::User)
+ @user.name.must_equal "Anders is the user"
+ end
+
+ it "creates a author user record" do
+ Author::User.count.must_equal 1
+ end
+
+ after do
+ Author::User.delete_all
+ end
+ end
+
+ describe "handles classes namespace by an module" do
+ before do
+ @bar = Foo::Bar.make.save
+ end
+
+ it "returns a populated record" do
+ @bar.must_be_instance_of(Foo::Bar)
+ @bar.name.must_equal "I am Foo::Bar"
+ end
+ it "creates a bar record" do
+ Foo::Bar.count.must_equal 1
+ end
+
+ after do
+ Foo::Bar.delete_all
+ end
end
end

0 comments on commit ae28652

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