restforce-db 0.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/.rubocop/custom/method_documentation.rb +65 -0
- data/.rubocop.yml +39 -0
- data/.travis.yml +3 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +68 -0
- data/Rakefile +13 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/lib/generators/restforce_generator.rb +19 -0
- data/lib/generators/templates/config.yml +8 -0
- data/lib/generators/templates/script +6 -0
- data/lib/restforce/db/command.rb +98 -0
- data/lib/restforce/db/configuration.rb +50 -0
- data/lib/restforce/db/instances/active_record.rb +48 -0
- data/lib/restforce/db/instances/base.rb +66 -0
- data/lib/restforce/db/instances/salesforce.rb +46 -0
- data/lib/restforce/db/mapping.rb +106 -0
- data/lib/restforce/db/model.rb +50 -0
- data/lib/restforce/db/record_type.rb +77 -0
- data/lib/restforce/db/record_types/active_record.rb +80 -0
- data/lib/restforce/db/record_types/base.rb +44 -0
- data/lib/restforce/db/record_types/salesforce.rb +94 -0
- data/lib/restforce/db/synchronizer.rb +57 -0
- data/lib/restforce/db/version.rb +10 -0
- data/lib/restforce/db/worker.rb +156 -0
- data/lib/restforce/db.rb +77 -0
- data/lib/restforce/extensions.rb +19 -0
- data/restforce-db.gemspec +41 -0
- data/test/cassettes/Restforce_DB/accessing_Salesforce/uses_the_configured_credentials.yml +43 -0
- data/test/cassettes/Restforce_DB_Instances_Salesforce/_copy_/updates_the_record_with_the_attributes_from_the_copied_object.yml +193 -0
- data/test/cassettes/Restforce_DB_Instances_Salesforce/_update_/updates_the_local_record_with_the_passed_attributes.yml +193 -0
- data/test/cassettes/Restforce_DB_Instances_Salesforce/_update_/updates_the_record_in_Salesforce_with_the_passed_attributes.yml +232 -0
- data/test/cassettes/Restforce_DB_RecordTypes_Salesforce/_create_/creates_a_record_in_Salesforce_from_the_passed_database_record_s_attributes.yml +158 -0
- data/test/cassettes/Restforce_DB_RecordTypes_Salesforce/_create_/updates_the_database_record_with_the_Salesforce_record_s_ID.yml +158 -0
- data/test/cassettes/Restforce_DB_RecordTypes_Salesforce/_find/finds_existing_records_in_Salesforce.yml +157 -0
- data/test/cassettes/Restforce_DB_RecordTypes_Salesforce/_find/returns_nil_when_no_matching_record_exists.yml +81 -0
- data/test/cassettes/Restforce_DB_Synchronizer/_run/given_a_Salesforce_record_with_an_existing_record_in_the_database/updates_the_database_record.yml +158 -0
- data/test/cassettes/Restforce_DB_Synchronizer/_run/given_an_existing_Salesforce_record/populates_the_database_with_the_new_record.yml +158 -0
- data/test/cassettes/Restforce_DB_Synchronizer/_run/given_an_existing_database_record/populates_Salesforce_with_the_new_record.yml +235 -0
- data/test/lib/restforce/db/configuration_test.rb +38 -0
- data/test/lib/restforce/db/instances/active_record_test.rb +39 -0
- data/test/lib/restforce/db/instances/salesforce_test.rb +51 -0
- data/test/lib/restforce/db/mapping_test.rb +70 -0
- data/test/lib/restforce/db/model_test.rb +48 -0
- data/test/lib/restforce/db/record_type_test.rb +26 -0
- data/test/lib/restforce/db/record_types/active_record_test.rb +85 -0
- data/test/lib/restforce/db/record_types/salesforce_test.rb +46 -0
- data/test/lib/restforce/db/synchronizer_test.rb +84 -0
- data/test/lib/restforce/db_test.rb +24 -0
- data/test/support/active_record.rb +20 -0
- data/test/support/database_cleaner.rb +3 -0
- data/test/support/salesforce.rb +48 -0
- data/test/support/vcr.rb +23 -0
- data/test/test_helper.rb +25 -0
- metadata +287 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 0cc0005c0f2912c195d0c16866ab8f0f2052e4db
|
4
|
+
data.tar.gz: 2b28c71a8c76b6bc4c08535d8d5c71a4ed1c7aef
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: d93308753057f5b9a020d59202f8acb3d1140cc18264d8669cd11e5c8818e960f3105870d6a02dbe4f1a2c88ca5b6f7e9f4eabd863cf070becf1b20ae18b38bd
|
7
|
+
data.tar.gz: 52ff1b22102b140ccec6021b57af2d2bd60996f3df4f47368e236f03642e4216389a588864c423c4fa6062d4df1b295309959b102fa6561a0c1aabadec412b12
|
data/.gitignore
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
|
5
|
+
module Cop
|
6
|
+
|
7
|
+
module Style
|
8
|
+
|
9
|
+
# This cop checks for missing method-level documentation.
|
10
|
+
class MethodDocumentation < Cop
|
11
|
+
include AnnotationComment
|
12
|
+
|
13
|
+
MSG = "Missing method-level documentation comment."
|
14
|
+
|
15
|
+
# Public: Investigate the source for undocumented methods.
|
16
|
+
def investigate(processed_source)
|
17
|
+
ast = processed_source.ast
|
18
|
+
return unless ast
|
19
|
+
|
20
|
+
ast_with_comments = Parser::Source::Comment.associate(
|
21
|
+
ast,
|
22
|
+
processed_source.comments
|
23
|
+
)
|
24
|
+
|
25
|
+
check(ast, ast_with_comments)
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
# Internal: Ensure that documentation is required for all methods. Adds
|
31
|
+
# an offense when undocumented methods are detected.
|
32
|
+
#
|
33
|
+
# Returns nothing.
|
34
|
+
def check(ast, ast_with_comments)
|
35
|
+
ast.each_node(:def) do |node|
|
36
|
+
_name, body = *node
|
37
|
+
|
38
|
+
next if associated_comment?(node, ast_with_comments)
|
39
|
+
add_offense(node, :keyword, MSG)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
# Internal: Does the node have a non-annotation comment on the preceding
|
44
|
+
# line?
|
45
|
+
#
|
46
|
+
# Returns a Boolean.
|
47
|
+
def associated_comment?(node, ast_with_comments)
|
48
|
+
return false if ast_with_comments[node].empty?
|
49
|
+
|
50
|
+
preceding_comment = ast_with_comments[node].last
|
51
|
+
distance = node.loc.keyword.line - preceding_comment.loc.line
|
52
|
+
return false if distance > 1
|
53
|
+
|
54
|
+
# As long as there's at least one comment line that isn't an
|
55
|
+
# annotation, it's OK.
|
56
|
+
ast_with_comments[node].any? { |comment| !annotation?(comment) }
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
62
|
+
|
63
|
+
end
|
64
|
+
|
65
|
+
end
|
data/.rubocop.yml
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
require:
|
2
|
+
- ./.rubocop/custom/method_documentation.rb
|
3
|
+
|
4
|
+
AllCops:
|
5
|
+
Exclude:
|
6
|
+
- ".bundle/**/*"
|
7
|
+
- "vendor/**/*"
|
8
|
+
|
9
|
+
Metrics/AbcSize:
|
10
|
+
Max: 25
|
11
|
+
|
12
|
+
Metrics/LineLength:
|
13
|
+
Enabled: false
|
14
|
+
|
15
|
+
Metrics/MethodLength:
|
16
|
+
Max: 20
|
17
|
+
|
18
|
+
Style/EmptyLinesAroundBlockBody:
|
19
|
+
Exclude:
|
20
|
+
# These are naturally DSL-y, and so let's be lenient
|
21
|
+
- "test/**/*"
|
22
|
+
|
23
|
+
Style/EmptyLinesAroundClassBody:
|
24
|
+
EnforcedStyle: empty_lines
|
25
|
+
|
26
|
+
Style/EmptyLinesAroundModuleBody:
|
27
|
+
EnforcedStyle: empty_lines
|
28
|
+
|
29
|
+
Style/SignalException:
|
30
|
+
EnforcedStyle: only_raise
|
31
|
+
|
32
|
+
Style/StringLiterals:
|
33
|
+
EnforcedStyle: double_quotes
|
34
|
+
|
35
|
+
Style/TrailingComma:
|
36
|
+
EnforcedStyleForMultiline: comma
|
37
|
+
|
38
|
+
Style/TrivialAccessors:
|
39
|
+
ExactNameMatch: true
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Table XI Partners, LLC
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
# Restforce::DB
|
2
|
+
|
3
|
+
Restforce::DB is an attempt at simplifying data integrations between a Salesforce setup and a Rails application. It provides a background worker which continuously polls for updated records both in Salesforce and in the database, and updates both systems with that data according to user-defined attribute mappings.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem "restforce-db"
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install restforce-db
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
First, you'll want to install the default bin and configuration files, which is handled by the included Rails generator:
|
24
|
+
|
25
|
+
$ bundle exec rails g restforce
|
26
|
+
|
27
|
+
This gem assumes that you're running Rails 4 or greater, therefore the `bin` file should be checked into the repository with the rest of your code. The `config/restforce-db.yml` file should be managed the same way you manage your secrets files, and probably not checked into the repository.
|
28
|
+
|
29
|
+
To register a Salesforce mapping in an `ActiveRecord` model, you need to add a few lines of DSL-style code to your class definition:
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
class LineCook < ActiveRecord::Base
|
33
|
+
|
34
|
+
include Restforce::DB::Model
|
35
|
+
|
36
|
+
map_to "Line_Cook__c", name: "Name", specialty: "Favorite_Food__c"
|
37
|
+
|
38
|
+
end
|
39
|
+
```
|
40
|
+
|
41
|
+
This will automatically register the model with an entry in the `Restforce::DB::RecordType` collection. Your schema **must** contain a `salesforce_id` column for each table you wish to synchronize with a record type in Salesforce.
|
42
|
+
|
43
|
+
To run the worker, you'll want to run the binstub installed through the generator (see above). Then you can run the self-daemonizing executable.
|
44
|
+
|
45
|
+
$ bin/restforce-db start
|
46
|
+
|
47
|
+
By default, this will load the credentials at the same location the generator installed them. You can explicitly pass the location of your configuration file with the `-c` option:
|
48
|
+
|
49
|
+
$ bin/restforce-db -c /path/to/my/config.yml start
|
50
|
+
|
51
|
+
For additional information and a full set of options, you can run:
|
52
|
+
|
53
|
+
$ bin/restforce-db -h
|
54
|
+
|
55
|
+
## Development
|
56
|
+
|
57
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
|
58
|
+
|
59
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
60
|
+
|
61
|
+
## Contributing
|
62
|
+
|
63
|
+
1. Fork it ( https://github.com/tablexi/restforce-db/fork )
|
64
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
65
|
+
3. Ensure that your changes pass all style checks and tests (`rake`)
|
66
|
+
4. Commit your changes (`git commit -am 'Add some feature'`)
|
67
|
+
5. Push to the branch (`git push origin my-new-feature`)
|
68
|
+
6. Create a new Pull Request
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "restforce/db"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
require "rails/generators/base"
|
2
|
+
|
3
|
+
# :nodoc:
|
4
|
+
class RestforceGenerator < Rails::Generators::Base
|
5
|
+
|
6
|
+
source_root File.expand_path("../templates", __FILE__)
|
7
|
+
|
8
|
+
# :nodoc:
|
9
|
+
def create_config_file
|
10
|
+
template "config.yml", "config/restforce-db.yml"
|
11
|
+
end
|
12
|
+
|
13
|
+
# :nodoc:
|
14
|
+
def create_executable_file
|
15
|
+
template "script", "bin/restforce-db"
|
16
|
+
chmod "bin/restforce-db", 0755
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
@@ -0,0 +1,8 @@
|
|
1
|
+
---
|
2
|
+
client:
|
3
|
+
username: "<username@example.com>"
|
4
|
+
password: "<password>"
|
5
|
+
security_token: "<your salesforce security token>"
|
6
|
+
client_id: "<your salesforce remote app client id>"
|
7
|
+
client_secret: "<your salesforce remote app client secret>"
|
8
|
+
host: "login.salesforce.com"
|
@@ -0,0 +1,98 @@
|
|
1
|
+
require "daemons"
|
2
|
+
require "optparse"
|
3
|
+
|
4
|
+
module Restforce
|
5
|
+
|
6
|
+
module DB
|
7
|
+
|
8
|
+
# Restforce::DB::Command represents the command line interface for our
|
9
|
+
# daemonized processing loop. It captures a set of options from the command
|
10
|
+
# line to configure the running worker thread for synchronizing data.
|
11
|
+
class Command
|
12
|
+
|
13
|
+
# Public: Initialize a new Restforce::DB::Command.
|
14
|
+
#
|
15
|
+
# args - A set of command line arguments to pass to the OptionParser.
|
16
|
+
def initialize(args)
|
17
|
+
@options = {
|
18
|
+
verbose: false,
|
19
|
+
pid_dir: Rails.root.join("tmp", "pids"),
|
20
|
+
config: Rails.root.join("config", "restforce-db.yml"),
|
21
|
+
}
|
22
|
+
|
23
|
+
@args = parser.parse!(args)
|
24
|
+
end
|
25
|
+
|
26
|
+
# Public: Initialize the daemonized processing loop for Restforce::DB.
|
27
|
+
#
|
28
|
+
# Returns nothing.
|
29
|
+
def daemonize
|
30
|
+
dir = @options[:pid_dir]
|
31
|
+
Dir.mkdir(dir) unless File.exist?(dir)
|
32
|
+
|
33
|
+
daemon_args = {
|
34
|
+
dir: @options[:pid_dir],
|
35
|
+
dir_mode: :normal,
|
36
|
+
monitor: @monitor,
|
37
|
+
ARGV: @args,
|
38
|
+
}
|
39
|
+
|
40
|
+
Restforce::DB::Worker.before_fork
|
41
|
+
Daemons.run_proc("restforce-db", daemon_args) do |*_args|
|
42
|
+
run @options
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
private
|
47
|
+
|
48
|
+
# Internal: Initiate a worker thread for Restforce::DB's record
|
49
|
+
# synchronization processing loop.
|
50
|
+
#
|
51
|
+
# options - A Hash of run configuration options for the worker.
|
52
|
+
#
|
53
|
+
# Returns nothing.
|
54
|
+
def run(options = {})
|
55
|
+
Dir.chdir(Rails.root)
|
56
|
+
|
57
|
+
Restforce::DB::Worker.after_fork
|
58
|
+
Restforce::DB::Worker.logger ||= Logger.new(File.join(Rails.root, "log", "restforce-db.log"))
|
59
|
+
|
60
|
+
worker = Restforce::DB::Worker.new(options)
|
61
|
+
worker.start
|
62
|
+
rescue => e
|
63
|
+
Restforce::DB::Worker.logger.fatal e
|
64
|
+
STDERR.puts e.message
|
65
|
+
exit 1
|
66
|
+
end
|
67
|
+
|
68
|
+
# Internal: Get an OptionParser for the Restforce::DB CLI.
|
69
|
+
#
|
70
|
+
# Returns an OptionParser.
|
71
|
+
def parser
|
72
|
+
@parser ||= OptionParser.new do |opt|
|
73
|
+
opt.banner = "Usage: #{File.basename($PROGRAM_NAME)} [options] start|stop|restart|run"
|
74
|
+
|
75
|
+
opt.on("-h", "--help", "Show this message") do
|
76
|
+
puts opt
|
77
|
+
exit 1
|
78
|
+
end
|
79
|
+
opt.on("-c FILE", "--config FILE", "The location of a Restforce credentials file.") do |file|
|
80
|
+
@options[:config] = file
|
81
|
+
end
|
82
|
+
opt.on("-i N", "--interval N", "Amount of time to wait between synchronizations.") do |n|
|
83
|
+
@options[:interval] = n.to_i
|
84
|
+
end
|
85
|
+
opt.on("--pid-dir DIR", "The directory in which to store the pidfile.") do |dir|
|
86
|
+
@options[:pid_dir] = dir
|
87
|
+
end
|
88
|
+
opt.on("-v", "--verbose", "Turn on noisy logging.") do
|
89
|
+
@options[:verbose] = true
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
end
|
95
|
+
|
96
|
+
end
|
97
|
+
|
98
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
require "yaml"
|
2
|
+
|
3
|
+
module Restforce
|
4
|
+
|
5
|
+
module DB
|
6
|
+
|
7
|
+
# Restforce::DB::Configuration exposes a handful of straightforward write
|
8
|
+
# and read methods to allow users to configure Restforce::DB.
|
9
|
+
class Configuration
|
10
|
+
|
11
|
+
attr_accessor(*%i(
|
12
|
+
username
|
13
|
+
password
|
14
|
+
security_token
|
15
|
+
client_id
|
16
|
+
client_secret
|
17
|
+
host
|
18
|
+
))
|
19
|
+
|
20
|
+
# Public: Parse a supplied YAML file for a set of credentials, and use
|
21
|
+
# them to populate the attributes on this configuraton object.
|
22
|
+
#
|
23
|
+
# file_path - A String or Path referencing a client configuration file.
|
24
|
+
#
|
25
|
+
# Returns nothing.
|
26
|
+
def parse(file_path)
|
27
|
+
settings = YAML.load_file(file_path)
|
28
|
+
load(settings["client"])
|
29
|
+
end
|
30
|
+
|
31
|
+
# Public: Populate this configuration object from a Hash of credentials.
|
32
|
+
#
|
33
|
+
# configurations - A Hash of credentials, with keys matching the names
|
34
|
+
# of the attributes for this class.
|
35
|
+
#
|
36
|
+
# Returns nothing.
|
37
|
+
def load(configurations)
|
38
|
+
self.username = configurations["username"]
|
39
|
+
self.password = configurations["password"]
|
40
|
+
self.security_token = configurations["security_token"]
|
41
|
+
self.client_id = configurations["client_id"]
|
42
|
+
self.client_secret = configurations["client_secret"]
|
43
|
+
self.host = configurations["host"]
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
module Restforce
|
2
|
+
|
3
|
+
module DB
|
4
|
+
|
5
|
+
module Instances
|
6
|
+
|
7
|
+
# Restforce::DB::Instances::ActiveRecord serves as a wrapper for
|
8
|
+
# ActiveRecord::Base-compatible objects, exposing a common API to
|
9
|
+
# reconcile record attributes with Salesforce instances.
|
10
|
+
class ActiveRecord < Base
|
11
|
+
|
12
|
+
# Public: Get a common identifier for this record.
|
13
|
+
#
|
14
|
+
# Returns a String.
|
15
|
+
def id
|
16
|
+
@record.salesforce_id
|
17
|
+
end
|
18
|
+
|
19
|
+
# Public: Has this record been synced to a Salesforce record?
|
20
|
+
#
|
21
|
+
# Returns a Boolean.
|
22
|
+
def synced?
|
23
|
+
@record.salesforce_id?
|
24
|
+
end
|
25
|
+
|
26
|
+
# Public: Get the time of the last update to this record.
|
27
|
+
#
|
28
|
+
# Returns a Time-compatible object.
|
29
|
+
def last_update
|
30
|
+
@record.updated_at
|
31
|
+
end
|
32
|
+
|
33
|
+
private
|
34
|
+
|
35
|
+
# Internal: Get a description of the expected attribute Hash format.
|
36
|
+
#
|
37
|
+
# Returns a Symbol.
|
38
|
+
def conversion
|
39
|
+
:database
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
module Restforce
|
2
|
+
|
3
|
+
module DB
|
4
|
+
|
5
|
+
module Instances
|
6
|
+
|
7
|
+
# Restforce::DB::Instances::Base defines common behavior for the other
|
8
|
+
# models defined in the Restforce::DB::Instances namespace.
|
9
|
+
class Base
|
10
|
+
|
11
|
+
attr_reader :record
|
12
|
+
|
13
|
+
# Public: Initialize a new Restforce::DB::Instances::Base instance.
|
14
|
+
#
|
15
|
+
# record - The Salesforce or database record to manage.
|
16
|
+
# mapping - An instance of Restforce::DB::Mapping.
|
17
|
+
def initialize(record, mapping = Mapping.new)
|
18
|
+
@record = record
|
19
|
+
@mapping = mapping
|
20
|
+
end
|
21
|
+
|
22
|
+
# Public: Update the instance with the passed attributes.
|
23
|
+
#
|
24
|
+
# attributes - A Hash mapping attribute names to values.
|
25
|
+
#
|
26
|
+
# Returns a truthy value.
|
27
|
+
# Raises if the update fails for any reason.
|
28
|
+
def update!(attributes)
|
29
|
+
record.update!(attributes)
|
30
|
+
end
|
31
|
+
|
32
|
+
# Public: Update the instance with attributes copied from the passed
|
33
|
+
# record.
|
34
|
+
#
|
35
|
+
# record - An object responding to `#attributes`. Must return a Hash of
|
36
|
+
# attributes corresponding to the configured mappings for this
|
37
|
+
# instance.
|
38
|
+
#
|
39
|
+
# Returns a truthy value.
|
40
|
+
# Raises if the update fails for any reason.
|
41
|
+
def copy!(from_record)
|
42
|
+
update! @mapping.convert(conversion, from_record.attributes)
|
43
|
+
end
|
44
|
+
|
45
|
+
# Public: Get a Hash mapping the configured attributes names to their
|
46
|
+
# values for this instance.
|
47
|
+
#
|
48
|
+
# Returns a Hash.
|
49
|
+
def attributes
|
50
|
+
@mapping.attributes(conversion) { |attribute| record.send(attribute) }
|
51
|
+
end
|
52
|
+
|
53
|
+
# Public: Has this record been synced with Salesforce?
|
54
|
+
#
|
55
|
+
# Returns a Boolean.
|
56
|
+
def synced?
|
57
|
+
true
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
63
|
+
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
module Restforce
|
2
|
+
|
3
|
+
module DB
|
4
|
+
|
5
|
+
module Instances
|
6
|
+
|
7
|
+
# Restforce::DB::Instances::Salesforce serves as a wrapper for Salesforce
|
8
|
+
# objects, exposing a common API to reconcile record attributes with
|
9
|
+
# ActiveRecord instances.
|
10
|
+
class Salesforce < Base
|
11
|
+
|
12
|
+
INTERNAL_ATTRIBUTES = %w(
|
13
|
+
Id
|
14
|
+
SystemModstamp
|
15
|
+
).freeze
|
16
|
+
|
17
|
+
# Public: Get a common identifier for this record.
|
18
|
+
#
|
19
|
+
# Returns a String.
|
20
|
+
def id
|
21
|
+
@record.Id
|
22
|
+
end
|
23
|
+
|
24
|
+
# Public: Get the time of the last update to this record.
|
25
|
+
#
|
26
|
+
# Returns a Time-compatible object.
|
27
|
+
def last_update
|
28
|
+
Time.parse(@record.SystemModstamp)
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
# Internal: Get a description of the expected attribute Hash format.
|
34
|
+
#
|
35
|
+
# Returns a Symbol.
|
36
|
+
def conversion
|
37
|
+
:salesforce
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|