Class: Restforce::DB::Mapping

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/restforce/db/mapping.rb

Overview

Restforce::DB::Mapping captures a set of mappings between database columns and Salesforce fields, providing utilities to transform hashes of attributes from one to the other.

Defined Under Namespace

Classes: InvalidMappingError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(database_model, salesforce_model, strategy = Strategies::Always.new) ⇒ Mapping

Public: Initialize a new Restforce::DB::Mapping.

database_model - A Class compatible with ActiveRecord::Base. salesforce_model - A String name of an object type in Salesforce. strategy - A synchronization Strategy object.



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/restforce/db/mapping.rb', line 40

def initialize(database_model, salesforce_model, strategy = Strategies::Always.new)
  @database_model = database_model
  @salesforce_model = salesforce_model

  @database_record_type = RecordTypes::ActiveRecord.new(database_model, self)
  @salesforce_record_type = RecordTypes::Salesforce.new(salesforce_model, self)

  self.fields = {}
  self.conversions = {}
  self.associations = []
  self.conditions = []
  self.strategy = strategy
end

Instance Attribute Details

#associationsObject

Returns the value of attribute associations.



27
28
29
# File 'lib/restforce/db/mapping.rb', line 27

def associations
  @associations
end

#conditionsObject

Returns the value of attribute conditions.



27
28
29
# File 'lib/restforce/db/mapping.rb', line 27

def conditions
  @conditions
end

#conversionsObject

Returns the value of attribute conversions.



27
28
29
# File 'lib/restforce/db/mapping.rb', line 27

def conversions
  @conversions
end

#database_modelObject (readonly)

Returns the value of attribute database_model.



20
21
22
# File 'lib/restforce/db/mapping.rb', line 20

def database_model
  @database_model
end

#database_record_typeObject (readonly)

Returns the value of attribute database_record_type.



20
21
22
# File 'lib/restforce/db/mapping.rb', line 20

def database_record_type
  @database_record_type
end

#fieldsObject

Returns the value of attribute fields.



27
28
29
# File 'lib/restforce/db/mapping.rb', line 27

def fields
  @fields
end

#salesforce_modelObject (readonly)

Returns the value of attribute salesforce_model.



20
21
22
# File 'lib/restforce/db/mapping.rb', line 20

def salesforce_model
  @salesforce_model
end

Instance Method Details

#database_fieldsObject

Public: Get a list of the relevant database column names for this mapping.

Returns an Array.



66
67
68
# File 'lib/restforce/db/mapping.rb', line 66

def database_fields
  fields.keys
end

#lookup_columnObject

Public: Get the name of the database column which should be used to store the Salesforce lookup ID.

Raises an InvalidMappingError if no database column exists. Returns a Symbol.



75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/restforce/db/mapping.rb', line 75

def lookup_column
  @lookup_column ||= begin
    column_prefix = salesforce_model.underscore.chomp("__c")
    column = :"#{column_prefix}_salesforce_id"

    if database_record_type.column?(column)
      column
    elsif database_record_type.column?(:salesforce_id)
      :salesforce_id
    else
      raise InvalidMappingError, "#{database_model} must define a Salesforce ID column"
    end
  end
end

#salesforce_fieldsObject

Public: Get a list of the relevant Salesforce field names for this mapping.

Returns an Array.



58
59
60
# File 'lib/restforce/db/mapping.rb', line 58

def salesforce_fields
  fields.values + associations.map(&:fields).flatten
end