Module: Mexico::FileSystem::StaticCollectionRef

Included in:
DesignComponent, LocalFile, Resource, URL
Defined in:
lib/mexico/file_system/static_collection_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

#collection_ref(field_identifier, field_class, field_collection, default) ⇒ 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/mexico/file_system/static_collection_ref.rb', line 28

def collection_ref(field_identifier, field_class, field_collection, default)
  
  field_name = field_identifier.to_s
  pluralized_field_name = "#{field_name}s"
  field_name_id = "#{field_name}_id"
  
  define_method field_name do
    if instance_variable_get("@#{field_name}").nil?
      self.send("#{field_name}=", instance_variable_get("@#{field_name_id}"))
      # self.instance_variable_set("@#{field_name}", instance_variable_get("@#{field_name_id}"))
    end
    return instance_variable_get("@#{field_name}")
  end
  
  define_method "#{field_name}=" do |param|
    #instance_variable_set("@#{field_name_id}", param.identifier)
    #instance_variable_set("@#{field_name}", param)

    if param.nil?
      instance_variable_set "@#{field_name}", default # Mexico::Constants::MediaTypes::OTHER
    elsif param.kind_of?(String)
      #@media_type = Mexico::Constants::MediaTypes::ALL.find{|m| m.identifier==new_media_type.to_s}
      instance_variable_set "@#{field_name}", field_collection.find{|i| i.identifier==param.to_s}
      if instance_variable_get("@#{field_name}").blank?
        instance_variable_set "@#{field_name}", default
      end
    elsif param.kind_of?(field_class)
      instance_variable_set "@#{field_name}", param
    end
    @media_type_id = @media_type.identifier
    instance_variable_set "@#{field_name_id}", instance_variable_get("@#{field_name}").identifier
    return instance_variable_get("@#{field_name}")
  end
  
end