No commit activity in last 3 years
No release in over 3 years
Object caching for machinist
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 2.8.1
>= 2.6.0
 Project Readme

Machinist-Caching

Machinist with the object-caching feature

When you create an object with make!, Machinist-caching keeps the object around in the database. If, in a later test, you request an identical object, Machinist will give you the cached copy. This is a big performance win, particularly when creating objects in the setup for a group of tests.

Installation

gem install machinist-caching

Usage

    require 'machinist/caching/active_record'

    # Test::Unit
    class Test::Unit::TestCase
      def setup
        Machinist::Caching::Shop.instance.reset!
      end
    end

    # RSpec
    RSpec.configure do |config|
      config.before do
        Machinist::Caching::Shop.instance.reset!
      end
    end

Problem Scenario

Imagine that you have countries, states and cities model. And you have this blueprints:

    Country.blueprint(:brasil) do
      name { 'Brasil sil sil!!' }
    end

    State.blueprint(:minas_gerais) do
      acronym { 'MG' }
      name    { 'Minas Gerais' }
      country { Country.make!(:brasil) }
    end

    State.blueprint(:rio_grande_do_sul) do
      acronym { 'RS' }
      name    { 'Rio Grande do Sul' }
      country { Country.make!(:brasil) }
    end

    City.blueprint(:belo_horizonte) do
      digit { 1 }
      name  { 'Belo Horizonte' }
      state { State.make!(:minas_gerais) }
    end

    City.blueprint(:porto_alegre) do
      digit { 1 }
      name  { 'Porto Alegre' }
      state { State.make!(:rio_grande_do_sul) }
    end

And you have some validations in the models:

    class Country < ActiveRecord::Base
      validates :name, presence: true, uniqueness: { allow_blank: true }
    end

    class State < ActiveRecord::Base
      validates :name, presence: true, uniqueness: { allow_blank: true }
    end

    class City < ActiveRecord::Base
      validates :name, presence: true, uniqueness: { allow_blank: true }
    end

Instead of you writing this to create a city

    brasil = Country.make!(:brasil)

    minas_gerais = State.make!(:minas_gerais, country: brasil)
    rio_grande_do_sul = State.make!(:rio_grande_do_sul, country: brasil)

    City.make!(:belo_horizonte, state: minas_gerais)
    City.make!(:porto_alegre, state: rio_grande_do_sul)

With machinist-caching you can do this:

    City.make!(:belo_horizonte)
    City.make!(:porto_alegre)

Profit!

:)