jay_z 0.0.3 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -3,12 +3,13 @@ require 'bundler/gem_tasks'
3
3
  require 'rake/testtask'
4
4
  require 'minitest/spec'
5
5
 
6
- Rake::TestTask.new do |t|
6
+ Rake::TestTask.new(:spec) do |t|
7
7
  t.libs.push "lib"
8
8
  t.test_files = FileList['spec/**/*_spec.rb']
9
9
  t.verbose = true
10
10
  end
11
11
 
12
+ task :test => :spec
12
13
  task :default => :test
13
14
 
14
15
  desc "open console (require 'jay_z')"
data/jay_z.gemspec CHANGED
@@ -12,6 +12,9 @@ Gem::Specification.new do |s|
12
12
  s.description = %q{A model factory. Say no to fixtures.}
13
13
 
14
14
  s.rubyforge_project = "jay_z"
15
+ s.add_development_dependency "activerecord", "~> 3.1.0"
16
+ s.add_development_dependency "railties", "~> 3.1.0"
17
+ s.add_development_dependency "sqlite3", "~> 1.3.4"
15
18
 
16
19
  example_files = `git ls-files -- example`.split("\n")
17
20
  no_gem_files = example_files + %w[.gitignore .rvmrc]
@@ -0,0 +1,23 @@
1
+ module JayZ
2
+ module Generators
3
+ class InstallGenerator < Rails::Generators::Base
4
+ source_root File.expand_path('../templates', __FILE__)
5
+
6
+ class_option :test_framework, :type => :string, :aliases => "-t", :desc => "Test framework to use JayZ with"
7
+
8
+ def blueprints_file
9
+ if rspec?
10
+ copy_file "blueprint.rb", "spec/blueprint.rb"
11
+ else
12
+ copy_file "blueprint.rb", "test/blueprint.rb"
13
+ end
14
+ end
15
+
16
+ private
17
+
18
+ def rspec?
19
+ options[:test_framework].to_sym == :rspec
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,23 @@
1
+ # endcoding: utf-8
2
+ module JayZ
3
+ # Add your blueprints here.
4
+ #
5
+ # e.g.
6
+ # class User < Blueprint(ActiveRecord)
7
+ # default do
8
+ # name { "Anders" }
9
+ # admin { false }
10
+ # end
11
+ #
12
+ # set(:admin) do
13
+ # admin { true }
14
+ # end
15
+ # end
16
+ #
17
+ # class Post < Blueprint(ActiveRecord)
18
+ # default do
19
+ # title { "Post #{sn}" }
20
+ # body { "Lorem ipsum...#" }
21
+ # end
22
+ # end
23
+ end
@@ -0,0 +1,29 @@
1
+ module JayZ
2
+ module Generators
3
+ class ModelGenerator < Rails::Generators::NamedBase
4
+ argument :attributes, :type => :array, :default => [], :banner => "field:type field:type"
5
+
6
+ def create_blueprint
7
+ file_name = if File.exists?(File.join(Rails.root, 'test', 'blueprint.rb'))
8
+ "test/blueprint.rb"
9
+ elsif File.exists?(File.join(Rails.root, 'spec', 'blueprint.rb'))
10
+ "spec/blueprint.rb"
11
+ else
12
+ raise "Cannot find the blueprint file"
13
+ end
14
+ gsub_file(file_name, /.end\s*\Z/mx) { |match| "#{code}\n#{match}" }
15
+ end
16
+
17
+ private
18
+
19
+ def code
20
+ %Q{\n
21
+ class #{class_name} < Blueprint(ActiveRecord)
22
+ default do
23
+ #{attributes.map { |a| "#{a.name} { #{a.default.inspect} }" }.join("\n ")}
24
+ end
25
+ end}
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,5 @@
1
+ require 'jay_z'
2
+ require 'jay_z/railtie'
3
+
4
+ ActiveRecord::Base.extend(JayZ)
5
+ JayZ::ActiveRecord::Base = ActiveRecord::Base
@@ -0,0 +1,14 @@
1
+ module JayZ
2
+ class Railtie < Rails::Railtie
3
+ config.after_initialize do
4
+ blueprints = [ File.join(Rails.root, 'test', 'blueprint'),
5
+ File.join(Rails.root, 'spec', 'blueprint')]
6
+ blueprints.each do |path|
7
+ if File.exists?("#{path}.rb")
8
+ load("#{path}.rb")
9
+ break
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
data/lib/jay_z/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module JayZ
2
- VERSION = "0.0.3"
2
+ VERSION = "0.1.0"
3
3
  end
