Module: Lims::Core::Persistence::Uuidable

Included in:
Sequel::Session
Defined in:
lib/lims-core/persistence/uuidable.rb

Overview

Add uuid behavior (lookup and creation) to a Session

Instance Method Summary collapse

Instance Method Details

#[](args) ⇒ Resource, ...

lookup one or more objects by uuid or resource_uuid

Parameters:

  • args (String, Arrary<String>, UUidResource)

Returns:



14
15
16
17
18
19
20
21
22
# File 'lib/lims-core/persistence/uuidable.rb', line 14

def [](args)
  case args
  when UuidResource then for_uuid_resource(args)
  when String then for_uuid(args)
  when Array then for_uuids(args)
  else
    super(args)
  end
end

#class_for(model_name) ⇒ Object

Get the class from the class name. Inverse of #model_name_for.



47
48
49
# File 'lib/lims-core/persistence/uuidable.rb', line 47

def class_for(model_name)
  persistor_for(model_name).model
end

#delete_resource(uuid_resource) ⇒ Id?

Delete the underlying resource of a UuidResource

Parameters:

Returns:

  • (Id, nil)


80
81
82
83
# File 'lib/lims-core/persistence/uuidable.rb', line 80

def delete_resource(uuid_resource)
  delete(for_uuid_resource(uuid_resource))
  uuid_resource.key
end

#id_for(object) ⇒ Object

Retrieve id from an object or a Hash with a uuid key A list of uuids will



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/lims-core/persistence/uuidable.rb', line 26

def id_for(object)
  case object
  when Array
    object.map { |o| id_for(o) }
  when Hash
    id_for(object[:uuid] || object["uuid"])
  when String 
    # We assume it is an uuid
    self.uuid_resource[:uuid => pack_uuid(object)].andtap {  |ur| ur.key }
  else
    super
  end
end

#model_name_for(model_class) ⇒ String

Compute the name (string) used to be saved in the Uuid table.

Parameters:

  • model_class (Class)

    class of the resource

Returns:

  • (String)


42
43
44
# File 'lib/lims-core/persistence/uuidable.rb', line 42

def model_name_for(model_class)
  persistor_name_for(model_class)
end

#new_uuid_resource_for(object) ⇒ Object



51
52
53
54
55
# File 'lib/lims-core/persistence/uuidable.rb', line 51

def new_uuid_resource_for(object)
  UuidResource.new(:state => state_for(object), :model_class => object.class).tap do |r|
    self << r
  end
end

#uuid_for(object) ⇒ Object

Finds the uuid of an object if it exists

Raises:

  • (RuntimeError)


63
64
65
66
67
68
69
# File 'lib/lims-core/persistence/uuidable.rb', line 63

def uuid_for(object)
  # We need to check if the object is managed and have alreday an id
  raise RuntimeError, "Unmanaged object, #{object.inspect}" unless managed?(object)
  state = state_for(object)
  state.uuid_resource.andtap { |ur| return ur.uuid }
  state.id && uuid_resource_for(state).andtap { |r|  r.uuid }
end

#uuid_for!(object) ⇒ Object

Find or create a uuid for an object



73
74
75
# File 'lib/lims-core/persistence/uuidable.rb', line 73

def uuid_for!(object)
  uuid_for(object) || new_uuid_resource_for(object).uuid
end

#uuid_resource_for(object) ⇒ Object



57
58
59
60
# File 'lib/lims-core/persistence/uuidable.rb', line 57

def uuid_resource_for(object)
  state, object = object.is_a?(ResourceState) ? [object, object.resource] : [state_for(object), object]
  self.uuid_resource[:key => state.id, :model_class => model_name_for(object.class)]
end