Class: Restforce::DB::AttributeMaps::Salesforce

Inherits:
Object
  • Object
show all
Defined in:
lib/restforce/db/attribute_maps/salesforce.rb

Overview

Restforce::DB::AttributeMaps::Database encapsulates the logic for compiling and parsing normalized attribute hashes from/for Salesforce objects.

Instance Method Summary collapse

Constructor Details

#initialize(fields) ⇒ Salesforce

Public: Initialize a Restforce::DB::AttributeMaps::Salesforce.

fields - A Hash of mappings between database columns and fields in

Salesforce.


16
17
18
# File 'lib/restforce/db/attribute_maps/salesforce.rb', line 16

def initialize(fields)
  @fields = fields
end

Instance Method Details

#attributes(record) ⇒ Object

Public: Build a normalized Hash of attributes from the appropriate set of mappings. The keys of the resulting mapping Hash will correspond to the Salesforce field names.

record - The underlying Salesforce object for which attributes should

be collected.

Returns a Hash.



28
29
30
31
32
33
34
# File 'lib/restforce/db/attribute_maps/salesforce.rb', line 28

def attributes(record)
  @fields.values.each_with_object({}) do |mapping, values|
    values[mapping] = mapping.split(".").inject(record) do |value, portion|
      value[portion]
    end
  end
end

#convert(attributes) ⇒ Object

Public: Convert a Hash of normalized attributes to a format suitable for consumption by an ActiveRecord object.

attributes - A Hash of attributes, with keys corresponding to the

normalized Salesforce attribute names.

Examples

attribute_map = AttributeMaps::Salesforce.new(
  some_key: "SomeField__c",
)

mapping.convert("Object__c", "Some_Field__c" => "some other value")
# => { "Some_Field__c" => "some other value" }

Returns a Hash.



52
53
54
# File 'lib/restforce/db/attribute_maps/salesforce.rb', line 52

def convert(attributes)
  attributes.dup
end