data/lib/jay_z.rb CHANGED
@@ -1,8 +1,33 @@
1
1
  # encoding: utf-8
2
2
  require "jay_z/version"
3
3
  require 'jay_z/blueprint'
4
+
4
5
  module JayZ
6
+ def self.Blueprint(jayz_module)
7
+ Class.new(Blueprint) do
8
+ include jayz_module
9
+ end
10
+ end
11
+
5
12
  def make(*args)
6
13
  JayZ.const_get(name).make(*args)
7
14
  end
15
+
16
+ module ActiveRecord
17
+ def new
18
+ if @object.valid?
19
+ @object
20
+ else
21
+ raise ActiveRecord::RecordInvalid.new(@object)
22
+ end
23
+ end
24
+
25
+ def new!
26
+ @object
27
+ end
28
+
29
+ def save
30
+ @object.tap { |record| record.save! }
31
+ end
32
+ end
8
33
  end
@@ -0,0 +1,141 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rails/railtie'
4
+ require 'active_record'
5
+ require 'jay_z/active_record'
6
+ require 'minitest/autorun'
7
+
8
+ ActiveRecord::Base.establish_connection(:adapter => "sqlite3",
9
+ :database => ":memory:")
10
+
11
+ ActiveRecord::Schema.define do
12
+ create_table :posts do |t|
13
+ t.string :title
14
+ t.text :body
15
+ t.integer :author_id
16
+ end
17
+ create_table :authors do |t|
18
+ t.string :name
19
+ end
20
+ end
21
+
22
+ class Post < ActiveRecord::Base
23
+ belongs_to :author
24
+ validates_presence_of :title
25
+ validates_presence_of :author_id
26
+ validates_uniqueness_of :title
27
+ end
28
+
29
+ class Author < ActiveRecord::Base
30
+ validates_presence_of :name
31
+ end
32
+
33
+ # blueprint.rb
34
+ module JayZ
35
+ class Author < Blueprint(ActiveRecord)
36
+ default do
37
+ name { "Anders" }
38
+ end
39
+ end
40
+
41
+ class Post < Blueprint(ActiveRecord)
42
+ default do
43
+ title { "Post #{sn}" }
44
+ body { "Lorem ipsum..." }
45
+ author { Author.make.save }
46
+ end
47
+ end
48
+ end
49
+
50
+ describe ActiveRecord do
51
+ describe ".make" do
52
+ it "returns a proxy object" do
53
+ Post.make.must_be_instance_of(JayZ::Post)
54
+ end
55
+ end
56
+
57
+ describe "#new" do
58
+ describe "a valid object" do
59
+ before { @post = Post.make.new }
60
+ it "returns a populated record" do
61
+ @post.must_be_instance_of(Post)
62
+ @post.body.must_equal "Lorem ipsum..."
63
+ end
64
+
65
+ it "does not persist the record" do
66
+ Post.count.must_equal 0
67
+ end
68
+ end
69
+
70
+ describe "an invalid object" do
71
+ it "raises an exception" do
72
+ lambda {
73
+ Post.make(:author => nil).new
74
+ }.must_raise(ActiveRecord::RecordInvalid)
75
+ end
76
+ end
77
+ end
78
+
79
+ describe "#new!" do
80
+ describe "a valid object" do
81
+ before { @post2 = Post.make.new! }
82
+ it "returns a populated record" do
83
+ @post2.must_be_instance_of(Post)
84
+ @post2.body.must_equal "Lorem ipsum..."
85
+ end
86
+
87
+ it "does not persist the record" do
88
+ Post.count.must_equal 0
89
+ end
90
+ end
91
+
92
+ describe "an invalid object" do
93
+ it "returns a populated object" do
94
+ Post.make(:author => nil).new!.body.must_equal "Lorem ipsum..."
95
+ end
96
+ end
97
+ end
98
+
99
+ describe "#save" do
100
+ before { @post3 = Post.make.save }
101
+
102
+ describe "a valid object" do
103
+ it "persists and returns a populated record" do
104
+ @post3.must_be_instance_of(Post)
105
+ @post3.body.must_equal "Lorem ipsum..."
106
+ Post.count.must_equal 1
107
+ end
108
+ end
109
+
110
+ describe "an invalid object" do
111
+ it "raises an exception" do
112
+ lambda {
113
+ Post.make(:author => nil).save
114
+ }.must_raise(ActiveRecord::RecordInvalid)
115
+ end
116
+ end
117
+ end
118
+
119
+ describe 'makes fields uniq with #{sn}' do
120
+ before do
121
+ user = Author.make.save
122
+ @post1 = Post.make(:author => user).save
123
+ @post2 = Post.make(:author => user).save
124
+ @post3 = Post.make(:author => user).save
125
+ @post4 = Post.make(:author => user).save
126
+ end
127
+
128
+ it "creates object that has uniq fields" do
129
+ @post1.valid?.must_equal true
130
+ @post2.valid?.must_equal true
131
+ @post3.valid?.must_equal true
132
+ @post4.valid?.must_equal true
133
+ Post.count.must_equal 4
134
+ end
135
+
136
+ after do
137
+ Post.delete_all
138
+ end
139
+
140
+ end
141
+ end
data/spec/jay_z_spec.rb CHANGED
@@ -3,35 +3,35 @@ require 'minitest/autorun'
3
3
  require 'jay_z'
