Sha256: 3e0fb8ca7a27390a031a375d3b2098fd3ab709950cedf51b08cdd9abd3ef714a

Contents?: true

Size: 1.41 KB

Versions: 62

Compression:

Stored size: 1.41 KB

Contents

module Workarea
  # 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.
  #
  # @example 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 StringId < String
    class << self
      def mongoize(object)
        return if object.nil?
        object.to_s.send(:convert_to_object_id)
      end
      alias_method :demongoize, :mongoize

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

Version data entries

62 entries across 62 versions & 1 rubygems

Version Path
workarea-core-3.5.27 lib/workarea/string_id.rb
workarea-core-3.5.26 lib/workarea/string_id.rb
workarea-core-3.4.45 lib/workarea/string_id.rb
workarea-core-3.5.25 lib/workarea/string_id.rb
workarea-core-3.5.23 lib/workarea/string_id.rb
workarea-core-3.4.44 lib/workarea/string_id.rb
workarea-core-3.5.22 lib/workarea/string_id.rb
workarea-core-3.4.43 lib/workarea/string_id.rb
workarea-core-3.5.21 lib/workarea/string_id.rb
workarea-core-3.4.42 lib/workarea/string_id.rb
workarea-core-3.5.20 lib/workarea/string_id.rb
workarea-core-3.4.41 lib/workarea/string_id.rb
workarea-core-3.5.19 lib/workarea/string_id.rb
workarea-core-3.4.40 lib/workarea/string_id.rb
workarea-core-3.5.18 lib/workarea/string_id.rb
workarea-core-3.4.39 lib/workarea/string_id.rb
workarea-core-3.5.17 lib/workarea/string_id.rb
workarea-core-3.4.38 lib/workarea/string_id.rb
workarea-core-3.5.16 lib/workarea/string_id.rb
workarea-core-3.4.37 lib/workarea/string_id.rb