Class: Heron::Factories::Sample

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Model
Defined in:
app/models/heron/factories/sample.rb

Overview

Factory class to generate sample tubes inside a Heron rack

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Sample

Returns a new instance of Sample.



13
14
15
# File 'app/models/heron/factories/sample.rb', line 13

def initialize(params)
  @params = params
end

Instance Method Details

#all_fields_are_existing_columnsObject



97
98
99
100
101
# File 'app/models/heron/factories/sample.rb', line 97

def all_fields_are_existing_columns
  return if unexisting_column_keys.empty?

  unexisting_column_keys.each { |col| errors.add(col, 'Unexisting field for sample or sample_metadata') }
end

#check_no_other_params_when_uuidObject



17
18
19
20
21
# File 'app/models/heron/factories/sample.rb', line 17

def check_no_other_params_when_uuid
  return if sample_keys.empty?

  sample_keys.each { |key| errors.add(key, 'No other params can be added when sample uuid specified') }
end

#createObject

Persists the material including the associated container



29
30
31
32
33
34
# File 'app/models/heron/factories/sample.rb', line 29

def create
  return unless valid?
  return @sample if @sample

  @sample = create_sample!
end

#create_aliquot_at(container) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'app/models/heron/factories/sample.rb', line 36

def create_aliquot_at(container)
  return unless create

  new_aliquot = container&.aliquots&.create(params_for_aliquot_creation)

  container.register_stock! if new_aliquot&.persisted?

  new_aliquot
end

#create_sample!Object



68
69
70
71
72
73
74
75
76
77
# File 'app/models/heron/factories/sample.rb', line 68

def create_sample!
  return sample if sample

  @sample =
    ::Sample.create!(params_for_sample_creation) do |sample|
      replace_uuid(sample) if @params[:uuid]
      sample..update!()
      sample.studies << study
    end
end

#create_sanger_sample_id!Object



50
51
52
# File 'app/models/heron/factories/sample.rb', line 50

def create_sanger_sample_id!
  SangerSampleId.generate_sanger_sample_id!(study.abbreviation)
end

#handle_uuid_duplication(uuid) ⇒ Object

Raises:

  • (StandardError)


87
88
89
90
91
# File 'app/models/heron/factories/sample.rb', line 87

def handle_uuid_duplication(uuid)
  msg = "Sample with uuid #{uuid} already exists"
  errors.add(:uuid, msg)
  raise StandardError, msg
end

#params_for_aliquot_creationObject



107
108
109
# File 'app/models/heron/factories/sample.rb', line 107

def params_for_aliquot_creation
  { sample:, study: }.merge(@params[:aliquot] || {})
end

#params_for_sample_creationObject



103
104
105
# File 'app/models/heron/factories/sample.rb', line 103

def params_for_sample_creation
  { name: sanger_sample_id, sanger_sample_id: sanger_sample_id }.merge(params_for_sample_table)
end

#params_for_sample_metadata_tableObject



115
116
117
# File 'app/models/heron/factories/sample.rb', line 115

def 
  @params.select { |k, _v| ::Sample::Metadata.column_names.include?(k.to_s) }
end

#params_for_sample_tableObject



111
112
113
# File 'app/models/heron/factories/sample.rb', line 111

def params_for_sample_table
  @params.select { |k, _v| ::Sample.column_names.include?(k.to_s) }
end

#replace_uuid(sample) ⇒ Object



79
80
81
82
83
84
85
# File 'app/models/heron/factories/sample.rb', line 79

def replace_uuid(sample)
  uuid = @params[:uuid]
  handle_uuid_duplication(uuid) if Uuid.with_external_id(uuid).count.positive?
  sample.lazy_uuid_generation = true
  sample.uuid_object = Uuid.new
  sample.uuid_object.update!(resource: sample, external_id: uuid) if uuid
end

#sampleObject



58
59
60
61
62
# File 'app/models/heron/factories/sample.rb', line 58

def sample
  return @sample if @sample

  @sample = ::Sample.with_uuid(@params[:sample_uuid]).first if @params[:sample_uuid]
end

#sample_already_present?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'app/models/heron/factories/sample.rb', line 64

def sample_already_present?
  sample.present?
end

#sample_keysObject



23
24
25
# File 'app/models/heron/factories/sample.rb', line 23

def sample_keys
  (@params.keys.map(&:to_sym) - %i[sample_uuid study study_uuid aliquot uuid])
end

#sanger_sample_idObject



54
55
56
# File 'app/models/heron/factories/sample.rb', line 54

def sanger_sample_id
  @sanger_sample_id ||= @params[:sanger_sample_id] || create_sanger_sample_id!
end

#studyObject



46
47
48
# File 'app/models/heron/factories/sample.rb', line 46

def study
  @study ||= @params[:study] || Study.with_uuid(@params[:study_uuid]).first
end

#unexisting_column_keysObject



93
94
95
# File 'app/models/heron/factories/sample.rb', line 93

def unexisting_column_keys
  (sample_keys - [params_for_sample_table.keys, .keys].flatten.map(&:to_sym))
end