Sha256: 0f9b9c4fe76e9aed289a858b0da04d4fa5e3fdb9ec653f184048224bc53fe075

Contents?: true

Size: 932 Bytes

Versions: 2

Compression:

Stored size: 932 Bytes

Contents

module Toy
  module Identity
    extend ActiveSupport::Concern

    included do
      key Toy.key_factory
    end

    module ClassMethods
      def key(name_or_factory = :uuid)
        @key_factory = case name_or_factory
        when :uuid
          UUIDKeyFactory.new
        when :object_id
          require 'toy/identity/object_id_key_factory'
          ObjectIdKeyFactory.new
        else
          if name_or_factory.respond_to?(:next_key) &&
             name_or_factory.respond_to?(:key_type)
            name_or_factory
          else
            raise InvalidKeyFactory.new(name_or_factory)
          end
        end
        attribute :id, @key_factory.key_type
        @key_factory
      end

      def key_type
        @key_factory.key_type
      end

      def next_key(object = nil)
        @key_factory.next_key(object).tap do |key|
          raise InvalidKey.new if key.nil?
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
toystore-0.6.5 lib/toy/identity.rb
toystore-0.6.4 lib/toy/identity.rb