Giter Site home page Giter Site logo

spawn's Introduction

Spawn

A ridiculously simple fixtures replacement for your web framework of choice.

Description

Spawn is a very small library that can effectively replace fixtures or any other huge library for the same task.

Usage

In the examples below we are using Faker to generate random data, but you can use any method.

Note that the object the spawner block yields is not an instance of User, but an OpenStruct that records the attributes passed and will in turn relay that information when the User is instantiated.

With ActiveRecord:

class User < ActiveRecord::Base
  spawner do |user|
    user.name = Faker::Name.name
    user.email = Faker::Internet.email
  end
end

With Sequel:

class User < Sequel::Model
  extend Spawn

  spawner do |user|
    user.name = Faker::Name.name
    user.email = Faker::Internet.email
  end
end

With Ohm:

class User < Ohm::Model
  extend Spawn

  attribute :name
  attribute :email

  spawner do |user|
    user.name = Faker::Name.name
    user.email = Faker::Internet.email
  end
end

If you don't want to pollute your class definition, you can of course use it from outside:

User.spawner do |user|
  user.name = Faker::Name.name
  user.email = Faker::Internet.email
end

Then, in your test or in any other place:

@user = User.spawn

Or, if you need something special:

@user = User.spawn :name => "Michel Martens"

This sends to User.new all the attributes defined in the spawner block, along with the hash of attributes passed when spawning.

If you want a reference to the model before the validity is checked, you can pass a block:

@user = User.spawn { |u| u.name = "Michel Martens" }

Conditional evaluation

Spawn will execute the right hand side of your assignment even if you provide a value for some particular key. Consider the following example:

User.spawner do |user|
  user.profile = Profile.spawn # Profile.spawn is executed even if you provide a value for :profile.
end

User.spawn(:profile => Profile.first)

Here, you will be creating an extra instance of Profile, because when the block is evaluated it calls Profile.spawn. If the right hand side of your assignment is costly or has side effects, you may want to avoid this behavior by using ||=:

User.spawner do |user|
  user.profile ||= Profile.spawn
end

Then, if you pass a :profile:

User.spawn(:profile => Profile.first)

You can verify that Profile.spawn is never called. Although this may sound evident, it can bite you if you rely on the RHS not executing (e.g. if you're using Spawn to populate fake data into a database and you want to control how many instances you create).

Installation

$ sudo gem install spawn

Contributors

Thanks to Foca, Pedro and Diego for the suggestions and improvements to this tiny library.

License

Copyright (c) 2009 Michel Martens and Damian Janowski

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.

spawn's People

Contributors

soveran avatar djanowski avatar foca avatar

Stargazers

Gabriel Aires Guedes avatar Ivan Nemytchenko avatar william avatar Angus H. avatar Jacques Fuentes avatar Kevin J. Hanna avatar Ce Manalang avatar Tomas Mattia avatar Guillermo Iguaran avatar Fernando Espinosa avatar Claudio Acciaresi avatar James Zhang avatar Martin Trejo avatar Vincent Glennon avatar Gus Becciu avatar Rui Andrada avatar Erol Fornoles avatar Daniel Cadenas avatar Ignacio Carrera avatar Mike Stenhouse avatar Sean Wolfe avatar Luciano Panaro avatar Bruno Deferrari avatar Masafumi Fujiwara avatar Ariel Pillet avatar Joshua Hull avatar Matías Flores avatar Scott Taylor avatar Nikolay Kolev avatar 0x44 0x46 avatar  avatar  avatar Adrian Madrid avatar  avatar Lourens Naudé avatar Simon Rozet avatar

Watchers

 avatar  avatar James Cloos avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.