Class: CaTissue::SpecimenEventParameters

Inherits:
Object
  • Object
show all
Includes:
Resource
Defined in:
lib/catissue/migration/shims.rb,
lib/catissue/domain/specimen_event_parameters.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Resource

#database, included, #tolerant_match?

Methods included from Annotatable

#annotation_proxy, #create_proxy, #method_missing

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class CaTissue::Annotatable

Class Method Details

.create_parameters(type, scg_or_specimen, params = Hash::EMPTY_HASH) ⇒ Object

Creates a SpecimenEventParameters of the specified subclass type. The type is a SpecimenEventParameters subclass name without the EventParameters suffix, e.g. Collection. Lower-case, underscore symbols are supported and preferred, e.g. the :collection type creates a CollectionEventParameters.

The required scg_or_specimen argument is either a SpecimenCollectionGroup or a Specimen.

The optional params argument are attribute => value associations, e.g.

SpecimenEventParameters.create_parameters(:collection, scg, :user => collector, :timestamp => DateTime.now)


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/catissue/domain/specimen_event_parameters.rb', line 37

def self.create_parameters(type, scg_or_specimen, params=Hash::EMPTY_HASH)
  # make the class name by joining the camel-cased type prefix to the subclass suffix.
  # classify converts a lower_case, underscore type to a valid class name, e.g. :check_in_check_out
  # becomes CheckInCheckOut.
  class_name = type.to_s.classify + SUBCLASS_SUFFIX
  begin
    klass = CaTissue.const_get(class_name.to_sym)
  rescue
    raise ArgumentError.new("Unsupported event parameters type: #{type}; #{class_name} must be a subtype of #{self}")
  end
  event_params = klass.new(params)
  case scg_or_specimen
    when SpecimenCollectionGroup then event_params.specimen_collection_group = scg_or_specimen
    when Specimen then event_params.specimen = scg_or_specimen
    when nil then raise ArgumentError.new("Missing SpecimenEventParameters scg_or_specimen factory argument")
    else
      raise ArgumentError.new("Unsupported SpecimenEventParameters factory argument - expected SpecimenCollectionGroup or Specimen, found #{scg_or_specimen.class}")
  end
  event_params
end

Instance Method Details

#collection_protocolObject



72
73
74
# File 'lib/catissue/domain/specimen_event_parameters.rb', line 72

def collection_protocol
  specimen_collection_group.collection_protocol if specimen_collection_group
end

#migrate_specimen_collection_group(scg, row) ⇒ Object

Returns nil by default, since only SCGEventParameters have a SCG owner. CaTissue::SCGEventParameters#migrate_specimen_collection_group overrides this method.

Returns:

  • nil



83
84
85
# File 'lib/catissue/migration/shims.rb', line 83

def migrate_specimen_collection_group(scg, row)
  nil
end

#subjectObject

Returns the Specimen or SpecimenCollectionGroup to which this event is attached.



59
60
61
# File 'lib/catissue/domain/specimen_event_parameters.rb', line 59

def subject
  specimen.nil? ? specimen_collection_group : specimen
end

#subject=(scg_or_specimen) ⇒ Object

Sets the scg_or_specimen subject to which this event is attached.



64
65
66
67
68
69
70
# File 'lib/catissue/domain/specimen_event_parameters.rb', line 64

def subject=(scg_or_specimen)
  spc_subject = scg_or_specimen if Specimen === scg_or_specimen
  scg_subject = scg_or_specimen if SpecimenCollectionGroup === scg_or_specimen
  specimen = spc_subject
  specimen_collection_group = scg_subject
  scg_or_specimen
end

#validateObject



76
77
78
79
80
81
# File 'lib/catissue/domain/specimen_event_parameters.rb', line 76

def validate
  super
  if subject.nil? then
    raise ValidationError.new("Both specimen_collection_group and specimen are missing in SpecimenEventParameters #{self}")
  end
end