Class: Rfm::Resultset

Inherits:
Object
  • Object
show all
Defined in:
lib/dm-filemaker-adapter/core_patches.rb

Instance Method Summary collapse

Instance Method Details

#mapObject

Does custom processing during each record-to-resource translation done in DataMapper::Model#load Doing this here means we don’t have to mess with DataMapper::Model#load.



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/dm-filemaker-adapter/core_patches.rb', line 146

def map
  super do |record|
    resource = yield(record)
    #puts "DM INPUT RECORD: #{record.class} #{record.instance_variable_get(:@record_id)}"

	if resource.kind_of?(DataMapper::Resource)
		#puts "MODEL#LOAD custom processing RECORD #{record.class} RESOURCE #{resource.class}"
		#puts record.inspect
      # For Testing:
      #resource.instance_variable_set(:@record, record)
      # WBR - Loads portal data into DM model attached to this resource.
      portals = record.instance_variable_get(:@portals)
      #puts "MODEL#LOAD record #{record.class} portals #{portals.keys rescue 'no portals'}"
      #if record.respond_to?(:portals) && record.portals.kind_of?(Hash) && record.portals.any?
      model = resource.class
      if portals.kind_of?(Hash) && portals.any?
        begin
        #puts record.portals.to_yaml
        portal_keys = portals.keys
        #puts "PORTALS: #{portal_keys}"
        portal_keys.each do |portal_key|
        	#relat = model.relationships.to_a.find{|r| storage_name = r.child_model.storage_names[:default]; portal_key.to_s == storage_name }
        	relat = model.relationships.to_a.find{|r| storage_name = r.child_model.storage_name; portal_key.to_s == storage_name }
        	if relat
         	#puts "BUILDING RELATIONSHIP FROM PORTAL: #{relat.name} #{relat.child_model.name}"
         	resource.instance_variable_set(relat.instance_variable_name, relat.child_model.load(record.instance_variable_get(:@portals)[portal_key], relat.child_model.query) )
        	end
        end
        rescue
          puts "ERROR LOADING PORTALS #{$!}"
        end
      end
		resource.instance_variable_set(:@_record_id, record.instance_variable_get(:@record_id))
		resource.instance_variable_set(:@_mod_id, record.instance_variable_get(:@mod_id))
	end

	resource
  end
end