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

alias_association, convert_labware_to_receptacle_for, find_by_id_or_name, find_by_id_or_name!

Methods included from Squishify

extended

Class Method Details

.filter_uncreated_uuids(resource_type, resource_ids) ⇒ Object

ids is a string of internal_ids



144
145
146
147
# File 'app/models/uuid.rb', line 144

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.



160
161
162
# File 'app/models/uuid.rb', line 160

def self.find_id(uuid, resource_type = nil)
  with_external_id(uuid).limited_to_resource(resource_type).pick(:resource_id)
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.



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

def self.find_uuid(resource_type, resource_id)
  find_by(resource_type:, 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 .



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

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_id:).external_id
end

.find_uuid_instance!(resource_type, resource_id) ⇒ Object



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

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

.generate_all_uuids_for_class(base_class_name) ⇒ Object



149
150
151
152
153
# File 'app/models/uuid.rb', line 149

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



92
93
94
# File 'app/models/uuid.rb', line 92

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
140
141
# 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



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

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

.lookup_single_uuid(uuid) ⇒ Object



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

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



96
97
98
# File 'app/models/uuid.rb', line 96

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

.uuid?(value) ⇒ Boolean

Returns:

  • (Boolean)


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

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

Instance Method Details

#objectObject

TODO: remove this and use resource everywhere!



71
72
73
# File 'app/models/uuid.rb', line 71

def object
  resource
end

#uuidObject



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

def uuid
  external_id
end