Class: Activr::Storage::MongoDriver
- Inherits:
- 
      Object
      
        - Object
- Activr::Storage::MongoDriver
 
- Defined in:
- lib/activr/storage/mongo_driver.rb
Overview
Generic Mongodb driver
This is main interface with the underlying MongobDB driver, which can be either the official ‘mongo` driver or `moped`, the `mongoid` driver.
Instance Method Summary collapse
- 
  
    
      #activities_selector(options)  ⇒ Hash 
    
    
  
  
  
  
  
  
  
  private
  
    Compute selector for querying ‘activities` collection. 
- 
  
    
      #activity_collection  ⇒ Mongo::Collection, Moped::Collection 
    
    
  
  
  
  
  
  
  
  private
  
    Get handler for ‘activities` collection. 
- 
  
    
      #add_activity_index(index, options = { })  ⇒ String 
    
    
  
  
  
  
  
  
  
  private
  
    Add index for activities. 
- 
  
    
      #add_index(col, index_spec, options = { })  ⇒ String 
    
    
  
  
  
  
  
  
  
  private
  
    Add index to given collection. 
- 
  
    
      #add_timeline_index(timeline_kind, index, options = { })  ⇒ String 
    
    
  
  
  
  
  
  
  
  private
  
    Add index for timeline entries. 
- 
  
    
      #collection(col_name)  ⇒ Mongo::Collection, Moped::Collection 
    
    
  
  
  
  
  
  
  
  private
  
    Mongodb collection. 
- 
  
    
      #config  ⇒ hash 
    
    
  
  
  
  
  
  
  
  private
  
    MongoDB config. 
- 
  
    
      #conn  ⇒ Mongo::MongoClient, ... 
    
    
  
  
  
  
  
  
  
  private
  
    Mongodb connection/session. 
- 
  
    
      #count(col, selector)  ⇒ Integer 
    
    
  
  
  
  
  
  
  
  private
  
    Count documents in given collection. 
- 
  
    
      #count_activities(options = { })  ⇒ Integer 
    
    
  
  
  
  
  
  
  
  private
  
    Count number of activities. 
- 
  
    
      #count_timeline_entries(timeline_kind, recipient_id, options = { })  ⇒ Integer 
    
    
  
  
  
  
  
  
  
  private
  
    Count number of timeline entry documents. 
- 
  
    
      #create_indexes {|String| ... } ⇒ Object 
    
    
  
  
  
  
  
  
  
  private
  
    Ensure all necessary indexes. 
- 
  
    
      #delete(col, selector)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  private
  
    Delete documents in given collection. 
- #delete_activities(options = { }) ⇒ Object private
- 
  
    
      #delete_timeline_entries(timeline_kind, recipient_id, options = { })  ⇒ Object 
    
    
  
  
  
  
  
  
  
  private
  
    Delete timeline entry documents. 
- 
  
    
      #drop_indexes(col)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  private
  
    Drop all indexes for given collection. 
- 
  
    
      #find(col, selector, limit, skip, sort_field = nil)  ⇒ Enumerable 
    
    
  
  
  
  
  
  
  
  private
  
    Find documents in given collection. 
- 
  
    
      #find_activities(limit, options = { })  ⇒ Array<Activity> 
    
    
  
  
  
  
  
  
  
  private
  
    Find latest activities. 
- 
  
    
      #find_activity(activity_id)  ⇒ Hash, ... 
    
    
  
  
  
  
  
  
  
  private
  
    Find an activity document. 
- 
  
    
      #find_one(col, selector)  ⇒ Hash, ... 
    
    
  
  
  
  
  
  
  
  private
  
    Find a document by id. 
- 
  
    
      #find_timeline_entries(timeline_kind, recipient_id, limit, options = { })  ⇒ Array<Hash> 
    
    
  
  
  
  
  
  
  
  private
  
    Find several timeline entry documents. 
- 
  
    
      #find_timeline_entry(timeline_kind, tl_entry_id)  ⇒ Hash, ... 
    
    
  
  
  
  
  
  
  
  private
  
    Find a timeline entry document. 
- 
  
    
      #indexes(col)  ⇒ Array<String> 
    
    
  
  
  
  
  
  
  
  private
  
    Get all indexes for given collection. 
- 
  
    
      #initialize  ⇒ MongoDriver 
    
    
  
  
  
    constructor
  
  
  
  
  
  
  
    A new instance of MongoDriver. 
- 
  
    
      #insert(col, doc)  ⇒ BSON::ObjectId, Moped::BSON::ObjectId 
    
    
  
  
  
  
  
  
  
  private
  
    Insert a document into given collection. 
- 
  
    
      #insert_activity(activity_hash)  ⇒ BSON::ObjectId, Moped::BSON::ObjectId 
    
    
  
  
  
  
  
  
  
  private
  
    Insert an activity document. 
- 
  
    
      #insert_timeline_entry(timeline_kind, timeline_entry_hash)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  private
  
    Insert a timeline entry document. 
- 
  
    
      #serialized_id?(doc_id)  ⇒ true, false 
    
    
  
  
  
  
  
  
  
  
  
    Is it a serialized document id (ie. with format { ‘$oid’ => … }). 
- 
  
    
      #timeline_collection(kind)  ⇒ Mongo::Collection, Moped::Collection 
    
    
  
  
  
  
  
  
  
  private
  
    Get handler for a ‘<kind>_timelines` collection. 
