Class: Uuid

Inherits:
ApplicationRecord show all
Defined in:
app/models/uuid.rb

Overview

Stores the uuids of all out records, associated via a polymorphic association Allows the V1 API to find any record from just a uuid

Defined Under Namespace

Modules: Uuidable

Constant Summary collapse

ValidRegexp =
/\A[\da-f]{8}(-[\da-f]{4}){3}-[\da-f]{12}\z/

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ApplicationRecord

convert_labware_to_receptacle_for, find_by_id_or_name, find_by_id_or_name!

Methods included from Warren::BroadcastMessages

#broadcast, included, #queue_associated_for_broadcast, #queue_for_broadcast, #warren

Class Method Details

.filter_uncreated_uuids(resource_type, resource_ids) ⇒ Object

ids is a string of internal_ids


142
143
144
145
# File 'app/models/uuid.rb', line 142

def self.filter_uncreated_uuids(resource_type, resource_ids)
  existing_uuids = where(resource_type: resource_type, resource_id: resource_ids)
  resource_ids - existing_uuids.pluck(:resource_id)
end

.find_id(uuid, resource_type = nil) ⇒ String?

Find the id corresponding to the uuid. Check the resource and base_class names are as expected if they are given.

Parameters:

  • uuid (String)
  • resource_type (String) (defaults to: nil)

    the name of the external project

Returns:

  • (String, nil)

Raises:

  • Response::Exception if system doesn't macth.


158
159
160
# File 'app/models/uuid.rb', line 158

def self.find_id(uuid, resource_type = nil)
  with_external_id(uuid).limited_to_resource(resource_type).limit(1).pluck(:resource_id).first
end

.find_uuid(resource_type, resource_id) ⇒ String?

Find the uuid corresponding id and system.

Parameters:

  • resource_type (String)

    the name of the external project

  • resource_id (String, Integer)

Returns:

  • (String, nil)

    the uuid if found.


112
113
114
# File 'app/models/uuid.rb', line 112

def self.find_uuid(resource_type, resource_id)
  find_by(resource_type: resource_type, resource_id: resource_id).try(:external_id)
end

.find_uuid!(resource_type, resource_id) ⇒ String

Find an Uuid or create it if needed.

Parameters:

  • resource_type (String)

    the name of the external project

  • resource_id (String, Integer)

Returns:

  • (String)

    the uuid .


120
121
122
123
124
125
# File 'app/models/uuid.rb', line 120

def self.find_uuid!(resource_type, resource_id)
  return unless resource_id # return nil for nil

  find_uuid(resource_type, resource_id) ||
    create!(resource_type: resource_type, resource_id: resource_id).external_id
end

.find_uuid_instance!(resource_type, resource_id) ⇒ Object


103
104
105
# File 'app/models/uuid.rb', line 103

def self.find_uuid_instance!(resource_type, resource_id)
  find_by!(resource_type: resource_type, resource_id: resource_id)
end

.generate_all_uuids_for_class(base_class_name) ⇒ Object


147
148
149
150
151
# File 'app/models/uuid.rb', line 147

def self.generate_all_uuids_for_class(base_class_name)
  eval(base_class_name).find_in_batches(batch_size: 5000) do |group|
    generate_uuids!(base_class_name.to_s, group.map(&:id))
  end
end

.generate_uuidObject


89
90
91
# File 'app/models/uuid.rb', line 89

def self.generate_uuid
  UUIDTools::UUID.timestamp_create.to_s
end

.generate_uuids!(resource_type, resource_ids) ⇒ String

Given a list of internal ids, create uuids in bulk

Parameters:

  • resource_type (String)

    the name of the external project

  • resource_ids (String, Integer)

Returns:

  • (String)

    the uuid .


131
132
133
134
135
136
137
138
139
# File 'app/models/uuid.rb', line 131

def self.generate_uuids!(resource_type, resource_ids)
  return if resource_ids.empty?

  ids_missing_uuids = filter_uncreated_uuids(resource_type, resource_ids)
  uuids_to_create = ids_missing_uuids.map { |id| create!(resource_type: resource_type, resource_id: id, external_id: generate_uuid) }
  # Uuid.import uuids_to_create unless uuids_to_create.empty?

  nil
end

.lookup_many_uuids(uuids) ⇒ Object


168
169
170
171
172
173
# File 'app/models/uuid.rb', line 168

def lookup_many_uuids(uuids)
  with_external_id(uuids).all.tap do |found|
    missing = uuids - found.map(&:external_id)
    raise ActiveRecord::RecordNotFound, "Could not find UUIDs #{missing.map(&:inspect).join(',')}" unless missing.empty?
  end
end

.lookup_single_uuid(uuid) ⇒ Object


163
164
165
166
# File 'app/models/uuid.rb', line 163

def lookup_single_uuid(uuid)
  with_external_id(uuid).first or
    raise ActiveRecord::RecordNotFound, "Could not find UUID #{uuid.inspect}"
end

.translate_uuids_to_ids_in_params(params) ⇒ Object


93
94
95
96
97
# File 'app/models/uuid.rb', line 93

def self.translate_uuids_to_ids_in_params(params)
  params.transform_values! do |value|
    uuid?(value) ? find_id(value) : value
  end
end

.uuid?(value) ⇒ Boolean

Returns:

  • (Boolean)

99
100
101
# File 'app/models/uuid.rb', line 99

def self.uuid?(value)
  value.is_a?(String) && value.match?(ValidRegexp)
end

Instance Method Details

#objectObject

TODO: remove this and use resource everywhere!


69
70
71
# File 'app/models/uuid.rb', line 69

def object
  resource
end

#uuidObject


85
86
87
# File 'app/models/uuid.rb', line 85

def uuid
  external_id
end