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.



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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/dm-filemaker-adapter/core_patches.rb', line 160

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
      return unless model.kind_of?(DataMapper::Model)
      #puts "MODEL#LOAD resource class: #{model}"
      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}"
          	resources_from_portal = relat.child_model.load(record.instance_variable_get(:@portals)[portal_key], relat.child_model.query)
          	resource.instance_variable_set(relat.instance_variable_name, resources_from_portal)
         	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