class Mongoid::GridFs

Constants

NamespaceMixin
VERSION

Attributes

chunk_model[RW]
file_model[RW]
namespace[RW]

Public Class Methods

build_namespace_for(prefix) click to toggle source
# File lib/mongoid/grid_fs.rb, line 54
def self.build_namespace_for(prefix)
  prefix = prefix.to_s.downcase
  const = prefix.camelize

  namespace =
    Module.new do
      module_eval(&NamespaceMixin)
      self
    end

  const_set(const, namespace)

  file_model = build_file_model_for(namespace)
  chunk_model = build_chunk_model_for(namespace)

  file_model.namespace = namespace
  chunk_model.namespace = namespace

  file_model.chunk_model = chunk_model
  chunk_model.file_model = file_model

  namespace.prefix = prefix
  namespace.file_model = file_model
  namespace.chunk_model = chunk_model

  namespace.send(:const_set, :File, file_model)
  namespace.send(:const_set, :Chunk, chunk_model)

  # at_exit{ file_model.create_indexes rescue nil }
  # at_exit{ chunk_model.create_indexes rescue nil }

  const_get(const)
end
init!() click to toggle source
# File lib/mongoid/grid_fs.rb, line 13
      def init!
        GridFs.build_namespace_for(:Fs)

        GridFs.namespace = Fs
        GridFs.file_model = Fs.file_model
        GridFs.chunk_model = Fs.chunk_model

        const_set(:File, Fs.file_model)
        const_set(:Chunk, Fs.chunk_model)

        to_delegate = %w(
          put
          get
          delete
          find
          []
          []=
          clear
        )

        to_delegate.each do |method|
          class_eval <<-__
              def self.#{method}(*args, &block)
                ::Mongoid::GridFs::Fs::#{method}(*args, &block)
              end
            __
        end
      end
namespace_for(prefix) click to toggle source
# File lib/mongoid/grid_fs.rb, line 45
def self.namespace_for(prefix)
  prefix = prefix.to_s.downcase
  const = "::GridFs::#{prefix.to_s.camelize}"
  namespace = const.split(/::/).last
  const_defined?(namespace) ? const_get(namespace) : build_namespace_for(namespace)
end