Module: Heron::Factories::Concerns::Recipients

Included in:
TubeRack
Defined in:
app/models/heron/factories/concerns/recipients.rb

Overview

This module adds validation and tools to manage factories that act as content in a receptacle Eg: ::Heron::Factories::Sample

Requirements - The method or attribute recipient_factory needs to be defined with the class that act as factory for receptacles. Eg: ::Heron::Factories::Tube - The content configuration should be already stored in @params - The content configuration object needs to be in @params - The method or attribute recipients_key will need to identify the key where the config is stored. Eg: :tubes - All keys referring to coordinates in the content configuration are considered as validated, if that is not the case, that validation can be provided by including the module Heron::Factories::Concerns::RecipientsCoordinate - The class should include the module Heron::Factories:Concerns::CoordinatesSupport

Use Include the module in the class after checking the previous list of requirements.

Effect Factories for each recipient in each well will be generated. Any validation error from them will be aggregated in the base object.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



30
31
32
# File 'app/models/heron/factories/concerns/recipients.rb', line 30

def self.included(klass)
  klass.instance_eval { validate :check_recipients, if: :recipients }
end

Instance Method Details

#check_recipientsObject



44
45
46
47
48
49
50
51
52
53
54
# File 'app/models/heron/factories/concerns/recipients.rb', line 44

def check_recipients
  return if errors.count.positive?

  recipients.each_key do |coordinate|
    recipient = recipients[coordinate]

    next if recipient.valid?

    recipient.errors.each { |error| errors.add("Recipient at #{coordinate} #{error.attribute}", error.message) }
  end
end

#params_for_recipientObject



56
57
58
59
60
61
62
63
64
65
# File 'app/models/heron/factories/concerns/recipients.rb', line 56

def params_for_recipient
  return unless @params[recipients_key]

  @params_for_recipient ||=
    @params[recipients_key]
      .keys
      .each_with_object({}) do |location, obj|
        obj[unpad_coordinate(location)] = @params.dig(recipients_key, location).except(:content)
      end
end

#recipientsObject



34
35
36
37
38
39
40
41
42
# File 'app/models/heron/factories/concerns/recipients.rb', line 34

def recipients
  return unless @params[recipients_key]
  return if errors.count.positive?

  @recipients ||=
    params_for_recipient.keys.index_with do |coordinate|
      recipient_factory.new(params_for_recipient[coordinate])
    end
end