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:) ⇒ 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.

Returns nothing.



42
43
44
45
46
47
# File 'lib/restforce/db/dsl.rb', line 42

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

#converts(conversions) ⇒ Object

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

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

record, and adapter objects as values.

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



96
97
98
99
100
101
102
# File 'lib/restforce/db/dsl.rb', line 96

def converts(conversions)
  unless conversions.values.all? { |c| c.respond_to?(:to_database) && c.respond_to?(:to_salesforce) }
    raise ArgumentError, "All adapters must implement `to_database` and `to_salesforce` methods"
  end

  @mapping.conversions = conversions
end

#has_many(association, through:) ⇒ 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.

Returns nothing.



70
71
72
73
74
75
# File 'lib/restforce/db/dsl.rb', line 70

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

#has_one(association, through:) ⇒ 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.

Returns nothing.



56
57
58
59
60
61
# File 'lib/restforce/db/dsl.rb', line 56

def has_one(association, through:) # rubocop:disable PredicateName
  @mapping.associations << Associations::HasOne.new(
    association,
    through: through,
  )
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.



84
85
86
# File 'lib/restforce/db/dsl.rb', line 84

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