- 
  
    
      #timeline_selector(timeline_kind, recipient_id, options = { })  ⇒ Hash 
    
    
  
  
  
  
  
  
  
  private
  
    Compute selector for querying a ‘*_timelines` collection. 
- 
  
    
      #unserialize_id(doc_id)  ⇒ BSON::ObjectId, Moped::BSON::ObjectId 
    
    
  
  
  
  
  
  
  
  
  
    Unserialize a document id. 
- 
  
    
      #valid_id?(doc_id)  ⇒ true, false 
    
    
  
  
  
  
  
  
  
  
  
    Is it a valid document id. 
Constructor Details
#initialize ⇒ MongoDriver
Returns a new instance of MongoDriver.
| 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | # File 'lib/activr/storage/mongo_driver.rb', line 20 def initialize # check settings raise "Missing setting :uri in config: #{self.config.inspect}" if self.config[:uri].blank? @collections = { } @kind = if defined?(::Moped) if defined?(::Moped::BSON) # moped < 2.0.0 :moped_1 else # moped driver :moped end elsif defined?(::Mongo::MongoClient) # mongo ruby driver < 2.0.0 :mongo elsif defined?(::Mongo::Client) raise "Sorry, mongo gem >= 2.0 is not supported yet" else raise "Can't find any suitable mongodb driver: please install 'mongo' or 'moped' gem" end # Activr.logger.info("Using mongodb driver: #{@kind}") if @kind == :mongo uri = URI.parse(self.config[:uri]) @db_name = uri.path[1..-1] raise "Missing database name in setting uri: #{config[:uri]}" if @db_name.blank? end end | 
Instance Method Details
#activities_selector(options) ⇒ Hash
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Compute selector for querying ‘activities` collection
| 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 | # File 'lib/activr/storage/mongo_driver.rb', line 383 def activities_selector() result = { } # compute selector if [:before] result['at'] ||= { } result['at']["$lt"] = [:before] end if [:after] result['at'] ||= { } result['at']["$gt"] = [:after] end ([:entities] || { }).each do |name, value| result[name.to_s] = value end if ![:only].blank? result['kind'] ||= { } result['kind']['$in'] = [:only].map(&:kind) end if ![:except].blank? result['kind'] ||= { } result['kind']['$nin'] = [:except].map(&:kind) end result end | 
#activity_collection ⇒ Mongo::Collection, Moped::Collection
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Get handler for ‘activities` collection
| 275 276 277 278 279 280 281 282 283 284 285 | # File 'lib/activr/storage/mongo_driver.rb', line 275 def activity_collection @activity_collection ||= begin col_name = self.config[:activities_col] if col_name.nil? col_name = "activities" col_name = "#{self.config[:col_prefix]}_#{col_name}" unless self.config[:col_prefix].blank? end self.collection(col_name) end end | 
#add_activity_index(index, options = { }) ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Add index for activities
| 450 451 452 453 454 455 | # File 'lib/activr/storage/mongo_driver.rb', line 450 def add_activity_index(index, = { }) index = index.is_a?(Array) ? index : [ index ] index_spec = index.map{ |field| [ field, 1 ] } self.add_index(self.activity_collection, index_spec, ) end | 
#add_index(col, index_spec, options = { }) ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Add index to given collection
| 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 | # File 'lib/activr/storage/mongo_driver.rb', line 211 def add_index(col, index_spec, = { }) = { :background => true, :sparse => false, }.merge() case @kind when :moped_1, :moped index_spec = index_spec.inject(ActiveSupport::OrderedHash.new) do |memo, field_spec| memo[field_spec[0]] = field_spec[1] memo end col.indexes.create(index_spec, ) index_spec when :mongo col.create_index(index_spec, ) end end | 
#add_timeline_index(timeline_kind, index, options = { }) ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Add index for timeline entries
| 571 572 573 574 575 576 | # File 'lib/activr/storage/mongo_driver.rb', line 571 def add_timeline_index(timeline_kind, index, = { }) index = index.is_a?(Array) ? index : [ index ] index_spec = index.map{ |field| [ field, 1 ] } self.add_index(self.timeline_collection(timeline_kind), index_spec, ) end | 
#collection(col_name) ⇒ Mongo::Collection, Moped::Collection
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Mongodb collection
| 84 85 86 87 88 89 90 91 | # File 'lib/activr/storage/mongo_driver.rb', line 84 def collection(col_name) case @kind when :moped_1, :moped self.conn[col_name] when :mongo self.conn.db(@db_name).collection(col_name) end end | 
#config ⇒ hash
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
MongoDB config
| 58 59 60 | # File 'lib/activr/storage/mongo_driver.rb', line 58 def config Activr.config.mongodb end | 
#conn ⇒ Mongo::MongoClient, ...
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Mongodb connection/session
| 67 68 69 70 71 72 73 74 75 76 | # File 'lib/activr/storage/mongo_driver.rb', line 67 def conn @conn ||= begin case @kind when :moped_1, :moped ::Moped::Session.connect(self.config[:uri]) when :mongo ::Mongo::MongoClient.from_uri(self.config[:uri]) end end end | 
#count(col, selector) ⇒ Integer
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Count documents in given collection
| 179 180 181 182 183 184 | # File 'lib/activr/storage/mongo_driver.rb', line 179 def count(col, selector) case @kind when :moped_1, :moped, :mongo col.find(selector).count() end end | 
#count_activities(options = { }) ⇒ Integer
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
If you use one of options selectors then you have to setup corresponding indexes in database.
Count number of activities
| 427 428 429 430 431 | # File 'lib/activr/storage/mongo_driver.rb', line 427 def count_activities( = { }) selector = [:mongo_selector] || self.activities_selector() self.count(self.activity_collection, selector) end | 
#count_timeline_entries(timeline_kind, recipient_id, options = { }) ⇒ Integer
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Count number of timeline entry documents
| 538 539 540 541 542 | # File 'lib/activr/storage/mongo_driver.rb', line 538 def count_timeline_entries(timeline_kind, recipient_id, = { }) selector = [:mongo_selector] || self.timeline_selector(timeline_kind, recipient_id, ) self.count(self.timeline_collection(timeline_kind), selector) end | 
#create_indexes {|String| ... } ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Ensure all necessary indexes
| 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 | # File 'lib/activr/storage/mongo_driver.rb', line 586 def create_indexes # Create indexes on 'activities' collection for models that includes Activr::Entity::ModelMixin # # eg: activities # [['actor', Mongo::ASCENDING], ['at', Mongo::ASCENDING]] # [['album', Mongo::ASCENDING], ['at', Mongo::ASCENDING]] # [['picture', Mongo::ASCENDING], ['at', Mongo::ASCENDING]] Activr.registry.models.each do |model_class| if !model_class.activr_entity_settings[:feed_index] # @todo Output a warning to remove the index if it exists else fields = [ model_class.activr_entity_feed_actual_name.to_s, 'at' ] index_name = self.add_activity_index(fields) yield("activity / #{index_name}") if block_given? end end # Create indexes on '*_timelines' collections for defined timeline classes # # eg: user_news_feed_timelines # [['rcpt', Mongo::ASCENDING], ['activity.at', Mongo::ASCENDING]] Activr.registry.timelines.each do |timeline_kind, timeline_class| fields = [ 'rcpt', 'activity.at' ] index_name = self.add_timeline_index(timeline_kind, fields) yield("#{timeline_kind} timeline / #{index_name}") if block_given? end # Create sparse indexes to remove activities and timeline entries when entity is deleted # # eg: activities # [['actor', Mongo::ASCENDING]], :sparse => true # # eg: user_news_feed_timelines # [['activity.actor', Mongo::ASCENDING]], :sparse => true # [['activity.album', Mongo::ASCENDING]], :sparse => true # [['activity.picture', Mongo::ASCENDING]], :sparse => true Activr.registry.models.each do |model_class| if model_class.activr_entity_settings[:deletable] # create sparse index on `activities` Activr.registry.activity_entities_for_model(model_class).each do |entity_name| # if entity activity feed is enabled and this is the entity name used to fetch that feed then we can use the existing index... if !model_class.activr_entity_settings[:feed_index] || (entity_name != model_class.activr_entity_feed_actual_name) # ... else we create an index index_name = self.add_activity_index(entity_name.to_s, :sparse => true) yield("activity / #{index_name}") if block_given? end end # create sparse index on timeline classes where that entity can be present Activr.registry.timeline_entities_for_model(model_class).each do |timeline_class, entities| entities.each do |entity_name| index_name = self.add_timeline_index(timeline_class.kind, "activity.#{entity_name}", :sparse => true) yield("#{timeline_class.kind} timeline / #{index_name}") if block_given? end end end end end | 
#delete(col, selector) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Delete documents in given collection
| 192 193 194 195 196 197 198 199 | # File 'lib/activr/storage/mongo_driver.rb', line 192 def delete(col, selector) case @kind when :moped_1, :moped col.find(selector).remove_all when :mongo col.remove(selector) end end | 
#delete_activities(options = { }) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
| 436 437 438 439 440 | # File 'lib/activr/storage/mongo_driver.rb', line 436 def delete_activities( = { }) selector = [:mongo_selector] || self.activities_selector() self.delete(self.activity_collection, selector) end | 
#delete_timeline_entries(timeline_kind, recipient_id, options = { }) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Delete timeline entry documents
WARNING: If recipient_id is ‘nil` then documents are deleted for ALL recipients
| 553 554 555 556 557 558 559 560 | # File 'lib/activr/storage/mongo_driver.rb', line 553 def delete_timeline_entries(timeline_kind, recipient_id, = { }) selector = [:mongo_selector] || self.timeline_selector(timeline_kind, recipient_id, ) # "end of the world" check raise "Deleting everything is not the solution" if selector.blank? self.delete(self.timeline_collection(timeline_kind), selector) end | 
#drop_indexes(col) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Drop all indexes for given collection
| 260 261 262 263 264 265 266 267 268 | # File 'lib/activr/storage/mongo_driver.rb', line 260 def drop_indexes(col) case @kind when :moped_1, :moped col.indexes.drop when :mongo col.drop_indexes end end | 
#find(col, selector, limit, skip, sort_field = nil) ⇒ Enumerable
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Find documents in given collection
| 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 | # File 'lib/activr/storage/mongo_driver.rb', line 151 def find(col, selector, limit, skip, sort_field = nil) case @kind when :moped_1, :moped result = col.find(selector).skip(skip).limit(limit) result.sort(sort_field => -1) if sort_field result when :mongo # compute options hash = { :limit => limit, :skip => skip, } [:sort] = [ sort_field, ::Mongo::DESCENDING ] if sort_field [:batch_size] = 100 if (limit > 100) col.find(selector, ) end end | 
#find_activities(limit, options = { }) ⇒ Array<Activity>
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
If you use others selectors then ‘limit’ argument and ‘skip’ option then you have to setup corresponding indexes in database.
Find latest activities
| 417 418 419 420 421 422 | # File 'lib/activr/storage/mongo_driver.rb', line 417 def find_activities(limit, = { }) selector = [:mongo_selector] || self.activities_selector() sort_field = [:mongo_sort_field] || 'at' self.find(self.activity_collection, selector, limit, [:skip], sort_field) end | 
#find_activity(activity_id) ⇒ Hash, ...
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Find an activity document
| 373 374 375 | # File 'lib/activr/storage/mongo_driver.rb', line 373 def find_activity(activity_id) self.find_one(self.activity_collection, { '_id' => activity_id }) end | 
#find_one(col, selector) ⇒ Hash, ...
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Find a document by id
| 132 133 134 135 136 137 138 139 | # File 'lib/activr/storage/mongo_driver.rb', line 132 def find_one(col, selector) case @kind when :moped_1, :moped col.find(selector).one when :mongo col.find_one(selector) end end | 
#find_timeline_entries(timeline_kind, recipient_id, limit, options = { }) ⇒ Array<Hash>
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Find several timeline entry documents
| 523 524 525 526 527 528 | # File 'lib/activr/storage/mongo_driver.rb', line 523 def find_timeline_entries(timeline_kind, recipient_id, limit, = { }) selector = [:mongo_selector] || self.timeline_selector(timeline_kind, recipient_id, ) sort_field = [:mongo_sort_field] || 'activity.at' self.find(self.timeline_collection(timeline_kind), selector, limit, [:skip], sort_field) end | 
#find_timeline_entry(timeline_kind, tl_entry_id) ⇒ Hash, ...
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Find a timeline entry document
| 479 480 481 | # File 'lib/activr/storage/mongo_driver.rb', line 479 def find_timeline_entry(timeline_kind, tl_entry_id) self.find_one(self.timeline_collection(timeline_kind), { '_id' => tl_entry_id }) end | 
#indexes(col) ⇒ Array<String>
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Get all indexes for given collection
| 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 | # File 'lib/activr/storage/mongo_driver.rb', line 239 def indexes(col) result = [ ] case @kind when :moped_1, :moped col.indexes.each do |index_spec| result << index_spec["name"] end when :mongo result = col.index_information.keys end result end | 
#insert(col, doc) ⇒ BSON::ObjectId, Moped::BSON::ObjectId
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Insert a document into given collection
| 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 | # File 'lib/activr/storage/mongo_driver.rb', line 100 def insert(col, doc) case @kind when :moped_1, :moped doc_id = doc[:_id] || doc['_id'] if doc_id.nil? doc_id = case @kind when :moped_1 # Moped < 2.0.0 uses a custom BSON implementation ::Moped::BSON::ObjectId.new when :moped # Moped >= 2.0.0 uses bson gem ::BSON::ObjectId.new end doc['_id'] = doc_id end col.insert(doc) doc_id when :mongo col.insert(doc) end end | 
#insert_activity(activity_hash) ⇒ BSON::ObjectId, Moped::BSON::ObjectId
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Insert an activity document
| 363 364 365 | # File 'lib/activr/storage/mongo_driver.rb', line 363 def insert_activity(activity_hash) self.insert(self.activity_collection, activity_hash) end | 
#insert_timeline_entry(timeline_kind, timeline_entry_hash) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Insert a timeline entry document
| 468 469 470 | # File 'lib/activr/storage/mongo_driver.rb', line 468 def insert_timeline_entry(timeline_kind, timeline_entry_hash) self.insert(self.timeline_collection(timeline_kind), timeline_entry_hash) end | 
#serialized_id?(doc_id) ⇒ true, false
Is it a serialized document id (ie. with format { ‘$oid’ => … })
| 324 325 326 | # File 'lib/activr/storage/mongo_driver.rb', line 324 def serialized_id?(doc_id) doc_id.is_a?(Hash) && !doc_id['$oid'].blank? end | 
#timeline_collection(kind) ⇒ Mongo::Collection, Moped::Collection
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Get handler for a ‘<kind>_timelines` collection
| 293 294 295 296 297 298 299 300 301 302 303 304 | # File 'lib/activr/storage/mongo_driver.rb', line 293 def timeline_collection(kind) @timeline_collection ||= { } @timeline_collection[kind] ||= begin col_name = self.config[:timelines_col] if col_name.nil? col_name = "#{kind}_timelines" col_name = "#{self.config[:col_prefix]}_#{col_name}" unless self.config[:col_prefix].blank? end self.collection(col_name) end end | 
#timeline_selector(timeline_kind, recipient_id, options = { }) ⇒ Hash
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Compute selector for querying a ‘*_timelines` collection
| 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 | # File 'lib/activr/storage/mongo_driver.rb', line 491 def timeline_selector(timeline_kind, recipient_id, = { }) result = { } # compute selector result['rcpt'] = recipient_id unless recipient_id.nil? if [:before] result['activity.at'] = { "$lt" => [:before] } end ([:entities] || { }).each do |name, value| result["activity.#{name}"] = value end if ![:only].blank? result['$or'] = [:only].map do |route| { 'routing' => route.routing_kind, 'activity.kind' => route.activity_class.kind } end end result end | 
#unserialize_id(doc_id) ⇒ BSON::ObjectId, Moped::BSON::ObjectId
Unserialize a document id
| 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 | # File 'lib/activr/storage/mongo_driver.rb', line 332 def unserialize_id(doc_id) # get string representation doc_id = self.serialized_id?(doc_id) ? doc_id['$oid'] : doc_id if @kind == :moped_1 # Moped < 2.0.0 uses a custom BSON implementation if doc_id.is_a?(::Moped::BSON::ObjectId) doc_id else ::Moped::BSON::ObjectId(doc_id) end else if doc_id.is_a?(::BSON::ObjectId) doc_id else ::BSON::ObjectId.from_string(doc_id) end end end | 
#valid_id?(doc_id) ⇒ true, false
Is it a valid document id
| 312 313 314 315 316 317 318 319 | # File 'lib/activr/storage/mongo_driver.rb', line 312 def valid_id?(doc_id) case @kind when :moped_1 doc_id.is_a?(String) || doc_id.is_a?(::Moped::BSON::ObjectId) when :mongo, :moped doc_id.is_a?(String) || doc_id.is_a?(::BSON::ObjectId) end end |