Class: Restforce::DB::AttributeMap
- Inherits:
-
Object
- Object
- Restforce::DB::AttributeMap
- Defined in:
- lib/restforce/db/attribute_map.rb
Overview
Restforce::DB::AttributeMap encapsulates the logic for converting between various representations of attribute hashes.
For the purposes of our mappings, a “normalized” attribute Hash maps the Salesforce field names to Salesforce-compatible values. Value conversion into and out of the database occurs through a lightweight Adapter object.
Instance Method Summary collapse
-
#attributes(from_format) ⇒ Object
Public: Build a normalized Hash of attributes from the appropriate set of mappings.
-
#convert(to_format, attributes) ⇒ Object
Public: Convert a Hash of normalized attributes to a format compatible with a specific platform.
-
#initialize(database_model, salesforce_model, fields = {}, adapter = Adapter.new) ⇒ AttributeMap
constructor
Public: Initialize a Restforce::DB::AttributeMap.
Constructor Details
#initialize(database_model, salesforce_model, fields = {}, adapter = Adapter.new) ⇒ AttributeMap
Public: Initialize a Restforce::DB::AttributeMap.
database_model - A Class compatible with ActiveRecord::Base. salesforce_model - A String name of an object type in Salesforce. fields - A Hash of mappings between database columns and
fields in Salesforce.
adapter - An adapter object which should be used to convert
between data formats.
21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/restforce/db/attribute_map.rb', line 21 def initialize(database_model, salesforce_model, fields = {}, adapter = Adapter.new) @database_model = database_model @salesforce_model = salesforce_model @fields = fields @adapter = adapter @types = { database_model => :database, salesforce_model => :salesforce, } end |
Instance Method Details
#attributes(from_format) ⇒ 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.
from_format - A String or Class reflecting the record type from which
the attribute Hash is being compiled.
Yields a series of attribute names. Returns a Hash.
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/restforce/db/attribute_map.rb', line 42 def attributes(from_format) case @types[from_format] when :salesforce @fields.values.each_with_object({}) do |mapping, values| values[mapping] = yield(mapping) end when :database attributes = @fields.keys.each_with_object({}) do |attribute, values| values[attribute] = yield(attribute) end attributes = @adapter.from_database(attributes) @fields.each_with_object({}) do |(attribute, mapping), final| final[mapping] = attributes[attribute] end else raise ArgumentError end end |
#convert(to_format, attributes) ⇒ Object
Public: Convert a Hash of normalized attributes to a format compatible with a specific platform.
to_format - A String or Class reflecting the record type for which the
attribute Hash is being compiled.
attributes - A Hash of attributes, with keys corresponding to the
normalized attribute names.
Examples
mapping = AttributeMap.new(
MyClass,
"Object__c",
some_key: "SomeField__c",
)
mapping.convert(MyClass, "Some_Field__c" => "some value")
# => { some_key: "some value" }
mapping.convert("Object__c", "Some_Field__c" => "some other value")
# => { "Some_Field__c" => "some other value" }
Returns a Hash.
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/restforce/db/attribute_map.rb', line 85 def convert(to_format, attributes) case @types[to_format] when :database attributes = @fields.each_with_object({}) do |(attribute, mapping), converted| next unless attributes.key?(mapping) converted[attribute] = attributes[mapping] end @adapter.to_database(attributes) when :salesforce attributes.dup else raise ArgumentError end end |