machine
Machine is a factory tool designed for a fixture replacement in Rails applications. It borrows a few concepts from factory_girl but applies attributes to objects differently and has a different concept for associations.
Written by Aubrey Holland.
download
Github: Page
Gem:
gem install aub-machine —source http://gems.github.com
Note: if you install using the gem from Github, you’ll need this
in your environment.rb if you want to use Rails 2.1’s dependency manager:
config.gem “aub-machine”,
:lib => “machine”,
:source => “http://gems.github.com”
define a machine
Machine.define :car do |car, machine|
car.make = 'Ford'
car.model = 'Taurus'
end
This defines a factory for building Car objects. The block is yielded an instance of the Car class
along with a machine object that can be used for building associated objects. Optionally, a class
name can be provided in cases where the name of the machine is not the same as that of the class.
Machine.define :auto, :class => Car do |auto, machine|
...
end
Machines can also be extended to any number of levels. When an extended machine is executed, the
tree will be applied starting from the bottom. This is useful in cases where one machine is a
specialization of another one.
Machine.define :car do |car, machine|
car.make = 'Ford'
car.type = 'Car'
end
Machine.define :station_wagon, :class => Car, :extends => :car do |car, machine|
car.type = 'Station Wagon'
end
In addition, groups of machines can be defined that are built from a set of base attributes.
This is useful for namespacing similar types.
Machine.define_group :user do |group|
group.base do |user, machine|
user.password = 'password'
user.password_confirmation = 'password'
user.status = 'active'
user.permissions = 'none'
end
group.define :super_user do |user, machine|
user.permissions = 'super'
end
end
This defines two machines, user and super_user, where super_user has all of the attributes of
user but with a different permissions value.
use a machine
Machines can be applied using one of three methods:
Machine(:car) # creates an unsaved object using the machine 'car'.
Machine.build(:car) # same as above.
Machine.build!(:car) # creates an object using the 'car' machine and saves it.
With each of these methods, a hash of replacement attributes can be passed, and those
attributes will be used in place of the default ones defined in the machine.
Machine(:car, :make => 'Ferrari')
In addition, a block can be passed and it will be called with the newly created
object as an argument.
Machine(:car) do |car|
car.make = 'Ferrari'
end
associations
Associations are filled using the machine object that is yielded in the block of the machine
definition. Instances of other machines can be created by calling the object with the name of
the machine.
Machine.define :garage do |garage, machine|
garage.car = machine.car
end
Replacement attributes can be passed to the machine in order to specialize the instance
for for a given association.
Machine.define :garage do |garage, machine|
garage.cars = [machine.car(:model => 'Thunderbird'), machine.car(:model => 'Mustang')]
end
sequences
In cases where an attribute is required to be unique, sequences can be used. A sequence is
defined as a block that takes as its argument a unique integer and returns a result.
Machine.sequence :vin do |n|
"abc123-#{n}"
end
Sequences can then be used in machine definitions to set attribute values.
Machine.define :car do |car, machine|
car.vin = machine.next(:vin)
car.make = 'Honda'
end
license
(The MIT License)
Copyright © 2008 Aubrey Holland and patch
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
‘Software’), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.