Class: Workarea::StringId

Inherits:
String
  • Object
show all
Defined in:
lib/workarea/string_id.rb

Overview

TODO: Remove in v4 A class used strictly for allowing custom strings for model ids while maintaining support for the use of a BSON::ObjectId as the default. Documents within a collection using StringId for _id can be created and queried with either a String or BSON::ObjectId. Legal object ids will be converted before reading or writing from the database.

Examples:

model using StringId

class Foo
  include Mongoid::Document
  field :_id, type: StringId, default: -> { BSON::ObjectId.new }
end

Creating documents:
  Foo.create
  Foo.create('5b8ff8e84907b7367471aded')
  Foo.create('some_custom_id')

Valid queries:
  foo = Foo.find('5b8ff8e84907b7367471aded')
  foo.id #=> BSON::ObjectId('5b8ff8e84907b7367471aded')

  foo = Foo.find(BSON::ObjectId.from_string('5b8ff8e84907b7367471aded'))
  foo.id #=> BSON::ObjectId('5b8ff8e84907b7367471aded')

  foo = Foo.find('some_custom_id')
  foo.id #=> "some_custom_id"

Class Method Summary collapse

Methods inherited from String

#optionize, optionize_seperator_regexes, optionize_unwanted_chars_regex

Class Method Details

.evolve(object) ⇒ Object



38
39
40
41
42
# File 'lib/workarea/string_id.rb', line 38

def evolve(object)
  __evolve__(object) do |obj|
    obj.regexp? ? obj : obj.to_s.send(:convert_to_object_id)
  end
end

.mongoize(object) ⇒ Object Also known as: demongoize



32
33
34
35
# File 'lib/workarea/string_id.rb', line 32

def mongoize(object)
  return if object.nil?
  object.to_s.send(:convert_to_object_id)
end