Skip to content

Instantly share code, notes, and snippets.

@maurcs
Created June 24, 2010 12:46
Show Gist options
  • Save maurcs/451404 to your computer and use it in GitHub Desktop.
Save maurcs/451404 to your computer and use it in GitHub Desktop.
What I did to get mongo_mapper and machinist to work with pickle.
# spec/pickle/adapter.rb
module Pickle
class Adapter
class MongoMapper < Adapter
def self.factories
factories = []
model_classes.each do |klass|
if blueprints = klass.instance_variable_get('@blueprints')
blueprints.keys.each {|blueprint| factories << new(klass, blueprint)}
end
end
factories
end
def initialize(klass, blueprint)
@klass, @blueprint = klass, blueprint
@name = @klass.name.underscore.gsub('/','_')
@name = "#{@blueprint}_#{@name}" unless @blueprint == :master
end
def create(attrs = {})
@klass.send(:make, @blueprint, attrs)
end
end
end
end
# spec/blueprints.rb
# include these in blueprints
# need machinist/mongo gem
require 'machinist/mongo_mapper'
require File.expand_path(File.dirname(__FILE__)) + "/pickle/adapter"
require File.expand_path(File.dirname(__FILE__)) + "/pickle/adapters/mongo_mapper"
# features/support/env.rb
require File.dirname(__FILE__) + '/../../spec/blueprints' # or wherever your blueprints are
Before { Sham.reset } # to reset Sham's seed between scenarios so each run has same random sequences
require 'pickle/world'
# Example of configuring pickle:
#
Pickle.configure do |config|
config.adapters = [MongoMapper]
config.map 'I', 'myself', 'me', 'my', :to => 'user: "me"'
end
# spec/pickle/adapters/mongo_mapper.rb
require 'mongo_mapper'
module MongoMapper
module Document
module PickleAdapter
include Pickle::Adapter::Base
# Do not consider these to be part of the class list
def self.except_classes
end
# Gets a list of the available models for this adapter
def self.model_classes
@model_classes ||= Module.constants.inject([]) do |h, c|
constant = eval(c)
if !constant.nil? && constant.is_a?(Class) && constant.include?(MongoMapper::Document)
h << constant
end
h
end
end
# get a list of column names for a given class
def self.column_names(klass)
klass.column_names
end
# Get an instance by id of the model
def self.get_model(klass, id)
klass.find(id)
end
# Find the first instance matching conditions
def self.find_first_model(klass, conditions)
klass.first conditions
end
# Find all models matching conditions
def self.find_all_models(klass, conditions)
klass.all conditions
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment