Class: ActiveSalesforce::IdResolver

Inherits:
Object
  • Object
show all
Defined in:
lib/active_record/connection_adapters/id_resolver.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection) ⇒ IdResolver

Returns a new instance of IdResolver.



25
26
27
28
# File 'lib/active_record/connection_adapters/id_resolver.rb', line 25

def initialize(connection)
  @connection = connection
  @object_type_to_ids = {}
end

Instance Attribute Details

#object_type_to_idsObject (readonly)

Returns the value of attribute object_type_to_ids.



23
24
25
# File 'lib/active_record/connection_adapters/id_resolver.rb', line 23

def object_type_to_ids
  @object_type_to_ids
end

Instance Method Details

#add(record, columns = nil) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/active_record/connection_adapters/id_resolver.rb', line 31

def add(record, columns = nil)
  if columns
    columns = columns.to_a.map { |column_name| record.column_for_attribute(column_name) }
  else
    columns = record.class.columns
  end
  
  columns.each do |column|
    reference_to = column.reference_to
    next unless reference_to
    
    value = record.send(column.name)
    if value
      ids = @object_type_to_ids[reference_to] ||= Set.new
      ids << value
    end
  end
end

#deserialize(source) ⇒ Object



78
79
80
# File 'lib/active_record/connection_adapters/id_resolver.rb', line 78

def deserialize(source)
  @object_type_to_ids = YAML.load(source)
end

#resolveObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/active_record/connection_adapters/id_resolver.rb', line 51

def resolve
  result = {}
  
  @object_type_to_ids.each do |object_type, ids|
    entity_def = @connection.get_entity_def(object_type)
    
    fields = (entity_def.columns.reject { |column| not column.is_name? }).map { |column| column.api_name }
    
    # DCHASMAN TODO Boxcar into requests of no more than 200 retrieves per request
    puts "Resolving references to #{object_type}"
    field_values = @connection.retrieve_field_values(object_type, fields, ids.to_a, "#{self}.resolve()") 
    
    field_values.each do |field_value|
      id = field_value.delete(:Id)
      result[id] = field_value
    end
  end
  
  result
end

#serializeObject



73
74
75
# File 'lib/active_record/connection_adapters/id_resolver.rb', line 73

def serialize
  YAML.dump @object_type_to_ids
end