Sha256: 00471ff442781bd150ffabd0a161248c6eba510d6275e6c9fa118da3d0b161b0

Contents?: true

Size: 1.5 KB

Versions: 1

Compression:

Stored size: 1.5 KB

Contents

class Factory
  class Proxy #:nodoc:
    class Create < Build #:nodoc:
      cattr_reader :instances
      @@instances = {}

      # Clear caches.
      #
      # You need to call this method (Or ::Factory.clear_caches) on every
      # tearing down of a database in each test (e.g: RSpec).
      #
      # With RSpec, you may want to  call this method in global before block 
      # in spec_helper.rb.
      #
      # Example 
      #   config.before(:each) {
      #     ::Factory.clear_caches
      #   }
      #
      def self.clear_caches
        @@instances = {}
      end

      def result
        try
      end

      private
      # Save new instance.
      #
      # If an exception occured during @instace.save!, rewrite error
      # message to identify that which class has raised an exception easily.
      def save_instance
        run_callbacks(:after_build)
        begin
          @instance.save!
          run_callbacks(:after_create)
        rescue => e 
          n = Exception.new("#{@instance.class.to_s} #{e.message}")
          n.set_backtrace(e.backtrace)
          raise n
        end
        @instance
      end

      # Try to use a cache. Instances to be cached is set :singleton
      # option when you call Factory.define .
      def try
        if singleton
          if @@instances[factory_name].nil? 
            @@instances[factory_name] = save_instance
          else
            @@instances[factory_name]
          end
        else
          save_instance
        end
      end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
masa-iwasaki-factory_girl-1.2.3.1 lib/factory_girl/proxy/create.rb