letterpress 0.0.1 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,6 +1,7 @@
1
- # Letterpress
1
+ # Letterpress [![Build Status](http://travis-ci.org/unders/letterpress.png)](http://travis-ci.org/unders/letterpress)
2
+
3
+ Letterpress is a _lightweight_ model generator that replaces fixture files.
2
4
 
3
- TODO: Write a gem description
4
5
 
5
6
  ## Installation
6
7
 
@@ -18,7 +19,271 @@ Or install it yourself as:
18
19
 
19
20
  ## Usage
20
21
 
21
- TODO: Write usage instructions here
22
+ ### Rails, ActiveRecord and minitest
23
+
24
+
25
+ 1. Update Gemfile
26
+
27
+ ``` ruby
28
+ group :development, :test do
29
+ gem 'letterpress', :require => 'letterpress/rails'
30
+ end
31
+ ```
32
+
33
+ 2. Install the gem
34
+
35
+ $ bundle install
36
+
37
+ 3. Generate (test|spec)/blueprint.rb file
38
+
39
+
40
+ $ rails generate letterpress:install
41
+
42
+
43
+ 4. Update config/application.rb
44
+
45
+ ``` ruby
46
+ config.generators do |g|
47
+ g.test_framework :mini_test, :spec => true, :fixture_replacement => :letterpress
48
+ end
49
+ ```
50
+
51
+ 5. Generate a model object with its factory
52
+
53
+ $ rails generate model Comment post_id:integer body:text
54
+
55
+ 6. It adds to the end of file (test|spec)/blueprint.rb
56
+
57
+ ``` ruby
58
+ class Comment < Blueprint(ProxyMethods)
59
+ default do
60
+ post_id { 1 }
61
+ body { "MyText" }
62
+ end
63
+ end
64
+ ```
65
+
66
+ 7. Modify the generated blueprint according to your preferences
67
+
68
+ ``` ruby
69
+ class Comment < Blueprint(ProxyMethods)
70
+ default do
71
+ post { Post.make.new }
72
+ body { "MyText" }
73
+ end
74
+ end
75
+ ```
76
+
77
+ 8. Write tests in test/comment_test.rb
78
+
79
+ ``` ruby
80
+ require "minitest_helper"
81
+
82
+ class CommentTest < MiniTest::Rails::Model
83
+ before do
84
+ @comment = Comment.make.new
85
+ end
86
+
87
+ it "must be valid" do
88
+ @comment.valid?.must_equal true
89
+ end
90
+ end
91
+ ```
92
+
93
+
94
+ ## Blueprint Class
95
+ A blueprint for a model is defined inside the Letterpress module; the class name of the blueprint
96
+ __must__ be the same as the models class name.
97
+
98
+ Below is an example of a Letterpress User class. Letterpress has two methods for
99
+ defining blueprints: `default` and `define`; `default` can be used once in each
100
+ class but `define` can be used as many time as you like. In this example
101
+ the `admin` attribute is overridden by the admin block. The admin block will inherit
102
+ the other attributes defined inside the default block.
103
+
104
+
105
+ ```ruby
106
+ module Letterpress
107
+ class User < Blueprint(ProxyMethods)
108
+ default do
109
+ email { "user@example.com" }
110
+ admin { false }
111
+ end
112
+
113
+ define(:admin) do
114
+ admin { true }
115
+ end
116
+ end
117
+ end
118
+
119
+ user = User.make # => returns a proxy object (an instance of Letterpress::User)
120
+ user.admin # => false; itdelegates to an instance of the User class
121
+ user.email # => "user@example.com"
122
+
123
+ user = User.make(:admin)
124
+ user.admin # => true
125
+ user.email # => "user@example.com"
126
+
127
+ user = User.make(:admin, admin: false)
128
+ user.admin # => false
129
+ user.email # => "user@example.com"
130
+ ```
131
+
132
+ #### Unique Attributes
133
+ For attributes that must be unique, you can call the `sn` method within the
134
+ attribute block to get a unique serial number for the object.
135
+
136
+ ```ruby
137
+ email { "user-#{sn}@example.com" } # Each email gets a unique serial number.
138
+
139
+ user1 = User.make
140
+ user2 = User.make
141
+
142
+ user1.email # => "user1@example.com"
143
+ user2.email # => "user2@example.com"
144
+ ```
145
+
146
+ #### Associations
147
+ When you create associations you don't have to persist them inside the blueprint.
148
+
149
+ * has_many
150
+
151
+ ```ruby
152
+ # When defining a has_many association, you define it with an array.
153
+ # These 2 comments will only be persisted in the database when Post.make.save
154
+ # is called, and not when Post.make or Post.make.new is called.
155
+ comments { [ Comment.make.new, Comment.make.new ] }
156
+ ```
157
+
158
+ * belongs_to
159
+
160
+ ```ruby
161
+ # Post.make.new defines a non persisted post instance.
162
+ # The post instance will only be saved to the database if
163
+ # Comment.make.save is called.
164
+ post { Post.make.new }
165
+ ```
166
+
167
+ #### What you must know to create a blueprint class:
168
+ * The class methods `default` and `define` is used for defining default attribute values.
169
+ * An attribute values __must__ be defined inside a block: `{}`.
170
+ * The `sn` method is used inside an attribute block to create unique attribute values, e.g if
171
+ each user instance must have a unique email address.
172
+
173
+ ## Creating Object
174
+ Letterpress ads [`make`](https://github.com/unders/letterpress/blob/master/lib/letterpress.rb#L12)
175
+ to each ActiveRecord class; When `make` is called on an ActiveRecord class, it returns a proxy object
176
+ that will [delegate](https://github.com/unders/letterpress/blob/master/lib/letterpress/blueprint.rb#L73)
177
+ its received messages to the ActiveRecord instance.
178
+
179
+ The [proxy object](https://github.com/unders/letterpress/blob/master/lib/letterpress.rb#L18) implements these methods:
180
+ * [`new`](https://github.com/unders/letterpress/blob/master/spec/letterpress/rails_spec.rb#L86) - returns the object under test or raises an exception if the object isn't valid.
181
+ * [`new!`](https://github.com/unders/letterpress/blob/master/spec/letterpress/rails_spec.rb#L108) - returns the object under test, even if not valid.
182
+ * [`save`](https://github.com/unders/letterpress/blob/master/spec/letterpress/rails_spec.rb#L128) - returns the persisted object under test or raises an exception if the object isn't valid.
183
+
184
+
185
+ ```ruby
186
+ User.make # Returns instance of Letterpress::User
187
+ User.make(:admin) # Returns instance of Letterpress::User
188
+ User.make(:admin, email: "foo@bar.com") # Returns instance of Letterpress::User
189
+
190
+
191
+ User.make.new # Returns a new User instance
192
+ User.make(:admin).save # Returns a persisted User instance
193
+ User.make(:admin, email: "foo@bar.com").new! # Returns a new User instance
194
+ ```
195
+
196
+ ## Creating global ProxyMethods
197
+ Since each Letterpress class inherits from
198
+ [ProxyMethods](https://github.com/unders/letterpress/blob/master/lib/letterpress.rb#L18),
199
+ we can define methods in that module and they will be added to each proxy object.
200
+
201
+ In the code snippet below I have added a `save_and_relod` method that will be
202
+ available on every proxy object.
203
+
204
+ ```ruby
205
+ module Letterpress
206
+ module ProxyMethods
207
+ def save_and_reload
208
+ record = save
209
+ @object = @object.class.find(record.id)
210
+ end
211
+ end
212
+ end
213
+
214
+ user = User.make.save_and_reload
215
+ ```
216
+
217
+ ## Creating ProxyMethods on a class
218
+ If you want to add a method to only one proxy object, see code below.
219
+
220
+ ```ruby
221
+ module Letterpress
222
+ class User < Blueprint(ProxyMethods)
223
+ default do
224
+ #...
225
+ end
226
+
227
+ def password(password)
228
+ @object.password = "#{password} - in proxy"
229
+ @object
230
+ end
231
+ end
232
+ end
233
+
234
+ user = User.make.password("secret").save
235
+ user.password # => "secret - in proxy"
236
+ ```
237
+
238
+ ## Overriding .make
239
+ If you for some reason want to do something with the proxy object before returning it, you can
240
+ do that, see code below.
241
+
242
+ ```ruby
243
+ module Letterpress
244
+ class User < Blueprint(ProxyMethods)
245
+ default do
246
+ #...
247
+ end
248
+
249
+ def self.make
250
+ user = super
251
+ # do something with the proxy object before returning it
252
+ user.password = "I always have this value"
253
+ user
254
+ end
255
+ end
256
+ end
257
+
258
+ user = User.make.new
259
+ user.password # => "I always have this value"
260
+ ```
261
+
262
+ Compatibility
263
+ -------------
264
+
265
+ Ruby version 1.9.2 and 1.9.3 and Rails version 3.1
266
+
267
+ [GemTesters](http://test.rubygems.org/gems/letterpress) has
268
+ more information on which platforms Letterpress is tested.
269
+
270
+
271
+ Test
272
+ -------------------------
273
+
274
+ gem install rubygems-test
275
+ gem test letterpress
276
+
277
+
278
+ For more info see: [GemTesters](http://test.rubygems.org/)
279
+
280
+ Where
281
+ -----
282
+ * [Letterpress @ Github](http://github.com/unders/letterpress)
283
+ * [Letterpress @ GemTesters](http://test.rubygems.org/gems/letterpress)
284
+ * [Letterpress @ Rubygems](http://rubygems.org/gems/letterpress)
285
+ * [Letterpress @ Rubydoc](http://rubydoc.info/gems/letterpress)
286
+ * [Letterpress @ Travis](http://travis-ci.org/#!/unders/letterpress)
22
287
 
23
288
  ## Contributing
24
289
 
@@ -33,7 +33,7 @@ module Letterpress
33
33
  end
34
34
 
35
35
  default = ghosts.fetch(:default, {})
36
- (default.keys - blueprint_keys).each do |key|
36
+ (default.keys - blueprint_keys - options.keys).each do |key|
37
37
  hash[key] = default.send(key)
38
38
  end
39
39
 
@@ -1,3 +1,3 @@
1
1
  module Letterpress
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.3"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: letterpress
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-04-13 00:00:00.000000000 Z
12
+ date: 2012-05-26 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord