Sha256: 721dfc43a4958e3e7d61f4109b310de051f64ca2773bffeff03621fb86504095

Contents?: true

Size: 1.93 KB

Versions: 2

Compression:

Stored size: 1.93 KB

Contents

require 'machinist'
require 'machinist/blueprints'

module Machinist
  
  class MongoMapperAdapter
    def self.has_association?(object, attribute)
      object.class.associations[attribute]
    end
    
    def self.class_for_association(object, attribute)
      association = object.class.associations[attribute]
      association && association.klass
    end
    
    def self.assigned_attributes_without_associations(lathe)
      attributes = {}
      lathe.assigned_attributes.each_pair do |attribute, value|
        association = lathe.object.class.associations[attribute]
        if association && association.belongs_to?
          attributes[association.foreign_key.to_sym] = value.id
        else
          attributes[attribute] = value
        end
      end
      attributes
    end
  end

  module MongoMapperExtensions
    module Document
      def make(*args, &block)
        lathe = Lathe.run(Machinist::MongoMapperAdapter, self.new, *args)
        lathe.object.save! unless Machinist.nerfed?
        lathe.object(&block)
      end

      def make_unsaved(*args)
        returning(Machinist.with_save_nerfed { make(*args) }) do |object|
          yield object if block_given?
        end
      end

      def plan(*args)
        lathe = Lathe.run(Machinist::MongoMapperAdapter, self.new, *args)
        Machinist::MongoMapperAdapter.assigned_attributes_without_associations(lathe)
      end
    end
    
    module EmbeddedDocument
      def make(*args, &block)
        lathe = Lathe.run(Machinist::MongoMapperAdapter, self.new, *args)
        lathe.object(&block)
      end
    end
  end
end

MongoMapper::Document.append_extensions(Machinist::Blueprints::ClassMethods)
MongoMapper::Document.append_extensions(Machinist::MongoMapperExtensions::Document)
MongoMapper::EmbeddedDocument::ClassMethods.send(:include, Machinist::Blueprints::ClassMethods)
MongoMapper::EmbeddedDocument::ClassMethods.send(:include, Machinist::MongoMapperExtensions::EmbeddedDocument)

Version data entries

2 entries across 2 versions & 2 rubygems

Version Path
machinist_mongomapper-1.0.0 lib/machinist/mongomapper.rb
machinist_mongo-1.0.1 lib/machinist/mongo_mapper.rb