databasedotcom 1.1.7 → 1.2.0
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.
- data/lib/databasedotcom/client.rb +37 -14
- data/lib/databasedotcom/sobject/sobject.rb +22 -9
- data/lib/databasedotcom/version.rb +1 -1
- metadata +2 -2
@@ -412,24 +412,47 @@ module Databasedotcom
|
|
412
412
|
|
413
413
|
def collection_from(response)
|
414
414
|
response = JSON.parse(response)
|
415
|
-
|
415
|
+
collection_from_hash( response )
|
416
|
+
end
|
417
|
+
|
418
|
+
# Converts a Hash of object data into a concrete SObject
|
419
|
+
def record_from_hash(data)
|
420
|
+
attributes = data.delete('attributes')
|
421
|
+
new_record = find_or_materialize(attributes["type"]).new
|
422
|
+
data.each do |name, value|
|
423
|
+
field = new_record.description['fields'].find do |field|
|
424
|
+
key_from_label(field["label"]) == name || field["name"] == name || field["relationshipName"] == name
|
425
|
+
end
|
426
|
+
|
427
|
+
# Field not found
|
428
|
+
if field == nil
|
429
|
+
break
|
430
|
+
end
|
431
|
+
|
432
|
+
# If reference/lookup field data was fetched, recursively build the child record and apply
|
433
|
+
if value.is_a?(Hash) and field['type'] == 'reference' and field["relationshipName"]
|
434
|
+
relation = record_from_hash( value )
|
435
|
+
set_value( new_record, field["relationshipName"], relation, 'reference' )
|
436
|
+
|
437
|
+
# Apply the raw value for all other field types
|
438
|
+
else
|
439
|
+
set_value(new_record, field["name"], value, field["type"]) if field
|
440
|
+
end
|
441
|
+
end
|
442
|
+
new_record
|
443
|
+
end
|
444
|
+
|
445
|
+
def collection_from_hash(data)
|
446
|
+
array_response = data.is_a?(Array)
|
416
447
|
if array_response
|
417
|
-
records =
|
448
|
+
records = data.collect { |rec| self.find(rec["attributes"]["type"], rec["Id"]) }
|
418
449
|
else
|
419
|
-
records =
|
420
|
-
|
421
|
-
new_record = find_or_materialize(attributes["type"]).new
|
422
|
-
record.each do |name, value|
|
423
|
-
field = new_record.description['fields'].find do |field|
|
424
|
-
key_from_label(field["label"]) == name || field["name"] == name
|
425
|
-
end
|
426
|
-
set_value(new_record, field["name"], value, field["type"]) if field
|
427
|
-
end
|
428
|
-
new_record
|
450
|
+
records = data["records"].collect do |record|
|
451
|
+
record_from_hash( record )
|
429
452
|
end
|
430
453
|
end
|
431
454
|
|
432
|
-
Databasedotcom::Collection.new(self, array_response ? records.length :
|
455
|
+
Databasedotcom::Collection.new(self, array_response ? records.length : data["totalSize"], array_response ? nil : data["nextRecordsUrl"]).concat(records)
|
433
456
|
end
|
434
457
|
|
435
458
|
def set_value(record, attr, value, attr_type)
|
@@ -492,7 +515,7 @@ module Databasedotcom
|
|
492
515
|
def query_org_id
|
493
516
|
query("select id from Organization")[0]["Id"]
|
494
517
|
end
|
495
|
-
|
518
|
+
|
496
519
|
def const_defined_in_module(mod, const)
|
497
520
|
mod.method(:const_defined?).arity == 1 ? mod.const_defined?(const) : mod.const_defined?(const, false)
|
498
521
|
end
|
@@ -127,7 +127,18 @@ module Databasedotcom
|
|
127
127
|
# client.materialize("Car")
|
128
128
|
# Car.attributes #=> ["Id", "Name", "Color", "Year"]
|
129
129
|
def self.attributes
|
130
|
-
self.description["fields"].collect { |f| f["name"] }
|
130
|
+
self.description["fields"].collect { |f| [f["name"], f["relationshipName"]] }.flatten.compact
|
131
|
+
end
|
132
|
+
|
133
|
+
def self.register_field( name, field )
|
134
|
+
attr_accessor name.to_sym
|
135
|
+
self.type_map[name] = {
|
136
|
+
:type => field["type"],
|
137
|
+
:label => field["label"],
|
138
|
+
:picklist_values => field["picklistValues"],
|
139
|
+
:updateable? => field["updateable"],
|
140
|
+
:createable? => field["createable"]
|
141
|
+
}
|
131
142
|
end
|
132
143
|
|
133
144
|
# Materializes the dynamically created Sobject class by adding all attribute accessors for each field as described in the description of the object on Force.com
|
@@ -139,16 +150,18 @@ module Databasedotcom
|
|
139
150
|
self.sobject_name = sobject_name
|
140
151
|
self.description = self.client.describe_sobject(self.sobject_name)
|
141
152
|
self.type_map = {}
|
153
|
+
|
142
154
|
self.description["fields"].each do |field|
|
155
|
+
|
156
|
+
# Register normal fields
|
143
157
|
name = field["name"]
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
}
|
158
|
+
register_field( field["name"], field )
|
159
|
+
|
160
|
+
# Register relationship fields.
|
161
|
+
if( field["type"] == "reference" and field["relationshipName"] )
|
162
|
+
register_field( field["relationshipName"], field )
|
163
|
+
end
|
164
|
+
|
152
165
|
end
|
153
166
|
end
|
154
167
|
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: databasedotcom
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 1.
|
5
|
+
version: 1.2.0
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Glenn Gillen, Danny Burkes & Richard Zhao
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-10-
|
13
|
+
date: 2011-10-22 00:00:00 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: multipart-post
|