Method: BSON::DBRef#initialize
- Defined in:
- lib/bson/dbref.rb
#initialize(hash_or_collection, id = nil, database = nil) ⇒ DBRef
Instantiate a new DBRef.
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/bson/dbref.rb', line 80 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 |