Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
unders committed May 23, 2012
1 parent 3fede96 commit c6f69a1
Showing 1 changed file with 33 additions and 37 deletions.
70 changes: 33 additions & 37 deletions README.md
Expand Up @@ -91,29 +91,22 @@ Or install it yourself as:
```


## Defining Blueprints
## Blueprint Class
A blueprint for a model is defined inside the Letterpress module; the class name of the blueprint
__must__ be the same as the models class name. Each blueprint class can inherit from
[Blueprint(ProxyMethods)](https://github.com/unders/letterpress/blob/master/lib/letterpress.rb#L18), which ads
three methods to the proxy object.
__must__ be the same as the models class name.

[These methods](https://github.com/unders/letterpress/blob/master/lib/letterpress.rb#L18) add the following functionality:
* [`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.
* [`new!`](https://github.com/unders/letterpress/blob/master/spec/letterpress/rails_spec.rb#L108) - returns the object under test, even if not valid.
* [`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.

The `new!` method should only be used if you want an invalid object, e.g. when you test validations. But
usually you can work directly with the proxy object when testing validations, since it will [delegate](https://github.com/unders/letterpress/blob/master/lib/letterpress/blueprint.rb#L73)
its messages to the model instance.

Below is an example of a Letterpress User class. Letterpress has two methods for defining blueprints: `default` and `define`; `default` can be used once in each class but `define` can be used as many time as you like. In this example the `admin` attribute is overridden by the admin block. The admin block will inherit the other attributes defined inside the default block. The `sn` method is used for methods that needs unique values.
Below is an example of a Letterpress User class. Letterpress has two methods for
defining blueprints: `default` and `define`; `default` can be used once in each
class but `define` can be used as many time as you like. In this example
the `admin` attribute is overridden by the admin block. The admin block will inherit
the other attributes defined inside the default block.


```ruby
module Letterpress
class User < Blueprint(ProxyMethods)
default do
email { "user#{sn}@example.com" }
email { "user@example.com" }
admin { false }
end

Expand All @@ -123,24 +116,31 @@ module Letterpress
end
end

user1 = User.make.new
user2 = User.make.new
user3 = User.make.new

user.1.email # => "user1@example.com"
user.2.email # => "user2@example.com"
user.3.email # => "user3@example.com"

user_proxy = user.make # return a proxy object; can be used when checking validation errors.
user = User.make.new # returns a new model object
user = User.make.save # returns a persisted model object
user = User.make
user.admin # => false
user.email # => "user@example.com"

user = User.make(:admin)
user.admin # => true
user.email # => "user@example.com"

user = User.make(:admin, admin: false)
user.admin # => false
user.email # => "user@example.com"
```

#### Unique Attributes
For attributes that must be unique, you can call the `sn` method within the
attribute block to get a unique serial number for the object.

```ruby
email { "user-#{sn}@example.com" }

user1 = User.make
user2 = User.make

user1.email # => "user1@example.com"
user2.email # => "user2@example.com"
```

#### What you must know to create a blueprint class:
Expand All @@ -149,22 +149,18 @@ user.admin # => false
* The `sn` method is used inside an attribute block to create unique attribute values, e.g if
each user instance must have a unique email address.

## Creating objects with Letterpress
## Creating Object
Letterpress ads [`make`](https://github.com/unders/letterpress/blob/master/lib/letterpress.rb#L12)
to each ActiveRecord class; When `make` is called on an ActiveRecord class, it returns a proxy object
that will delegate its received messages to the ActiveRecord instance.
that will [delegate](https://github.com/unders/letterpress/blob/master/lib/letterpress/blueprint.rb#L73)
its received messages to the ActiveRecord instance.


### Overriding attribute values defined in the default block
1. Send an options hash as an argument to `make`, e.g. `User.make(email: "new@email.com")`.
2. Send a symbol as the first argument to make, e.g. `User.make(:admin)`, then the ActiveRecord
instance will use the values defined by the default and admin blocks, but values
defined by both blocks will use values from the admin block.

That means that values defined in the default block is always used, unless they are overridden with another
block or by sending an options hash to `make`.
[The proxy object](https://github.com/unders/letterpress/blob/master/lib/letterpress.rb#L18) implements these methods:
* [`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.
* [`new!`](https://github.com/unders/letterpress/blob/master/spec/letterpress/rails_spec.rb#L108) - returns the object under test, even if not valid.
* [`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.

Below is a code snippet that shows how to create a proxy object and how to create an model object. The proxy object will delegate its received messages to the model instance.

```ruby
User.make # Returns instance of Letterpress::User
Expand Down

0 comments on commit c6f69a1

Please sign in to comment.