salesforce-orm 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 303fe6c567cdeb834103e79e613875af01482118
4
+ data.tar.gz: 4ab2e2a284c173d67999eb1ade3f39bff2921a3b
5
+ SHA512:
6
+ metadata.gz: 53f212dd26d796b3d3d5c139ac239e74e692cff06f82ee046aab734c4838b1018887cc24b5e3d35ca2f63faabd4bf8d19c47eeccf294d4eb7489c0ee4e870141
7
+ data.tar.gz: ea1d0e11c91c047f610f1c10aa8ce008278ae616a0eba92c0bba2ce029175f7b4ddd24cfe451f901522cecdf7f25fae5a39f44e52d2e848b1cdcef37b4c3ef25
data/.editorconfig ADDED
@@ -0,0 +1,37 @@
1
+ # http://editorconfig.org
2
+
3
+ # A special property that should be specified at the top of the file outside of
4
+ # any sections. Set to true to stop .editor config file search on current file
5
+ root = true
6
+
7
+ [*]
8
+ # Indentation style
9
+ # Possible values - tab, space
10
+ indent_style = space
11
+
12
+ # Indentation size in single-spaced characters
13
+ # Possible values - an integer, tab
14
+ indent_size = 2
15
+
16
+ # Line ending file format
17
+ # Possible values - lf, crlf, cr
18
+ end_of_line = lf
19
+
20
+ # File character encoding
21
+ # Possible values - latin1, utf-8, utf-16be, utf-16le
22
+ charset = utf-8
23
+
24
+ # Denotes whether to trim whitespace at the end of lines
25
+ # Possible values - true, false
26
+ trim_trailing_whitespace = true
27
+
28
+ # Denotes whether file should end with a newline
29
+ # Possible values - true, false
30
+ insert_final_newline = true
31
+
32
+ #Not supported by all the editor.
33
+ #More details: https://github.com/editorconfig/editorconfig/wiki/EditorConfig-Properties#max_line_length
34
+ max_line_length = 80
35
+
36
+ [*.md]
37
+ trim_trailing_whitespace = false
data/.gitignore ADDED
@@ -0,0 +1,53 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /spec/examples.txt
9
+ /test/tmp/
10
+ /test/version_tmp/
11
+ /tmp/
12
+
13
+ # Used by dotenv library to load environment variables.
14
+ # .env
15
+
16
+ ## Specific to RubyMotion:
17
+ .dat*
18
+ .repl_history
19
+ build/
20
+ *.bridgesupport
21
+ build-iPhoneOS/
22
+ build-iPhoneSimulator/
23
+
24
+ ## Specific to RubyMotion (use of CocoaPods):
25
+ #
26
+ # We recommend against adding the Pods directory to your .gitignore. However
27
+ # you should judge for yourself, the pros and cons are mentioned at:
28
+ # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
29
+ #
30
+ # vendor/Pods/
31
+
32
+ ## Documentation cache and generated files:
33
+ /.yardoc/
34
+ /_yardoc/
35
+ /doc/
36
+ /rdoc/
37
+
38
+ ## Environment normalization:
39
+ /.bundle/
40
+ /vendor/bundle
41
+ /lib/bundler/man/
42
+
43
+ # for a library or gem, you might want to ignore these files since the code is
44
+ # intended to run in multiple environments; otherwise, check them in:
45
+ # Gemfile.lock
46
+ # .ruby-version
47
+ # .ruby-gemset
48
+
49
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
50
+ .rvmrc
51
+ .DS_Store
52
+ .byebug_history
53
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.1.5
5
+ before_install: gem install bundler -v 1.15.1
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,74 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ salesforce-orm (1.0.0)
5
+ activerecord (~> 3)
6
+ activerecord-nulldb-adapter (~> 0)
7
+ restforce (~> 2.5)
8
+
9
+ GEM
10
+ remote: https://rubygems.org/
11
+ specs:
12
+ activemodel (3.2.22.5)
13
+ activesupport (= 3.2.22.5)
14
+ builder (~> 3.0.0)
15
+ activerecord (3.2.22.5)
16
+ activemodel (= 3.2.22.5)
17
+ activesupport (= 3.2.22.5)
18
+ arel (~> 3.0.2)
19
+ tzinfo (~> 0.3.29)
20
+ activerecord-nulldb-adapter (0.3.7)
21
+ activerecord (>= 2.0.0)
22
+ activesupport (3.2.22.5)
23
+ i18n (~> 0.6, >= 0.6.4)
24
+ multi_json (~> 1.0)
25
+ arel (3.0.3)
26
+ builder (3.0.4)
27
+ byebug (0.0.1)
28
+ columnize (>= 0.3.1)
29
+ debugger-linecache (~> 1.2.0)
30
+ columnize (0.9.0)
31
+ debugger-linecache (1.2.0)
32
+ diff-lcs (1.3)
33
+ faraday (0.12.1)
34
+ multipart-post (>= 1.2, < 3)
35
+ faraday_middleware (0.11.0.1)
36
+ faraday (>= 0.7.4, < 1.0)
37
+ hashie (3.5.5)
38
+ i18n (0.8.4)
39
+ json (2.1.0)
40
+ multi_json (1.12.1)
41
+ multipart-post (2.0.0)
42
+ rake (10.4.2)
43
+ restforce (2.5.3)
44
+ faraday (>= 0.9.0, <= 1.0)
45
+ faraday_middleware (>= 0.8.8, <= 1.0)
46
+ hashie (>= 1.2.0, < 4.0)
47
+ json (>= 1.7.5)
48
+ rspec (3.6.0)
49
+ rspec-core (~> 3.6.0)
50
+ rspec-expectations (~> 3.6.0)
51
+ rspec-mocks (~> 3.6.0)
52
+ rspec-core (3.6.0)
53
+ rspec-support (~> 3.6.0)
54
+ rspec-expectations (3.6.0)
55
+ diff-lcs (>= 1.2.0, < 2.0)
56
+ rspec-support (~> 3.6.0)
57
+ rspec-mocks (3.6.0)
58
+ diff-lcs (>= 1.2.0, < 2.0)
59
+ rspec-support (~> 3.6.0)
60
+ rspec-support (3.6.0)
61
+ tzinfo (0.3.53)
62
+
63
+ PLATFORMS
64
+ ruby
65
+
66
+ DEPENDENCIES
67
+ bundler (~> 1.15)
68
+ byebug (~> 0)
69
+ rake (~> 10.0)
70
+ rspec (~> 3.0)
71
+ salesforce-orm!
72
+
73
+ BUNDLED WITH
74
+ 1.15.1
data/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "{}"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright {yyyy} {name of copyright owner}
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
data/README.md ADDED
@@ -0,0 +1,228 @@
1
+ # salesforce-orm
2
+ Active record like ORM for Salesforce
3
+
4
+ [![Build Status](https://travis-ci.org/NestAway/salesforce-orm.svg?branch=master)](https://travis-ci.org/NestAway/salesforce-orm)
5
+
6
+ ## Setup
7
+
8
+ Add gem to your Gemfile
9
+
10
+ ```
11
+ gem 'salesforce-orm'
12
+ ```
13
+
14
+ Or, If you want to install globally
15
+
16
+ ```
17
+ gem install salesforce-orm
18
+ ```
19
+
20
+ This Gem internally use [Restforce](https://github.com/ejholmes/restforce), So you have configure it
21
+
22
+ There are 2 options to configure ([Restforce config](https://github.com/ejholmes/restforce#initialization))
23
+
24
+ **Option 1**
25
+
26
+ Set ENV variable as per Restforce doc
27
+
28
+ **Option 2**
29
+
30
+ In rails, write below code in application.rb or environment specific file
31
+
32
+ Other projects, run it before you use SaleforceOrm
33
+
34
+ ```
35
+ SaleforceOrm::Configuration.restforce_config = {
36
+ ... # Restforce configuration
37
+ }
38
+ ```
39
+
40
+ ## Usage
41
+
42
+ Create object class
43
+
44
+ ```
45
+ class SampleObject < SalesforceOrm::ObjectBase
46
+ end
47
+ ```
48
+
49
+ ### object_name
50
+
51
+ Default object name is `class.name`
52
+
53
+ ```
54
+ SampleObject
55
+ ```
56
+
57
+ If you have a custom object name,
58
+
59
+ ```
60
+ class SampleObject < SalesforceOrm::ObjectBase
61
+ self.object_name = 'SampleObject__c'
62
+ end
63
+ ```
64
+
65
+ ### field_map
66
+
67
+ Field map is used for create, update actions. This can be used for aliasing the field names
68
+
69
+ Default field map for `SampleObject`
70
+
71
+ ```
72
+ {
73
+ id: :Id,
74
+ created_at: :CreatedAt,
75
+ updated_at: :UpdatedAt
76
+ }
77
+ ```
78
+
79
+ If you wanna map more fields for an object
80
+
81
+ ```
82
+ class SampleObject < SalesforceOrm::ObjectBase
83
+ self.field_map = {
84
+ field_one: :FieldOne,
85
+ field_two: :FieldTwo__c,
86
+ }
87
+ end
88
+ ```
89
+
90
+ ### data_type_map
91
+
92
+ Allowed data types are,
93
+
94
+ - `:integer`
95
+ - `:date_time`
96
+ - `:array`
97
+
98
+ Default is same data type of given value
99
+
100
+ Default data type map for `SampleObject`
101
+
102
+ ```
103
+ {
104
+ created_at: :datetime,
105
+ updated_at: :datetime
106
+ }
107
+ ```
108
+
109
+ If you wanna change the data type of some fields
110
+
111
+ ```
112
+ {
113
+ field_one: :boolean,
114
+ field_two: :integer
115
+ }
116
+ ```
117
+
118
+ **NOTE: It's mandatory to add data type map for boolean fields**
119
+
120
+ ### Methods
121
+
122
+ Methods are similar to ActiveRecord::Base
123
+
124
+ Class methods
125
+
126
+ ```
127
+ SampleObject.[
128
+ :create!,
129
+ :update_all!,
130
+ :destroy_all!,
131
+ :where,
132
+ :select,
133
+ :except,
134
+ :group,
135
+ :order,
136
+ :reorder,
137
+ :limit,
138
+ :offset,
139
+ :first,
140
+ :last,
141
+ :each,
142
+ :scoped,
143
+ :all,
144
+ :find_by_*
145
+ ]
146
+ ```
147
+
148
+ eg:
149
+ ```
150
+ SampleObject.where(id: 'qd')
151
+
152
+ SampleObject.where(id: ['eqd', 'qqwd'])
153
+
154
+ SampleObject.where(id: ['eqd', 'qqwd'], field_one: 'KJbn').where('a = b').all
155
+
156
+ SampleObject.where(id: 'qd').group(:a, :b).each do |sobj|
157
+ puts sobj.id
158
+ end
159
+
160
+ SampleObject.find('qwd')
161
+
162
+ SampleObject.find_by_id('qwd')
163
+
164
+ SampleObject.find_by_field_one_and_field_two_and_field_three(1, 2, 3)
165
+
166
+ SampleObject.select('count(id)').all
167
+ ```
168
+
169
+ Instance methods
170
+
171
+ ```
172
+ SampleObject.[
173
+ :update_attributes,
174
+ :destroy
175
+ ]
176
+ ```
177
+
178
+ Other class methods (Specific to SalesforceOrm)
179
+
180
+ #### update_by_id!
181
+
182
+ To update an object by id
183
+
184
+ ```
185
+ SampleObject.update_by_id!('some_id', {feild_one: 'some_value', field_two: 'some_other_value'})
186
+ ```
187
+
188
+ #### destroy_by_id!
189
+
190
+ To destroy an object by id
191
+
192
+ ```
193
+ SampleObject.destroy_by_id!('some_id')
194
+ ```
195
+
196
+ #### to_soql
197
+
198
+ To generate, SOQL query (Equavalent to `to_sql`)
199
+
200
+ #### build
201
+
202
+ To create a new instance of SampleObject
203
+
204
+ ```
205
+ SampleObject.build({id: 'some id', field_one: 'Some value'})
206
+ ```
207
+
208
+ ## Pending
209
+
210
+ - Default values
211
+ - Relationships
212
+ - Record type
213
+ - More data types
214
+ - Better aggregate methods
215
+
216
+ ## Contributing
217
+
218
+ If you'd like to contribute a feature or bugfix: Thanks! To make sure your
219
+ fix/feature has a high chance of being included, please read the following
220
+ guidelines:
221
+
222
+ 1. Post a [pull request](https://github.com/NestAway/salesforce-orm/compare).
223
+ 2. Make sure there are tests! We will not accept any patch that is not tested.
224
+ It's a rare time when explicit tests aren't needed. If you have questions
225
+ about writing tests for salesforce-orm, please open a
226
+ [GitHub issue](https://github.com/NestAway/salesforce-orm/issues/new).
227
+
228
+ Thank you to all [the contributors](https://github.com/NestAway/salesforce-orm/graphs/contributors)!
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
data/build.sh ADDED
@@ -0,0 +1,6 @@
1
+ gem uninstall -a salesforce-orm-*
2
+ rm salesforce-orm-*
3
+ gem build salesforce-orm.gemspec
4
+ gem install salesforce-orm-*
5
+
6
+
@@ -0,0 +1,6 @@
1
+ Dir[File.expand_path('salesforce-orm/*.rb', File.dirname(__FILE__))].each do |file|
2
+ require file
3
+ end
4
+
5
+ module SalesforceOrm
6
+ end
@@ -0,0 +1,175 @@
1
+ require 'forwardable'
2
+ require_relative 'sql_to_soql'
3
+
4
+ module SalesforceOrm
5
+ class Base
6
+
7
+ include Enumerable, SqlToSoql
8
+ extend Forwardable
9
+
10
+ def_delegators :make_query, *([:each] + Enumerable.instance_methods)
11
+
12
+ attr_reader :builder, :client, :klass
13
+
14
+ def initialize(klass)
15
+ unless klass.singleton_class.included_modules.include?(ObjectMaker)
16
+ raise Error::CommonError, "Salesforce object has to be extended from #{ObjectMaker.name}"
17
+ end
18
+
19
+ @klass = klass
20
+ @client = RestforceClient.instance
21
+ @builder = QueryBuilder.select(klass.field_map.keys)
22
+ end
23
+
24
+ # create! doesn't return the SalesForce object back
25
+ # It will return only the object id
26
+ def create!(attributes)
27
+ client.create!(klass.object_name, map_to_keys(attributes))
28
+ end
29
+
30
+ # Transaction not guaranteed
31
+ def destroy_all!(*args)
32
+ each do |object|
33
+ object.destroy!(*args)
34
+ end
35
+ end
36
+
37
+ def destroy_by_id!(id)
38
+ client.destroy(klass.object_name, id)
39
+ end
40
+
41
+ def destroy!(object)
42
+ destroy_by_id!(object.id)
43
+ end
44
+
45
+ # Transaction not guaranteed
46
+ def update_all!(attributes)
47
+ each do |object|
48
+ object.update_attributes!(attributes)
49
+ end
50
+ end
51
+
52
+ def update_by_id!(id, attributes)
53
+ client.update!(
54
+ klass.object_name,
55
+ map_to_keys(attributes.merge({id: id}))
56
+ )
57
+ end
58
+
59
+ def update_attributes!(object, attributes)
60
+ update_by_id!(object.id, attributes)
61
+ end
62
+
63
+ # Handling select differently because we select all the fields by default
64
+ def select(*args)
65
+ except(:select)
66
+ @builder = builder.select(*args)
67
+ self
68
+ end
69
+
70
+ [
71
+ :scoped,
72
+ :except,
73
+ :where,
74
+ :group,
75
+ :limit,
76
+ :offset,
77
+ :order,
78
+ :reorder
79
+ ].each do |method_name|
80
+ define_method(method_name) do |*args|
81
+ @builder = builder.send(method_name, *args)
82
+ self
83
+ end
84
+ end
85
+
86
+ def all(*args)
87
+ make_query
88
+ end
89
+
90
+ def first
91
+ limit(1).make_query.first
92
+ end
93
+
94
+ def last
95
+ order('created_at DESC').first
96
+ end
97
+
98
+ def to_soql
99
+ sql_to_soql(builder.to_sql)
100
+ end
101
+
102
+ def make_query
103
+ begin
104
+ soql = to_soql
105
+ client.query(to_soql).find_all.map do |object|
106
+ build(object)
107
+ end
108
+ rescue => e
109
+ # On passing a invalid object id, salesforce throughs an exception
110
+ # with message starting with INVALID_QUERY_FILTER_OPERATOR, we'll be
111
+ # considering this as an ObjectNotFound exception
112
+ if e.message =~ /^INVALID_QUERY_FILTER_OPERATOR/
113
+ raise Error::ObjectNotFound
114
+ else
115
+ raise
116
+ end
117
+ end
118
+ end
119
+
120
+ def build(object)
121
+ result = klass.new(map_from_keys(object))
122
+ result.original_object = object
123
+ result
124
+ end
125
+
126
+ private
127
+
128
+ def map_to_keys(attributes)
129
+ map = klass.field_map
130
+ new_attributes = {}
131
+ attributes.keys.each do |key|
132
+ key_sym = key.to_sym
133
+ new_attributes[map[key_sym]] = attributes[key] if map[key_sym]
134
+ end
135
+ new_attributes
136
+ end
137
+
138
+ def map_from_keys(attributes)
139
+ map = klass.field_map
140
+ data_type_map = klass.data_type_map
141
+ new_attributes = {}
142
+ attributes.keys.each do |key|
143
+ key_sym = key.to_sym
144
+ new_key = map.key(key_sym)
145
+ if new_key
146
+ new_attributes[new_key] = cast_to(
147
+ value: attributes[key],
148
+ data_type: data_type_map[new_key]
149
+ )
150
+ else
151
+ # The feilds which is not in field_map also will get added
152
+ # to the ObjectBase, to support aggregate queries
153
+ new_attributes[key] = attributes[key]
154
+ end
155
+ end
156
+ new_attributes
157
+ end
158
+
159
+ def cast_to(value:, data_type:)
160
+ case data_type
161
+ when :integer
162
+ value.to_i
163
+ when :date_time
164
+ return nil if value.blank?
165
+ Time.zone.parse(value)
166
+ when :array
167
+ return [] if value.blank?
168
+ value.split(';')
169
+ else
170
+ value
171
+ end
172
+ end
173
+
174
+ end
175
+ end
@@ -0,0 +1,16 @@
1
+ module SalesforceOrm
2
+ class Configuration
3
+
4
+ class << self
5
+
6
+ def restforce_config=(config)
7
+ @restforce_config = config
8
+ end
9
+
10
+ def restforce_config
11
+ @restforce_config
12
+ end
13
+
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,11 @@
1
+ module SalesforceOrm
2
+ module Error
3
+
4
+ # A general exception
5
+ class Base < StandardError; end
6
+
7
+ class CommonError < Base; end
8
+
9
+ class ObjectNotFound < Base; end
10
+ end
11
+ end
@@ -0,0 +1,68 @@
1
+ require 'ostruct'
2
+ require_relative 'object_maker'
3
+
4
+ module SalesforceOrm
5
+ class ObjectBase < OpenStruct
6
+
7
+ extend ObjectMaker
8
+
9
+ class << self
10
+
11
+ [
12
+ :create!,
13
+ :update_all!,
14
+ :update_by_id!,
15
+ :destroy_all!,
16
+ :destroy_by_id!,
17
+ :where,
18
+ :select,
19
+ :except,
20
+ :group,
21
+ :order,
22
+ :reorder,
23
+ :limit,
24
+ :offset,
25
+ :first,
26
+ :last,
27
+ :each,
28
+ :scoped,
29
+ :all,
30
+ :to_soql,
31
+ :build
32
+ ].each do |method_name|
33
+ define_method(method_name) do |*args|
34
+ orm.send(method_name, *args)
35
+ end
36
+ end
37
+
38
+ def find(*args)
39
+ find_by_id(*args)
40
+ end
41
+
42
+ def method_missing(method, *args, &block)
43
+ regex = /^find_by_(.+)$/
44
+ if method =~ regex
45
+ fields = method.to_s.match(regex).captures[0].split('_and_')
46
+ condition = {}
47
+ fields.each_with_index do |field, index|
48
+ condition[field.to_sym] = args[index]
49
+ end
50
+ where(condition).first
51
+ end
52
+ end
53
+
54
+ def orm
55
+ Base.new(self)
56
+ end
57
+ end
58
+
59
+ [
60
+ :update_attributes!,
61
+ :destroy!
62
+ ].each do |method_name|
63
+ define_method(method_name) do |*args|
64
+ self.class.orm.send(method_name, *([self] + args))
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,39 @@
1
+ module SalesforceOrm
2
+ module ObjectMaker
3
+
4
+ DEFAULT_FIELD_MAP = {
5
+ id: :Id,
6
+ created_at: :CreatedDate,
7
+ updated_at: :LastModifiedDate
8
+ }
9
+
10
+ DEFAULT_DATA_TYPE_MAP = {
11
+ created_at: :date_time,
12
+ updated_at: :date_time
13
+ }
14
+
15
+ def field_map=(field_map)
16
+ @field_map = DEFAULT_FIELD_MAP.merge(field_map)
17
+ end
18
+
19
+ def field_map
20
+ @field_map || DEFAULT_FIELD_MAP
21
+ end
22
+
23
+ def data_type_map=(data_type_map)
24
+ @data_type_map = DEFAULT_DATA_TYPE_MAP.merge(data_type_map)
25
+ end
26
+
27
+ def data_type_map
28
+ @data_type_map || DEFAULT_DATA_TYPE_MAP
29
+ end
30
+
31
+ def object_name=(new_name)
32
+ @object_name = new_name
33
+ end
34
+
35
+ def object_name
36
+ @object_name || name.demodulize
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,14 @@
1
+ require 'active_record'
2
+
3
+ module SalesforceOrm
4
+ class QueryBuilder < ActiveRecord::Base
5
+
6
+ DUMMY_TABLE_NAME = 'table_name'
7
+
8
+ self.table_name = DUMMY_TABLE_NAME
9
+
10
+ establish_connection(
11
+ adapter: :nulldb
12
+ )
13
+ end
14
+ end
@@ -0,0 +1,24 @@
1
+ require 'restforce'
2
+
3
+ module SalesforceOrm
4
+ module RestforceClient
5
+
6
+ module_function
7
+
8
+ def instance
9
+ # TODO need to verify with Sidekiq
10
+
11
+ # Making Restforce object a thread local variable to avoid making
12
+ # authentication request for each query we make to SalesForce.
13
+ # With this, only one authentication request will be made to SalesForce
14
+ # per request.
15
+ #
16
+ # Thread local variable will guarantee us a new Restforce object for each request.
17
+ #
18
+ # If this was a singleton object for process, then we'll have to
19
+ # manually refresh authentication token for each request
20
+ Thread.current[:salesforce_orm_restforce_client] ||= Restforce.new(Configuration.restforce_config || {})
21
+ end
22
+
23
+ end
24
+ end
@@ -0,0 +1,71 @@
1
+ module SalesforceOrm
2
+ module SqlToSoql
3
+ def aggregate_function?(keyword)
4
+ keyword =~ /^(AVG|COUNT|COUNT|COUNT_DISTINCT|MIN|MAX|SUM)\(/i
5
+ end
6
+
7
+ def convert_aliased_fields(sql_str, split_by = Regexp.new('\s+'), join_by = ' ')
8
+ spcial_char_regex = /[=<>!,]+/
9
+ sql_str.split(split_by, -1).map do |keyword|
10
+ if aggregate_function?(keyword)
11
+ aggregate_data = keyword.match(/^(.*)\((.*)(\).*)/i).captures
12
+ raise Error::CommonError, 'Invalid aggregate function' unless aggregate_data[1]
13
+ "#{aggregate_data[0]}(#{convert_aliased_fields(aggregate_data[1])}#{aggregate_data[2]}"
14
+ elsif keyword =~ spcial_char_regex
15
+ convert_aliased_fields(keyword, spcial_char_regex, keyword.gsub(spcial_char_regex).first)
16
+ else
17
+ klass.field_map[keyword.to_sym] || keyword
18
+ end
19
+ end.join(join_by)
20
+ end
21
+
22
+ def boolean_data_type_conversion(sql)
23
+ klass.data_type_map.each do |keyword, data_type|
24
+ if data_type == :boolean
25
+ [0, 1].each do |value|
26
+ regex = Regexp.new("\s+#{keyword}\s*\=\s*#{value}(\s+|$)")
27
+ sql.gsub!(regex, " #{keyword} = #{value == 1}\\1")
28
+ end
29
+ ['t', 'f'].each do |value|
30
+ regex = Regexp.new("\s+#{keyword}\s*\=\s*'#{value}'(\s+|$)")
31
+ sql.gsub!(regex, " #{keyword} = #{value == 't'}\\1")
32
+ end
33
+ end
34
+ end
35
+ sql
36
+ end
37
+
38
+ # TODO: optimize this method
39
+ def sql_to_soql(sql)
40
+ # Unescape column and table names
41
+ sql.gsub!('`', '')
42
+
43
+ # Remove table namespace from fields
44
+ sql.gsub!("#{QueryBuilder::DUMMY_TABLE_NAME}.", '')
45
+
46
+ # Add table name
47
+ sql.gsub!(QueryBuilder::DUMMY_TABLE_NAME, klass.object_name)
48
+
49
+ # Convert 1=0 to id IS NULL (id never be NULL, so it a false case)
50
+ sql.gsub!(/\s+1=0(\s*)/i, ' id IS NULL\1')
51
+
52
+ # Convert IS NOT to !=
53
+ sql.gsub!(/\s+IS\s+NOT\s+/i, ' != ')
54
+
55
+ # Convert IS to =
56
+ sql.gsub!(/\s+IS\s+/i, ' = ')
57
+
58
+ # Convert datatime to salesforce format
59
+ sql.gsub!(/'(\d{4}-\d{2}-\d{2}) (\d{2}:\d{2}:\d{2})'/, '\1T\2Z')
60
+
61
+ # Convert date to salesforce format
62
+ sql.gsub!(/'(\d{4}-\d{2}-\d{2})'/, '\1')
63
+
64
+ # Convert boolean_field = (1|0) to boolean_field = (true|false)
65
+ sql = boolean_data_type_conversion(sql)
66
+
67
+ # Convert aliased fields
68
+ convert_aliased_fields(sql).strip
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,3 @@
1
+ module SalesforceOrm
2
+ VERSION = '1.0.0'.freeze
3
+ end
@@ -0,0 +1,31 @@
1
+ lib = File.expand_path("../lib", __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'salesforce-orm/version'
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = 'salesforce-orm'
7
+ s.version = SalesforceOrm::VERSION.dup
8
+ s.platform = Gem::Platform::RUBY
9
+ s.date = '2017-06-07'
10
+ s.summary = 'Ruby ORM for Salesforce'
11
+ s.description = 'Active record like ORM for Salesforce'
12
+ s.authors = ['Vishal Vijay', 'Shivansh Gaur']
13
+ s.email = ['tech_team@nestaway.com', '0vishalvijay0@gmail.com']
14
+ s.files = `git ls-files`.split("\n").reject do |f|
15
+ f.match(%r{^(spec)/})
16
+ end
17
+ s.homepage = 'https://github.com/NestAway/salesforce-orm'
18
+ s.license = 'Apache License 2.0'
19
+
20
+ s.required_ruby_version = '>= 1.9.8'
21
+ s.require_paths = ['lib']
22
+
23
+ s.add_dependency 'activerecord', '~> 3'
24
+ s.add_dependency 'activerecord-nulldb-adapter', '~> 0'
25
+ s.add_dependency 'restforce', '~> 2.5'
26
+
27
+ s.add_development_dependency 'byebug', '~> 0'
28
+ s.add_development_dependency 'rspec', '~> 3.0'
29
+ s.add_development_dependency 'bundler', '~> 1.15'
30
+ s.add_development_dependency 'rake', '~> 10.0'
31
+ end
metadata ADDED
@@ -0,0 +1,165 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: salesforce-orm
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Vishal Vijay
8
+ - Shivansh Gaur
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2017-06-07 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activerecord
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '3'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '3'
28
+ - !ruby/object:Gem::Dependency
29
+ name: activerecord-nulldb-adapter
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ type: :runtime
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: restforce
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - "~>"
47
+ - !ruby/object:Gem::Version
48
+ version: '2.5'
49
+ type: :runtime
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "~>"
54
+ - !ruby/object:Gem::Version
55
+ version: '2.5'
56
+ - !ruby/object:Gem::Dependency
57
+ name: byebug
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - "~>"
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ - !ruby/object:Gem::Dependency
71
+ name: rspec
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - "~>"
75
+ - !ruby/object:Gem::Version
76
+ version: '3.0'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - "~>"
82
+ - !ruby/object:Gem::Version
83
+ version: '3.0'
84
+ - !ruby/object:Gem::Dependency
85
+ name: bundler
86
+ requirement: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - "~>"
89
+ - !ruby/object:Gem::Version
90
+ version: '1.15'
91
+ type: :development
92
+ prerelease: false
93
+ version_requirements: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - "~>"
96
+ - !ruby/object:Gem::Version
97
+ version: '1.15'
98
+ - !ruby/object:Gem::Dependency
99
+ name: rake
100
+ requirement: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - "~>"
103
+ - !ruby/object:Gem::Version
104
+ version: '10.0'
105
+ type: :development
106
+ prerelease: false
107
+ version_requirements: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - "~>"
110
+ - !ruby/object:Gem::Version
111
+ version: '10.0'
112
+ description: Active record like ORM for Salesforce
113
+ email:
114
+ - tech_team@nestaway.com
115
+ - 0vishalvijay0@gmail.com
116
+ executables: []
117
+ extensions: []
118
+ extra_rdoc_files: []
119
+ files:
120
+ - ".editorconfig"
121
+ - ".gitignore"
122
+ - ".rspec"
123
+ - ".travis.yml"
124
+ - Gemfile
125
+ - Gemfile.lock
126
+ - LICENSE
127
+ - README.md
128
+ - Rakefile
129
+ - build.sh
130
+ - lib/salesforce-orm.rb
131
+ - lib/salesforce-orm/base.rb
132
+ - lib/salesforce-orm/configuration.rb
133
+ - lib/salesforce-orm/error.rb
134
+ - lib/salesforce-orm/object_base.rb
135
+ - lib/salesforce-orm/object_maker.rb
136
+ - lib/salesforce-orm/query_builder.rb
137
+ - lib/salesforce-orm/restforce_client.rb
138
+ - lib/salesforce-orm/sql_to_soql.rb
139
+ - lib/salesforce-orm/version.rb
140
+ - salesforce-orm.gemspec
141
+ homepage: https://github.com/NestAway/salesforce-orm
142
+ licenses:
143
+ - Apache License 2.0
144
+ metadata: {}
145
+ post_install_message:
146
+ rdoc_options: []
147
+ require_paths:
148
+ - lib
149
+ required_ruby_version: !ruby/object:Gem::Requirement
150
+ requirements:
151
+ - - ">="
152
+ - !ruby/object:Gem::Version
153
+ version: 1.9.8
154
+ required_rubygems_version: !ruby/object:Gem::Requirement
155
+ requirements:
156
+ - - ">="
157
+ - !ruby/object:Gem::Version
158
+ version: '0'
159
+ requirements: []
160
+ rubyforge_project:
161
+ rubygems_version: 2.2.2
162
+ signing_key:
163
+ specification_version: 4
164
+ summary: Ruby ORM for Salesforce
165
+ test_files: []