Module: Toy::Identity::ClassMethods

Defined in:
lib/toy/identity.rb

Instance Method Summary collapse

Instance Method Details

#key(name_or_factory = :uuid, options = {}) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/toy/identity.rb', line 10

def key(name_or_factory = :uuid, options = {})
  @key_factory = if name_or_factory == :uuid
    UUIDKeyFactory.new
  elsif name_or_factory == :native_uuid
    NativeUUIDKeyFactory.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, :virtual => true
  @key_factory
end

#key_factoryObject



27
28
29
# File 'lib/toy/identity.rb', line 27

def key_factory
  @key_factory || raise('Set your key_factory using key(...)')
end

#key_typeObject



31
32
33
# File 'lib/toy/identity.rb', line 31

def key_type
  @key_factory.key_type
end

#next_key(object = nil) ⇒ Object



35
36
37
38
39
# File 'lib/toy/identity.rb', line 35

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