Class: Restforce::DB::Mapping
- Inherits:
-
Object
- Object
- Restforce::DB::Mapping
- 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
-
#adapter ⇒ Object
Returns the value of attribute adapter.
-
#associations ⇒ Object
Returns the value of attribute associations.
-
#conditions ⇒ Object
Returns the value of attribute conditions.
-
#database_model ⇒ Object
readonly
Returns the value of attribute database_model.
-
#database_record_type ⇒ Object
readonly
Returns the value of attribute database_record_type.
-
#fields ⇒ Object
Returns the value of attribute fields.
-
#salesforce_model ⇒ Object
readonly
Returns the value of attribute salesforce_model.
Instance Method Summary collapse
-
#database_fields ⇒ Object
Public: Get a list of the relevant database column names for this mapping.
-
#initialize(database_model, salesforce_model, strategy = Strategies::Always.new) ⇒ Mapping
constructor
Public: Initialize a new Restforce::DB::Mapping.
-
#lookup_column ⇒ Object
Public: Get the name of the database column which should be used to store the Salesforce lookup ID.
-
#salesforce_fields ⇒ Object
Public: Get a list of the relevant Salesforce field names for this mapping.
-
#unscoped ⇒ Object
Public: Access the Mapping object without any conditions on the fetched records.
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.
39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/restforce/db/mapping.rb', line 39 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.adapter = Adapter.new self.fields = {} self.associations = [] self.conditions = [] self.strategy = strategy end |
Instance Attribute Details
#adapter ⇒ Object
Returns the value of attribute adapter.
26 27 28 |
# File 'lib/restforce/db/mapping.rb', line 26 def adapter @adapter end |
#associations ⇒ Object
Returns the value of attribute associations.
26 27 28 |
# File 'lib/restforce/db/mapping.rb', line 26 def associations @associations end |
#conditions ⇒ Object
Returns the value of attribute conditions.
26 27 28 |
# File 'lib/restforce/db/mapping.rb', line 26 def conditions @conditions end |
#database_model ⇒ Object (readonly)
Returns the value of attribute database_model.
19 20 21 |
# File 'lib/restforce/db/mapping.rb', line 19 def database_model @database_model end |
#database_record_type ⇒ Object (readonly)
Returns the value of attribute database_record_type.
19 20 21 |
# File 'lib/restforce/db/mapping.rb', line 19 def database_record_type @database_record_type end |
#fields ⇒ Object
Returns the value of attribute fields.
26 27 28 |
# File 'lib/restforce/db/mapping.rb', line 26 def fields @fields end |
#salesforce_model ⇒ Object (readonly)
Returns the value of attribute salesforce_model.
19 20 21 |
# File 'lib/restforce/db/mapping.rb', line 19 def salesforce_model @salesforce_model end |
Instance Method Details
#database_fields ⇒ Object
Public: Get a list of the relevant database column names for this mapping.
Returns an Array.
65 66 67 |
# File 'lib/restforce/db/mapping.rb', line 65 def database_fields fields.keys end |
#lookup_column ⇒ Object
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.
74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/restforce/db/mapping.rb', line 74 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_fields ⇒ Object
Public: Get a list of the relevant Salesforce field names for this mapping.
Returns an Array.
57 58 59 |
# File 'lib/restforce/db/mapping.rb', line 57 def salesforce_fields fields.values + associations.map(&:fields).flatten end |
#unscoped ⇒ Object
Public: Access the Mapping object without any conditions on the fetched records. Allows for a comparison of all modified records to only those modified records that still fit the ‘where` criteria.
block - A block of code to execute in a condition-less context.
Yields the Mapping with its conditions removed. Returns the result of the block.
97 98 99 100 101 102 103 |
# File 'lib/restforce/db/mapping.rb', line 97 def unscoped criteria = @conditions @conditions = [] yield self ensure @conditions = criteria end |