Class: Study::PolyMetadataHandler

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Validations
Defined in:
app/models/study/poly_metadata_handler.rb

Overview

Handles the processing of polymorphic metadata for a study.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(study) ⇒ void

Initializes a new instance of the PolyMetadataHandler class. The studies controller creates an instance of this class in the create and update actions and passes the study instance.

Parameters:

  • study (Study)

    The study to handle polymorphic metadata for.



28
29
30
# File 'app/models/study/poly_metadata_handler.rb', line 28

def initialize(study)
  @study = study
end

Instance Attribute Details

#scrna_core_pbmc_donor_pooling_required_number_of_cellsInteger

Returns The number of cells required for PBMC donor pooling.

Returns:

  • (Integer)

    The number of cells required for PBMC donor pooling.



12
13
14
# File 'app/models/study/poly_metadata_handler.rb', line 12

def scrna_core_pbmc_donor_pooling_required_number_of_cells
  @scrna_core_pbmc_donor_pooling_required_number_of_cells
end

Instance Method Details

#assign_attributes(params) ⇒ void

This method returns an undefined value.

Assigns the given parameters to attributes if they are defined.

Parameters:

  • params (Hash)

    The parameters to assign.



52
53
54
# File 'app/models/study/poly_metadata_handler.rb', line 52

def assign_attributes(params)
  params.each { |key, value| send(:"#{key}=", value) if self.class.method_defined?(key) }
end

#dispatch(params) ⇒ void

This method returns an undefined value.

Dispatches the given parameters by calling a handler method for each one if it exists. The convention for the handler methods is to prefix the key with ‘handle_’. For example, if the key is ‘scrna_core_pbmc_donor_pooling_required_number_of_cells’, the handler method would be ‘handle_scrna_core_pbmc_donor_pooling_required_number_of_cells’.

:reek:ManualDispatch

Parameters:

  • params (Hash)

    The parameters to dispatch.



79
80
81
82
83
84
# File 'app/models/study/poly_metadata_handler.rb', line 79

def dispatch(params)
  params.each do |key, value|
    method = "handle_#{key}"
    send(method, key, value) if respond_to?(method)
  end
end

#handle_scrna_core_pbmc_donor_pooling_required_number_of_cells(key, value) ⇒ void

This method returns an undefined value.

Handles ‘scrna_core_pbmc_donor_pooling_required_number_of_cells’ parameter. A blank value defaults to Limber’s configuration. Limber will warn but allow proceeding with the default value for the study. If a matching PolyMetadatum exists with the same value as the parameter, the method exits early to avoid redundant updates. Otherwise, a new PolyMetadatum is created or updated with the new value, followed by a save operation.

Parameters:

  • value (String)

    The value of the scrna_core_pbmc_donor_pooling_required_number_of_cells parameter.



96
97
98
99
100
101
102
103
104
105
106
# File 'app/models/study/poly_metadata_handler.rb', line 96

def handle_scrna_core_pbmc_donor_pooling_required_number_of_cells(key, value)
  poly_metadatum = @study.poly_metadatum_by_key(key)

  if value.blank?
    poly_metadatum&.destroy!
  elsif poly_metadatum&.value != value
    poly_metadatum ||= PolyMetadatum.new(key: key, metadatable: @study)
    poly_metadatum.value = value
    poly_metadatum.save!
  end
end

#process(params) ⇒ void

This method returns an undefined value.

Processes the provided poly_metadta parameters. This involves three steps: 1. Assigning the parameters to corresponding attributes. 2. Validating the assigned attributes. 3. Dispatching the parameters to their specific handler methods. The parameters passed from the controller are solely poly_metadata keys and values. This is because they are nested under the poly_metadata key in the form fields, which allows for straightforward iterations within this class.

Parameters:

  • params (Hash)

    The poly_metadata parameters to process.



42
43
44
45
46
# File 'app/models/study/poly_metadata_handler.rb', line 42

def process(params)
  assign_attributes(params)
  validate_attributes
  dispatch(params)
end

#validate_attributesvoid

This method returns an undefined value.

Validates the assigned attributes. If any attributes are invalid, their errors are added to the study’s errors, and an ActiveRecord::RecordInvalid exception is raised with the study as its record. Adding errors to the study is important to render messages in the UI. Raising the specific exception is important to rollback the active transaction.

Raises:



64
65
66
67
68
# File 'app/models/study/poly_metadata_handler.rb', line 64

def validate_attributes
  return if valid?
  errors.each { |error| @study.errors.add(error.attribute, error.message) }
  raise ActiveRecord::RecordInvalid, @study
end