Sha256: bee68ef4be4eba18dc05c3c9470848a03697282a22451646ff7b52ce6c89fefd

Contents?: true

Size: 1.95 KB

Versions: 3

Compression:

Stored size: 1.95 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::ClassMethods.send(:include, Machinist::Blueprints::ClassMethods)
MongoMapper::Document::ClassMethods.send(:include, Machinist::MongoMapperExtensions::Document)
MongoMapper::EmbeddedDocument::ClassMethods.send(:include, Machinist::Blueprints::ClassMethods)
MongoMapper::EmbeddedDocument::ClassMethods.send(:include, Machinist::MongoMapperExtensions::EmbeddedDocument)

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
machinist_mongomapper-0.9.7 lib/machinist/mongomapper.rb
machinist_mongomapper-0.9.6 lib/machinist/mongomapper.rb
machinist_mongomapper-0.9.5 lib/machinist/mongomapper.rb