Class: Restforce::DB::DSL

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

Overview

Restforce::DB::DSL defines a syntax through which a Mapping may be configured between a database model and an object type in Salesforce.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(database_model, salesforce_model, strategy_name, options = {}) ⇒ DSL

Public: Initialize a Restforce::DB::DSL.

database_model - An ActiveRecord::Base subclass. salesforce_model - A String Salesforce object name. strategy_name - A Symbol initialization strategy name. options - A Hash of options to pass to the Strategy object.

Returns nothing.



19
20
21
22
23
# File 'lib/restforce/db/dsl.rb', line 19

def initialize(database_model, salesforce_model, strategy_name, options = {})
  strategy = Strategy.for(strategy_name, options)
  @mapping = Mapping.new(database_model, salesforce_model, strategy)
  Registry << @mapping
end

Instance Attribute Details

#mappingObject (readonly)

Returns the value of attribute mapping.



9
10
11
# File 'lib/restforce/db/dsl.rb', line 9

def mapping
  @mapping
end

Instance Method Details

#belongs_to(association, through:, build: true) ⇒ Object

Public: Define a relationship in which the current mapping contains the lookup ID for another mapping.

association - The name of the ActiveRecord association. through - A String or Array of Strings representing the Lookup IDs. build - A Boolean indicating whether or not the association chain

should be built out for new records.

Returns nothing.



44
45
46
47
48
49
50
# File 'lib/restforce/db/dsl.rb', line 44

def belongs_to(association, through:, build: true)
  @mapping.associations << Associations::BelongsTo.new(
    association,
    through: through,
    build: build,
  )
end

#converts_with(adapter) ⇒ Object

Public: Define a set of adapters which should be used to translate data between the database and Salesforce.

adapter - An adapter object, which converts an attribute Hash between

normalized and database-ready formats.

Raises ArgumentError if the adapter object has an incomplete interface. Returns nothing.



105
106
107
108
109
110
111
# File 'lib/restforce/db/dsl.rb', line 105

def converts_with(adapter)
  unless adapter.respond_to?(:to_database) && adapter.respond_to?(:from_database)
    raise ArgumentError, "Your adapter must implement `to_database` and `from_database` methods"
  end

  @mapping.adapter = adapter
end

#has_many(association, through:, build: true) ⇒ Object

Public: Define a relationship in which the current mapping is referenced by many objects through a lookup ID on another mapping.

association - The name of the ActiveRecord association. through - A String representing the Lookup ID. build - A Boolean indicating whether or not the association chain

should be built out for new records.

Returns nothing.



78
79
80
81
82
83
84
# File 'lib/restforce/db/dsl.rb', line 78

def has_many(association, through:, build: true) # rubocop:disable PredicateName
  @mapping.associations << Associations::HasMany.new(
    association,
    through: through,
    build: build,
  )
end

#has_one(association, through:, build: true) ⇒ Object

Public: Define a relationship in which the current mapping is referenced by one object through a lookup ID on another mapping.

association - The name of the ActiveRecord association. through - A String representing the Lookup ID. build - A Boolean indicating whether or not the association chain

should be built out for new records.

Returns nothing.



61
62
63
64
65
66
67
# File 'lib/restforce/db/dsl.rb', line 61

def has_one(association, through:, build: true) # rubocop:disable PredicateName
  @mapping.associations << Associations::HasOne.new(
    association,
    through: through,
    build: build,
  )
end

#maps(fields) ⇒ Object

Public: Define a set of fields which should be synchronized between the database record and Salesforce.

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

record, and values corresponding to field names in Salesforce.

Returns nothing.



93
94
95
# File 'lib/restforce/db/dsl.rb', line 93

def maps(fields)
  @mapping.fields = fields
end

#where(*conditions) ⇒ Object

Public: Define a set of conditions which should be used to filter the Salesforce record lookups for this mapping.

conditions - An Array of String query conditions.

Returns nothing.



31
32
33
# File 'lib/restforce/db/dsl.rb', line 31

def where(*conditions)
  @mapping.conditions = conditions
end