Module: Mongoid::Clients::StorageOptions::ClassMethods

Extended by:
Gem::Deprecate
Defined in:
lib/mongoid/clients/storage_options.rb

Instance Method Summary collapse

Instance Method Details

#client_nameSymbol Also known as: session_name

Get the client name for the model.

Examples:

Get the client name.

Model.client_name

Returns:

  • (Symbol)

    The name of the client.

Since:

  • 3.0.0



103
104
105
# File 'lib/mongoid/clients/storage_options.rb', line 103

def client_name
  __evaluate__(storage_options[:client])
end

#collection_nameSymbol

Get the name of the collection this model persists to.

Examples:

Get the collection name.

Model.collection_name

Returns:

  • (Symbol)

    The name of the collection.

Since:

  • 3.0.0



91
92
93
# File 'lib/mongoid/clients/storage_options.rb', line 91

def collection_name
  __evaluate__(storage_options[:collection])
end

#database_nameSymbol

Get the database name for the model.

Examples:

Get the database name.

Model.database_name

Returns:

  • (Symbol)

    The name of the client.

Since:

  • 4.0.0



117
118
119
# File 'lib/mongoid/clients/storage_options.rb', line 117

def database_name
  __evaluate__(storage_options[:database])
end

#reset_storage_options!Object

Reset the store_in options

Examples:

Reset the store_in options

Model.reset_storage_options!

Since:

  • 4.0.0



63
64
65
# File 'lib/mongoid/clients/storage_options.rb', line 63

def reset_storage_options!
  self.storage_options = storage_options_defaults.dup
end

#storage_options_defaultsHash

Get the default storage options.

Examples:

Get the default storage options.

Model.storage_options_defaults

Returns:

  • (Hash)

    Default storage options.

Since:

  • 4.0.0



75
76
77
78
79
80
81
# File 'lib/mongoid/clients/storage_options.rb', line 75

def storage_options_defaults
  {
    collection: name.collectionize.to_sym,
    client: :default,
    database: -> { configured_database }
  }
end

#store_in(options) ⇒ Class

Give this model specific custom default storage options.

Examples:

Store this model by default in “artists”

class Band
  include Mongoid::Document
  store_in collection: "artists"
end

Store this model by default in the sharded db.

class Band
  include Mongoid::Document
  store_in database: "echo_shard"
end

Store this model by default in a different client.

class Band
  include Mongoid::Document
  store_in client: "secondary"
end

Store this model with a combination of options.

class Band
  include Mongoid::Document
  store_in collection: "artists", database: "secondary"
end

Parameters:

  • options (Hash)

    The storage options.

Options Hash (options):

  • :collection (String, Symbol)

    The collection name.

  • :database (String, Symbol)

    The database name.

  • :client (String, Symbol)

    The client name.

Returns:

  • (Class)

    The model class.

Since:

  • 3.0.0



52
53
54
55
# File 'lib/mongoid/clients/storage_options.rb', line 52

def store_in(options)
  Validators::Storage.validate(self, options)
  storage_options.merge!(options)
end