Module: Mexico::FileSystem::IdRef

Included in:
DesignComponent, LocalFile, Participant, ParticipantRole, Resource, Trial, URL
Defined in:
lib/mexico/file_system/id_ref.rb

Overview

The IdRef module provides a simple mechanism to map IDREF attributes in an XML document to Ruby objects that are stored elsewhere in the object tree. They are retrieved and accessed via the central corpus object.

Instance Method Summary collapse

Instance Method Details

#id_ref(field_name) ⇒ Object

Defines a foreign key reference for an object. By default, it will refer to the corresponding collection in the top-level corpus, and it will retrieve the object via its identifier field.


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/mexico/file_system/id_ref.rb', line 28

def id_ref(field_name)
  
  pluralized_field_name = "#{field_name}s"
  field_name_id = "#{field_name}_id"
  
  define_method field_name do
    inst_var = instance_variable_get("@#{field_name}")
    inst_var = instance_variable_set("@#{field_name}", @corpus.send(pluralized_field_name).find{|x| x.identifier==instance_variable_get("@#{field_name_id}") }) if inst_var.nil?
    return inst_var
  end
  
  define_method "#{field_name}=" do |param|
    instance_variable_set("@#{field_name_id}", param.identifier)
    instance_variable_set("@#{field_name}", param)
  end
  
end