jay_z 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gemtest ADDED
File without changes
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm use 1.9.2-p290@jay_z --create
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in jay_z.gemspec
4
+ gemspec
data/Rakefile ADDED
@@ -0,0 +1,15 @@
1
+ require 'bundler/gem_tasks'
2
+
3
+ require 'rake/testtask'
4
+ require 'minitest/spec'
5
+
6
+ Rake::TestTask.new do |t|
7
+ t.libs.push "lib"
8
+ t.test_files = FileList['spec/**/*_spec.rb']
9
+ t.verbose = true
10
+ end
11
+
12
+ desc "open console (require 'jay_z')"
13
+ task :c do
14
+ system "irb -I lib -r jay_z"
15
+ end
data/example/blog.rb ADDED
@@ -0,0 +1,66 @@
1
+ # encoding: utf-8
2
+ $LOAD_PATH << File.dirname(__FILE__) + "../../lib"
3
+ require 'jay_z'
4
+
5
+ module JayZ
6
+ class Post < Blueprint
7
+ default do
8
+ author { Author.make.save }
9
+ body { "I am a default post body" }
10
+ end
11
+
12
+ set(:video) do
13
+ author { Author.make(:name => 'Anders defined in video').save }
14
+ url { 'http://www.youtube.com/watch?v=g5950v0kTJg' }
15
+ end
16
+ end
17
+
18
+ class Author < Blueprint
19
+ default do
20
+ name { 'Anders Törnqvist' }
21
+ end
22
+ end
23
+ end
24
+
25
+ class Post
26
+ extend JayZ
27
+ attr_accessor :author
28
+ attr_accessor :body
29
+ attr_accessor :url
30
+ def save; 'save in post called'; end
31
+ def save!; self; end
32
+ end
33
+
34
+ class Author
35
+ extend JayZ
36
+ attr_accessor :name
37
+ def save; self; end
38
+ end
39
+
40
+ puts "\nPost.make \n"
41
+ p Post.make # => <JayZ::Post:0x007ffb9b838c10
42
+ # @object=#<Post:0x007ffb9b8389b8
43
+ # @author=#<Author:0x007ffb9b8386e8
44
+ # @name="Anders Törnqvist">,
45
+ # @body="I am a default post body">>
46
+
47
+ puts "\nPost.make(:video) \n"
48
+ p Post.make(:video) # => <JayZ::Post:0x007fd329837f80
49
+ # @object=#<Post:0x007fd329837dc8
50
+ # @author=#<Author:0x007fd329837ad0
51
+ # @name="Anders defined in video">,
52
+ # @body="I am a default post body",
53
+ # @url="http://www.youtube.com/watch?v=g5950v0kTJg">>
54
+
55
+
56
+ puts "\npost = Post.make.save! \n"
57
+ post = Post.make.save!
58
+ p post.author # => <Author:0x007fc70a838d70 @name="Anders Törnqvist">
59
+ p post.body # => "I am a default post body"
60
+ p post.url # => nil
61
+
62
+ puts "\npost = Post.make(:video).save! \n"
63
+ post = Post.make(:video).save!
64
+ p post.author # => <Author:0x007ff669037db8 @name="Anders defined in video">
65
+ p post.body # => "I am a default post body"
66
+ p post.url # => "http://www.youtube.com/watch?v=g5950v0kTJg"
data/jay_z.gemspec ADDED
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "jay_z/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "jay_z"
7
+ s.version = JayZ::VERSION
8
+ s.authors = ["Anders Törnqvist"]
9
+ s.email = ["anders.tornqvist@gmail.com"]
10
+ s.homepage = "https://github.com/unders/jay_z"
11
+ s.summary = %q{A model factory}
12
+ s.description = %q{A model factory. Say no to fixtures.}
13
+
14
+ s.rubyforge_project = "jay_z"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {spec}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+ end
@@ -0,0 +1,78 @@
1
+ require 'jay_z/serial_number'
2
+ require 'jay_z/ghost'
3
+
4
+ module JayZ
5
+ class Blueprint; end
6
+
7
+ class << Blueprint
8
+ def make(*args)
9
+ new(*args)
10
+ end
11
+
12
+ def default(options={}, &block)
13
+ if block_given?
14
+ ghosts[:default] = Ghost.new(serial_number).instance_eval(&block)
15
+ else
16
+ hash = options.merge({})
17
+ default = ghosts[:default]
18
+ (default.keys - options.keys).each do |key|
19
+ hash[key] = default.send(key)
20
+ end
21
+ hash
22
+ end
23
+ end
24
+
25
+ def set(method, &block)
26
+ ghosts[method] = Ghost.new(serial_number).instance_eval(&block)
27
+ self.class.send(:define_method, method) do |options={}|
28
+ hash = options.merge({})
29
+ blueprint = ghosts[method]
30
+ blueprint_keys = blueprint.keys
31
+ (blueprint_keys - options.keys).each do |key|
32
+ hash[key] = blueprint.send(key)
33
+ end
34
+
35
+ default = ghosts.fetch(:default, {})
36
+ (default.keys - blueprint_keys).each do |key|
37
+ hash[key] = default.send(key)
38
+ end
39
+
40
+ hash
41
+ end
42
+ end
43
+
44
+ private
45
+
46
+ def ghosts
47
+ @ghosts ||={}
48
+ end
49
+
50
+ def serial_number
51
+ @serial_number ||= SerialNumber.new
52
+ end
53
+ end
54
+
55
+ class Blueprint
56
+ def initialize(*args)
57
+ symbol = args.first.is_a?(Symbol) ? args.first : :default
58
+ options = args.last.is_a?(Hash) ? args.pop : {}
59
+ @object = Kernel.const_get(self.class.name.sub('JayZ::', '')).new
60
+
61
+ self.class.send(symbol, options).each do |key, value|
62
+ @object.send("#{key}=", value)
63
+ end
64
+ end
65
+
66
+ def new
67
+ @object
68
+ end
69
+
70
+ def method_missing(method, *args, &block)
71
+ @object.send(method, *args, &block)
72
+ end
73
+
74
+ def respond_to_missing?(method, *)
75
+ @object.respond_to?(method)
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,27 @@
1
+ module JayZ
2
+ class Ghost
3
+ def initialize(serial_number)
4
+ @serial_number = serial_number
5
+ end
6
+
7
+ def sn
8
+ @serial_number.next
9
+ end
10
+
11
+ def keys
12
+ @keys ||= []
13
+ end
14
+
15
+ def method_missing(method, *args, &block)
16
+ if block_given?
17
+ keys << method
18
+ m = Module.new do
19
+ define_method(method, &block)
20
+ end
21
+ extend m
22
+ else
23
+ super
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,9 @@
1
+ module JayZ
2
+ class SerialNumber
3
+ def next
4
+ @next ||=0
5
+ @next += 1
6
+ end
7
+ end
8
+ end
9
+
@@ -0,0 +1,3 @@
1
+ module JayZ
2
+ VERSION = "0.0.1"
3
+ end
data/lib/jay_z.rb ADDED
@@ -0,0 +1,8 @@
1
+ # encoding: utf-8
2
+ require "jay_z/version"
3
+ require 'jay_z/blueprint'
4
+ module JayZ
5
+ def make(*args)
6
+ JayZ.const_get(name).make(*args)
7
+ end
8
+ end
data/readme.rdoc ADDED
File without changes
@@ -0,0 +1,156 @@
1
+ # encoding: utf-8
2
+ require 'jay_z/blueprint'
3
+ require 'minitest/autorun'
4
+
5
+ module JayZ
6
+ class User < Blueprint; end
7
+ class Comment < Blueprint; end
8
+ end
9
+
10
+ class User
11
+ extend JayZ
12
+ attr_accessor :name
13
+ def self.counter; @counter; end
14
+ def self.counter=(c); @counter = c; end
15
+ def initialize; self.class.counter = 0; end
16
+ def save; self.class.counter += 1; end
17
+ end
18
+
19
+ class Comment
20
+ extend JayZ
21
+ attr_accessor :user
22
+ attr_accessor :body
23
+ end
24
+
25
+ describe JayZ::Blueprint do
26
+ describe ".make" do
27
+ it "creates an instance" do
28
+ JayZ::User.make.must_be_instance_of(JayZ::User)
29
+ end
30
+ end
31
+
32
+ describe ".default" do
33
+ before do
34
+ JayZ::User.default do
35
+ name { "User 1" }
36
+ end
37
+ end
38
+
39
+ describe "when called with a block" do
40
+ it "saves the block" do
41
+ JayZ::User.default.must_equal :name => 'User 1'
42
+ end
43
+ end
44
+
45
+ describe "when called without a block" do
46
+ it "returns a hash" do
47
+ JayZ::User.default.must_equal :name => 'User 1'
48
+ end
49
+ end
50
+ end
51
+
52
+ describe ".set" do
53
+ before do
54
+ JayZ::Comment.set(:admin) do
55
+ user { JayZ::User.make(:name => 'Anders Admin').new }
56
+ end
57
+ end
58
+
59
+ describe "when called with a block" do
60
+ it "creates a class method specified by the symbol name" do
61
+ JayZ::Comment.admin[:user].name.must_equal 'Anders Admin'
62
+ end
63
+
64
+ it "saves the block" do
65
+ JayZ::Comment.admin[:user].name.must_equal 'Anders Admin'
66
+ end
67
+ end
68
+
69
+ describe "when called without a block" do
70
+ it "returns a hash" do
71
+ JayZ::Comment.admin[:user].name.must_equal 'Anders Admin'
72
+ end
73
+ end
74
+ end
75
+
76
+ describe "when the default block values is used" do
77
+ before do
78
+ JayZ::Comment.default do
79
+ user { User.make(:name => 'Anders Default').new }
80
+ body { 'I am defined in default' }
81
+ end
82
+ JayZ::Comment.set(:spam) do
83
+ user { User.make(:name => 'Anders Spam').new }
84
+ end
85
+ end
86
+
87
+ describe "when .set(:spam) has not defined the method in its block" do
88
+ it "returns the body defined in the default block" do
89
+ JayZ::Comment.spam[:body].must_equal 'I am defined in default'
90
+ end
91
+ end
92
+ describe "when .set(:spam) has defined the method in its block" do
93
+ it "returns the value from the spam block" do
94
+ JayZ::Comment.spam[:user].name.must_equal 'Anders Spam'
95
+ end
96
+ end
97
+ end
98
+
99
+ describe "serial number interpolation with sn method" do
100
+ before do
101
+ JayZ::User.default do
102
+ name { "User #{sn}" }
103
+ end
104
+ JayZ::User.set(:admin) do
105
+ name { "Admin #{sn}" }
106
+ end
107
+ end
108
+
109
+ it "returns the next number for each hash" do
110
+ JayZ::User.admin[:name].must_equal "Admin 1"
111
+ JayZ::User.default[:name].must_equal "User 2"
112
+ JayZ::User.default[:name].must_equal "User 3"
113
+ JayZ::User.admin[:name].must_equal "Admin 4"
114
+ end
115
+ end
116
+
117
+ describe "#new" do
118
+ before do
119
+ JayZ::User.default do
120
+ name { 'Anders' }
121
+ end
122
+ end
123
+ it "returns the correct object" do
124
+ JayZ::User.make.new.must_be_instance_of(User)
125
+ end
126
+ end
127
+
128
+ describe "when a message is sent to the instance" do
129
+ before do
130
+ JayZ::User.default do
131
+ name { 'Anders' }
132
+ end
133
+ @jayz_user = JayZ::User.make
134
+ end
135
+ it "delegates that message" do
136
+ @jayz_user.save.must_equal 1
137
+ end
138
+ end
139
+
140
+ describe "blocks are not executed when defined" do
141
+ before do
142
+ JayZ::User.default do
143
+ name { 'Anders' }
144
+ end
145
+ JayZ::Comment.default do
146
+ user { User.make.save }
147
+ end
148
+ end
149
+
150
+ it "executes the block when called" do
151
+ User.counter.must_equal nil
152
+ JayZ::Comment.default
153
+ User.counter.must_equal 1
154
+ end
155
+ end
156
+ end
@@ -0,0 +1,43 @@
1
+ require 'minitest/autorun'
2
+ require 'jay_z/ghost'
3
+ describe JayZ::Ghost do
4
+ before do
5
+ def Object.next; 1; end
6
+ @ghost = JayZ::Ghost.new(Object)
7
+ end
8
+
9
+ describe "#keys" do
10
+ it "returns a collection of all methods sent to the object" do
11
+ @ghost.title { 'block' }
12
+ @ghost.body { 'block' }
13
+ @ghost.keys.must_equal [:title, :body]
14
+ end
15
+ end
16
+
17
+ describe "#sn" do
18
+ it "returns a serial number" do
19
+ @ghost.sn.must_equal 1
20
+ end
21
+ end
22
+
23
+ describe "a message with a block is sent to the object" do
24
+ it "creates a method that returns the content of the block" do
25
+ @ghost.title { 'I am a block' }
26
+ @ghost.title.must_equal 'I am a block'
27
+ end
28
+
29
+ it "doesn't execute the code inside the block until the method is called" do
30
+ def Object.counter; @counter ||= 0; @counter +=1; end
31
+ @ghost.title { Object.counter }
32
+ @ghost.title.must_equal 1
33
+ @ghost.title.must_equal 2
34
+ end
35
+ end
36
+
37
+ describe "a message is sent with a block containing a string with \#{sn}" do
38
+ it "interporlates the sn number" do
39
+ @ghost.town { "I am ghost town number-#{sn}" }
40
+ @ghost.town.must_equal "I am ghost town number-1"
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,22 @@
1
+ require 'minitest/autorun'
2
+ require 'jay_z/serial_number'
3
+ describe JayZ::SerialNumber do
4
+ before do
5
+ @serial_number = JayZ::SerialNumber.new
6
+ end
7
+
8
+ describe "#next" do
9
+ describe "when called the first time" do
10
+ it "returns 1" do
11
+ @serial_number.next.must_equal 1
12
+ end
13
+ end
14
+
15
+ describe "when called the second time" do
16
+ it "returns the consecutive number" do
17
+ number = @serial_number.next
18
+ @serial_number.next.must_equal (number + 1)
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,83 @@
1
+ # encoding: utf-8
2
+ require 'minitest/autorun'
3
+ require 'jay_z'
4
+
5
+ module JayZ
6
+ class Post < Blueprint
7
+ default do
8
+ author { Author.make.save }
9
+ body { "I am a default post body" }
10
+ end
11
+
12
+ set(:video) do
13
+ author { Author.make(:name => 'Anders defined in Post.video').save }
14
+ url { 'http://www.youtube.com/watch?v=g5950v0kTJg' }
15
+ end
16
+ end
17
+
18
+ class Author < Blueprint
19
+ default do
20
+ name { 'Anders Törnqvist' }
21
+ end
22
+ end
23
+ end
24
+
25
+ class Post
26
+ extend JayZ
27
+ attr_accessor :author
28
+ attr_accessor :body
29
+ attr_accessor :url
30
+ def save; 'save in post called'; end
31
+ def save!; self; end
32
+ end
33
+
34
+ class Author
35
+ extend JayZ
36
+ attr_accessor :name
37
+ def save; self; end
38
+ end
39
+
40
+ describe JayZ do
41
+ describe "when extending a class with the JayZ module" do
42
+ it "adds a #make method" do
43
+ Post.make.must_be_instance_of(JayZ::Post)
44
+ end
45
+
46
+ describe "#make" do
47
+ it "creates a blueprint proxy object" do
48
+ Post.make.must_be_instance_of(JayZ::Post)
49
+ end
50
+ end
51
+
52
+ describe "blueprint proxy object" do
53
+ it "delegates all messages" do
54
+ Post.make.save.must_equal 'save in post called'
55
+ end
56
+ end
57
+
58
+ describe "before it delegates a message" do
59
+ it "populates the receiver with the values from the blueprint" do
60
+ Post.make.save!.body.must_equal 'I am a default post body'
61
+ end
62
+ end
63
+
64
+ describe ".make message with a symbol argument" do
65
+ before { @video = Post.make(:video).save! }
66
+ it "populates the receiver with values defined in the video block" do
67
+ @video.url.must_equal 'http://www.youtube.com/watch?v=g5950v0kTJg'
68
+ end
69
+
70
+ it %q{populates the receiver with values from default block if not
71
+ defined in video block} do
72
+ @video.body.must_equal 'I am a default post body'
73
+ end
74
+
75
+ describe "inside a blueprint block" do
76
+ it "can create other objects" do
77
+ @video.author.name.must_equal 'Anders defined in Post.video'
78
+ end
79
+ end
80
+ end
81
+
82
+ end
83
+ end
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jay_z
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Anders Törnqvist
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-09-23 00:00:00.000000000 +02:00
13
+ default_executable:
14
+ dependencies: []
15
+ description: A model factory. Say no to fixtures.
16
+ email:
17
+ - anders.tornqvist@gmail.com
18
+ executables: []
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - .gemtest
23
+ - .gitignore
24
+ - .rvmrc
25
+ - Gemfile
26
+ - Rakefile
27
+ - example/blog.rb
28
+ - jay_z.gemspec
29
+ - lib/jay_z.rb
30
+ - lib/jay_z/blueprint.rb
31
+ - lib/jay_z/ghost.rb
32
+ - lib/jay_z/serial_number.rb
33
+ - lib/jay_z/version.rb
34
+ - readme.rdoc
35
+ - spec/jay_z/blueprint_spec.rb
36
+ - spec/jay_z/ghost_spec.rb
37
+ - spec/jay_z/serial_number_spec.rb
38
+ - spec/jay_z_spec.rb
39
+ has_rdoc: true
40
+ homepage: https://github.com/unders/jay_z
41
+ licenses: []
42
+ post_install_message:
43
+ rdoc_options: []
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ none: false
48
+ requirements:
49
+ - - ! '>='
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ! '>='
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ requirements: []
59
+ rubyforge_project: jay_z
60
+ rubygems_version: 1.6.2
61
+ signing_key:
62
+ specification_version: 3
63
+ summary: A model factory
64
+ test_files: []