module Mexico::FileSystem::IdRef
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.
Public Instance Methods
id_ref(field_name)
click to toggle source
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.
# 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