databasedotcom 1.1.7 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -412,24 +412,47 @@ module Databasedotcom
412
412
 
413
413
  def collection_from(response)
414
414
  response = JSON.parse(response)
415
- array_response = response.is_a?(Array)
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 = response.collect { |rec| self.find(rec["attributes"]["type"], rec["Id"]) }
448
+ records = data.collect { |rec| self.find(rec["attributes"]["type"], rec["Id"]) }
418
449
  else
419
- records = response["records"].collect do |record|
420
- attributes = record.delete('attributes')
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 : response["totalSize"], array_response ? nil : response["nextRecordsUrl"]).concat(records)
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
- attr_accessor name.to_sym
145
- self.type_map[name] = {
146
- :type => field["type"],
147
- :label => field["label"],
148
- :picklist_values => field["picklistValues"],
149
- :updateable? => field["updateable"],
150
- :createable? => field["createable"]
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
 
@@ -1,3 +1,3 @@
1
1
  module Databasedotcom
2
- VERSION = "1.1.7"
2
+ VERSION = "1.2.0"
3
3
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: databasedotcom
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 1.1.7
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-20 00:00:00 Z
13
+ date: 2011-10-22 00:00:00 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: multipart-post