4
4
 
5
5
  module JayZ
6
- class Post < Blueprint
6
+ class Blog < Blueprint
7
7
  default do
8
- author { Author.make.save }
8
+ writer { Writer.make.save }
9
9
  body { "I am a default post body" }
10
10
  end
11
11
 
12
12
  set(:video) do
13
- author { Author.make(:name => 'Anders defined in Post.video').save }
13
+ writer { Writer.make(:name => 'Anders defined in Blog.video').save }
14
14
  url { 'http://www.youtube.com/watch?v=g5950v0kTJg' }
15
15
  end
16
16
  end
17
17
 
18
- class Author < Blueprint
18
+ class Writer < Blueprint
19
19
  default do
20
20
  name { 'Anders Törnqvist' }
21
21
  end
22
22
  end
23
23
  end
24
24
 
25
- class Post
25
+ class Blog
26
26
  extend JayZ
27
- attr_accessor :author
27
+ attr_accessor :writer
28
28
  attr_accessor :body
29
29
  attr_accessor :url
30
30
  def save; 'save in post called'; end
31
31
  def save!; self; end
32
32
  end
33
33
 
34
- class Author
34
+ class Writer
35
35
  extend JayZ
36
36
  attr_accessor :name
37
37
  def save; self; end
@@ -40,29 +40,29 @@ end
40
40
  describe JayZ do
41
41
  describe "when extending a class with the JayZ module" do
42
42
  it "adds a .make method" do
43
- Post.make.must_be_instance_of(JayZ::Post)
43
+ Blog.make.must_be_instance_of(JayZ::Blog)
44
44
  end
45
45
 
46
46
  describe ".make" do
47
47
  it "creates a blueprint proxy object" do
48
- Post.make.must_be_instance_of(JayZ::Post)
48
+ Blog.make.must_be_instance_of(JayZ::Blog)
49
49
  end
