Class: Restforce::DB::FieldProcessor

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

Overview

Restforce::DB::FieldProcessor encapsulates logic for preventing information for unwriteable fields from being submitted to Salesforce.

Constant Summary collapse

RELATIONSHIP_MATCHER =

This token indicates that a relationship is being accessed for a specific field.

/(.+)__r\./.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.fetch(sobject_type) ⇒ Object

Public: Get a collection of all fields for the passed Salesforce Object Type, with an indication of whether or not they are readable and writable for both create and update actions.

sobject_type - A String name of an Object Type in Salesforce.

Returns a Hash.



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

def fetch(sobject_type)
  field_cache[sobject_type] ||= begin
    fields = DB.client.describe(sobject_type).fields

    fields.each_with_object({}) do |field, permissions|
      permissions[field["name"]] = {
        read:   true,
        create: field["createable"],
        update: field["updateable"],
      }
    end
  end
end

.field_cacheObject

Public: Get a global cache with which to store/fetch the field metadata for each Salesforce Object Type.

Returns a Hash.



29
30
31
# File 'lib/restforce/db/field_processor.rb', line 29

def field_cache
  @field_cache ||= {}
end

.preloadObject

Public: Fetch the field metadata for all Salesforce models registered through mappings in the system. Useful to ensure that forked worker processes have access to all of the field metadata without the need for additional querying.

Returns nothing.



21
22
23
# File 'lib/restforce/db/field_processor.rb', line 21

def preload
  Registry.each { |mapping| fetch(mapping.salesforce_model) }
end

.resetObject

Public: Clear out the global field cache.

Returns nothing.



57
58
59
# File 'lib/restforce/db/field_processor.rb', line 57

def reset
  @field_cache = {}
end

Instance Method Details

#available_fields(sobject_type, fields, action = :read) ⇒ Object

Public: Get a list of valid fields for a specific action from the passed list of proposed fields. Allows access to related object fields on a read-only basis.

sobject_type - A String name of an SObject Type in Salesforce. attributes - A Hash with keys corresponding to Salesforce field names. action - A Symbol reflecting the action to perform. Accepted

values are :read, :create, and :update.

Returns a Hash.



73
74
75
76
77
78
79
80
# File 'lib/restforce/db/field_processor.rb', line 73

def available_fields(sobject_type, fields, action = :read)
  fields.select do |field|
    known_field = available?(sobject_type, field, action)
    relationship = action == :read && relationship?(field)

    known_field || relationship
  end
end

#process(sobject_type, attributes, action) ⇒ Object

Public: Get a restricted version of the passed attributes Hash, with inaccessible fields for the specified action stripped out.

sobject_type - A String name of an SObject Type in Salesforce. attributes - A Hash with keys corresponding to Salesforce field names. action - A Symbol reflecting the action to perform. Accepted

values are :create and :update.

Returns a Hash.



91
92
93
# File 'lib/restforce/db/field_processor.rb', line 91

def process(sobject_type, attributes, action)
  attributes.select { |field, _| available?(sobject_type, field, action) }
end