Class: Study::PolyMetadataHandler
- Inherits:
-
Object
- Object
- Study::PolyMetadataHandler
- 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
-
#scrna_core_pbmc_donor_pooling_required_number_of_cells ⇒ Integer
The number of cells required for PBMC donor pooling.
Instance Method Summary collapse
-
#assign_attributes(params) ⇒ void
Assigns the given parameters to attributes if they are defined.
-
#dispatch(params) ⇒ void
Dispatches the given parameters by calling a handler method for each one if it exists.
-
#handle_scrna_core_pbmc_donor_pooling_required_number_of_cells(key, value) ⇒ void
Handles ‘scrna_core_pbmc_donor_pooling_required_number_of_cells’ parameter.
-
#initialize(study) ⇒ void
constructor
Initializes a new instance of the PolyMetadataHandler class.
-
#process(params) ⇒ void
Processes the provided poly_metadta parameters.
-
#validate_attributes ⇒ void
Validates the assigned attributes.
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.
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_cells ⇒ Integer
Returns 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.
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
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.
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) = @study.(key) if value.blank? &.destroy! elsif &.value != value ||= PolyMetadatum.new(key: key, metadatable: @study) .value = value .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.
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_attributes ⇒ void
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.
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.) } raise ActiveRecord::RecordInvalid, @study end |