lib/databasedotcom/sobject/sobject.rb in databasedotcom-1.2.2 vs lib/databasedotcom/sobject/sobject.rb in databasedotcom-1.2.3
- old
+ new
@@ -17,10 +17,11 @@
self.send("#{field["name"]}=", field["defaultValueFormula"])
end
self.attributes=(attrs)
end
+ # Set attributes of this object, from a hash, in bulk
def attributes=(attrs)
attrs.each do |key, value|
self.send("#{key}=", value)
end
end
@@ -111,14 +112,16 @@
def reload
self.attributes = self.class.find(self.Id).attributes
self
end
+ # Get a named attribute on this object
def [](attr_name)
self.send(attr_name) rescue nil
end
+ # Set a named attribute on this object
def []=(attr_name, value)
raise ArgumentError.new("No attribute named #{attr_name}") unless self.class.attributes.include?(attr_name)
self.send("#{attr_name}=", value)
end
@@ -128,42 +131,31 @@
# Car.attributes #=> ["Id", "Name", "Color", "Year"]
def self.attributes
self.description["fields"].collect { |f| [f["name"], f["relationshipName"]] }.flatten.compact
end
- def self.register_field( name, field )
- attr_accessor name.to_sym
- self.type_map[name] = {
- :type => field["type"],
- :label => field["label"],
- :picklist_values => field["picklistValues"],
- :updateable? => field["updateable"],
- :createable? => field["createable"]
- }
- end
-
# 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
def self.materialize(sobject_name)
self.cattr_accessor :description
self.cattr_accessor :type_map
self.cattr_accessor :sobject_name
self.sobject_name = sobject_name
self.description = self.client.describe_sobject(self.sobject_name)
self.type_map = {}
-
+
self.description["fields"].each do |field|
-
+
# Register normal fields
name = field["name"]
register_field( field["name"], field )
-
+
# Register relationship fields.
if( field["type"] == "reference" and field["relationshipName"] )
register_field( field["relationshipName"], field )
end
-
+
end
end
# Returns the Force.com type of the attribute +attr_name+. Raises ArgumentError if attribute does not exist.
#
@@ -241,11 +233,11 @@
# Delegates to Client.delete with arguments +record_id+ and self
def self.delete(record_id)
self.client.delete(self.sobject_name, record_id)
end
-
+
# Get the total number of records
def self.count
self.client.query("SELECT COUNT() FROM #{self.sobject_name}").total_size
end
@@ -269,11 +261,11 @@
attrs_and_values_for_find[attr] = value
attrs_and_values_for_write[attr] = value unless hash_args
end
limit_clause = method_name.to_s.include?('_all_by_') ? "" : " LIMIT 1"
-
+
results = self.client.query("SELECT #{self.field_list} FROM #{self.sobject_name} WHERE #{soql_conditions_for(attrs_and_values_for_find)}#{limit_clause}")
results = limit_clause == "" ? results : results.first rescue nil
if results.nil?
if method_name.to_s =~ /^find_or_create_by_(.+)$/
@@ -312,29 +304,42 @@
end
end
private
+ def self.register_field( name, field )
+ public
+ attr_accessor name.to_sym
+ private
+ self.type_map[name] = {
+ :type => field["type"],
+ :label => field["label"],
+ :picklist_values => field["picklistValues"],
+ :updateable? => field["updateable"],
+ :createable? => field["createable"]
+ }
+ end
+
def self.field_list
self.description['fields'].collect { |f| f['name'] }.join(',')
end
def self.type_map_attr(attr_name, key)
raise ArgumentError.new("No attribute named #{attr_name}") unless self.type_map.has_key?(attr_name)
self.type_map[attr_name][key]
end
-
+
def self.soql_conditions_for(params)
params.inject([]) do |arr, av|
case av[1]
when String
value_str = "'#{av[1].gsub("'", "\\\\'")}'"
when DateTime, Time
value_str = av[1].strftime("%Y-%m-%dT%H:%M:%S.%L%z").insert(-3, ":")
else
value_str = av[1].to_s
end
-
+
arr << "#{av[0]} = #{value_str}"
arr
end.join(" AND ")
end
end