Class: Pipeline::BSI::Models::Specimen

Inherits:
Model::Base
  • Object
show all
Defined in:
lib/bsi-pipeline/models.rb

Constant Summary collapse

REQUIRED_FIELDS =
%w(study_id subject_id specimen_type date_drawn date_received label_status billing_method thaws).map{|v| v.to_sym}
BSI_CORE_FIELDS =
{
  # Sample level fields
  :subject_id             => 'sample.subject_id',
  :sample_id              => 'sample.sample_id',
  :date_drawn             => 'sample.date_drawn',
  :kit_id                 => 'sample.kit_id',
  :pickup_location        => 'sample.pickup_location',
  :sample_modifiers       => 'sample.sample_modifiers',
  # Vial fields
  :study_id               => 'vial.study_id',
  :repos_id               => 'vial.repos_id',
  :sequence               => 'vial.seq_num',
  :current_label          => 'vial.current_label',
  :specimen_type          => 'vial.mat_type',
  :parent_id              => 'vial.parent_id',
  :measurement            => 'vial.volume',
  :vial_status            => 'vial.vial_status',
  # Location Fields
  :room                   => 'location.room',
  :building               => 'location.building',
  :freezer                => 'location.freezer',
  :shelf                  => 'location.shelf',
  :rack                   => 'location.rack',
  :box                    => 'location.box',
  # Vial-Location fields
  :row                    => 'vial_location.row',
  :col                    => 'vial_location.col'
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Specimen

Define Defaults


41
42
43
44
45
46
47
48
# File 'lib/bsi-pipeline/models.rb', line 41

def initialize(options={})
  @bfh_map = Hash.new
  add_attributes(REQUIRED_FIELDS)
  add_attributes(options[:custom_required_fields])  if options[:custom_required_fields]
  add_attributes(BSI_CORE_FIELDS)
  add_attributes(options[:custom_fields])           if options[:custom_fields]
  self.thaws = '0'
end

Instance Attribute Details

#bfh_mapObject

Returns the value of attribute bfh_map.


8
9
10
# File 'lib/bsi-pipeline/models.rb', line 8

def bfh_map
  @bfh_map
end

#seminal_parentObject

Returns the value of attribute seminal_parent.


8
9
10
# File 'lib/bsi-pipeline/models.rb', line 8

def seminal_parent
  @seminal_parent
end

Instance Method Details

#add_attributes(attributes) ⇒ Object


50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/bsi-pipeline/models.rb', line 50

def add_attributes(attributes)
  new_attributes = Hash.new
  case attributes.class.to_s
  when Hash.to_s
    attributes.each do |k,v|
      new_attributes[k.to_sym] = v.to_s
      self.bfh_map[k.to_sym] = v.to_s
    end
  when Array.to_s
    attributes.each do |elem|
      new_attributes[elem.to_sym] = "vial.#{elem}"
      self.bfh_map[elem.to_sym] = "vial.#{elem}"
    end
  else
    raise 'Please pass either an Array or Hash of attributes'
  end
  new_attributes.keys.each do |attr|
    self.class.send(:attr_accessor, attr) unless self.respond_to? attr
  end
  self
end

#bsi_idObject


83
84
85
# File 'lib/bsi-pipeline/models.rb', line 83

def bsi_id()
  "#{self.sample_id} #{self.seq_num}"
end

#build(bfh = {}) ⇒ Object


72
73
74
75
76
77
78
79
80
81
# File 'lib/bsi-pipeline/models.rb', line 72

def build(bfh={})
  self.seminal_parent = true
  bfh.keys.each do |bfh_key|
    if BFH_MAP.has_value?(bfh_key)
      instance_eval("self.#{BFH_MAP.key(bfh_key)} = '#{bfh[bfh_key]}'")
    else
      instance_eval("self.#{bfh_key.gsub(/vial\./, '')} = '#{bfh[bfh_key]}'")
    end
  end
end

#missing_attrsObject


108
109
110
# File 'lib/bsi-pipeline/models.rb', line 108

def missing_attrs
  REQUIRED_FIELDS.find_all{|a| send(a).nil?}
end

#seminal_parent?Boolean

Returns:

  • (Boolean)

87
88
89
# File 'lib/bsi-pipeline/models.rb', line 87

def seminal_parent?
  return seminal_parent
end

#to_bfhObject


91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/bsi-pipeline/models.rb', line 91

def to_bfh
  bfh = Hash.new
  # Add 1-1 matches/translations
  formatted_attributes.each do |k,v|
    if bfh_map.has_key?(k)
      bfh[bfh_map[k]] = v
    else
      bfh["vial.#{k}"] = v
    end
  end
  bfh
end

#valid?Boolean

Returns:

  • (Boolean)

104
105
106
# File 'lib/bsi-pipeline/models.rb', line 104

def valid?
  incomplete_attrs = REQUIRED_FIELDS.find{|v| send(v).nil?}.nil?
end