0.0
No commit activity in last 3 years
No release in over 3 years
A simple test data generator.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 5.0.0
~> 1.12
~> 10.0
~> 2.2.1
~> 3.0
~> 1.3.10
~> 1.1.1
 Project Readme

SimpleFactory

Gem Version Build Status

A simple test data generator.

Installation

Add this line to your application's Gemfile:

gem 'simple_factory'

And then execute:

$ bundle

Or install it yourself as:

$ gem install simple_factory

Usage

Define factory

# Assume that Item model was defined like this:
Item = Struct.new(:name, :price)

class ItemFactory < SimpleFactory::Factory
  define do
    # Define default params here.
    name 'Milk'
    price 100
  end

  def create(params)
    # Code to create an object here.
    Item.new(params[:name], params[:price])
  end
end

Use factory

# Simple usage
ItemFactory.create # => #<struct Item name="Milk", price=100>

# Use with overriding params
ItemFactory.create(name: 'Cookie', price: 298) # => #<struct Item name="Cookie", price=298>

Define Factory with YAML

model.yml:

_sample:
  - name: Milk
    price: 100
  - name: Cookie
    price: 298

Define a factory with YAML definitions:

class ItemFactory < SimpleFactory::Factory
  define '/path/to/model.yml'
  # or, if you set `SimpleFactory.definitions_dir = '/path/to'`, simply:
  #   define 'model.yml'

  def create(params)
    Item.new(params[:name], params[:price])
  end
end

Use the factory:

ItemFactory.create # returns Milk or Cookie randomly.

Other options:

name: # name will be "Taro" or "Hanako" randomly.
  _sample:
    - Taro
    - Hanako
email: # email will be "user1@example.com", "user2@example.com", ... in series.
  _sequence: 'user%{i}@example.com'

License

MIT