50
50
  end
51
51
 
52
52
  describe "blueprint proxy object" do
53
53
  it "delegate all messages" do
54
- Post.make.save.must_equal 'save in post called'
54
+ Blog.make.save.must_equal 'save in post called'
55
55
  end
56
56
  end
57
57
 
58
58
  describe "before it delegates a message" do
59
59
  it "populates the receiver with values from the blueprint" do
60
- Post.make.save!.body.must_equal 'I am a default post body'
60
+ Blog.make.save!.body.must_equal 'I am a default post body'
61
61
  end
62
62
  end
63
63
 
64
64
  describe ".make message with a symbol argument" do
65
- before { @video = Post.make(:video).save! }
65
+ before { @video = Blog.make(:video).save! }
66
66
  it "populates the receiver with values defined in the video block" do
67
67
  @video.url.must_equal 'http://www.youtube.com/watch?v=g5950v0kTJg'
68
68
  end
@@ -74,7 +74,7 @@ describe JayZ do
74
74
 
75
75
  describe "inside a blueprint block" do
76
76
  it "can create other objects" do
77
- @video.author.name.must_equal 'Anders defined in Post.video'
77
+ @video.writer.name.must_equal 'Anders defined in Blog.video'
78
78
  end
79
79
  end
80
80
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jay_z
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,9 +9,41 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-09-24 00:00:00.000000000 +02:00
13
- default_executable:
14
- dependencies: []
12
+ date: 2011-10-07 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activerecord
16
+ requirement: &70165095778160 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 3.1.0
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *70165095778160
25
+ - !ruby/object:Gem::Dependency
26
+ name: railties
27
+ requirement: &70165095777520 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: 3.1.0
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70165095777520
36
+ - !ruby/object:Gem::Dependency
37
+ name: sqlite3
38
+ requirement: &70165095776660 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: 1.3.4
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *70165095776660
15
47
  description: A model factory. Say no to fixtures.
16
48
  email:
17
49
  - anders.tornqvist@gmail.com
@@ -23,17 +55,22 @@ files:
23
55
  - Gemfile
24
56
  - Rakefile
25
57
  - jay_z.gemspec
58
+ - lib/generators/jay_z/install/install_generator.rb
59
+ - lib/generators/jay_z/install/templates/blueprint.rb
60
+ - lib/generators/jay_z/model/model_generator.rb
26
61
  - lib/jay_z.rb
62
+ - lib/jay_z/active_record.rb
27
63
  - lib/jay_z/blueprint.rb
28
64
  - lib/jay_z/ghost.rb
65
+ - lib/jay_z/railtie.rb
29
66
  - lib/jay_z/serial_number.rb
30
67
  - lib/jay_z/version.rb
31
68
  - readme.rdoc
69
+ - spec/jay_z/active_record_spec.rb
32
70
  - spec/jay_z/blueprint_spec.rb
33
71
  - spec/jay_z/ghost_spec.rb
34
72
  - spec/jay_z/serial_number_spec.rb
35
73
  - spec/jay_z_spec.rb
36
- has_rdoc: true
37
74
  homepage: https://github.com/unders/jay_z
38
75
  licenses: []
39
76
  post_install_message:
@@ -54,11 +91,12 @@ required_rubygems_version: !ruby/object:Gem::Requirement
54
91
  version: '0'
55
92
  requirements: []
56
93
  rubyforge_project: jay_z
57
- rubygems_version: 1.6.2
94
+ rubygems_version: 1.8.10
58
95
  signing_key:
59
96
  specification_version: 3
60
97
  summary: A model factory
61
98
  test_files:
99
+ - spec/jay_z/active_record_spec.rb
62
100
  - spec/jay_z/blueprint_spec.rb
63
101
  - spec/jay_z/ghost_spec.rb
64
102
  - spec/jay_z/serial_number_spec.rb