class BSON::DBRef
def initialize(hash_or_collection, id = nil, database = nil)
-
(BSON::Error::InvalidDBRefArgument)
- if giving invalid arguments
Parameters:
-
database
(String
) -- The database name, when using the legacy API. -
id
(Object
) -- The object id, when using the legacy API. -
hash_or_collection
(Hash | String
) -- The DBRef hash, when using
Other tags:
- Example: Create the DBRef - legacy API. -
Example: Create the DBRef - hash API. -
def initialize(hash_or_collection, id = nil, database = nil) if hash_or_collection.is_a?(Hash) hash = hash_or_collection unless id.nil? && database.nil? raise Error::InvalidDBRefArgument, 'When using the hash API, DBRef constructor accepts only one argument' end else warn("BSON::DBRef constructor called with the legacy API - please use the hash API instead") if id.nil? raise Error::InvalidDBRefArgument, 'When using the legacy constructor API, id must be provided' end hash = { :$ref => hash_or_collection, :$id => id, :$db => database, } end hash = reorder_fields(hash) %w($ref $id).each do |key| unless hash[key] raise Error::InvalidDBRefArgument, "DBRef must have #{key}: #{hash}" end end unless hash['$ref'].is_a?(String) raise Error::InvalidDBRefArgument, "The value for key $ref must be a string, got: #{hash['$ref']}" end if db = hash['$db'] unless db.is_a?(String) raise Error::InvalidDBRefArgument, "The value for key $db must be a string, got: #{hash['$db']}" end end super(hash) end