Class: RForce::Wrapper::Types::SObject

Inherits:
Object
  • Object
show all
Defined in:
lib/rforce-wrapper/types/sobject.rb

Overview

Represents an sObject in the Salesforce database.

Constant Summary

INVALID_FIELDS =

Fields that are transformed into calls to attribute methods if they are set with #[]= or retrieved with #[].

['type', 'fieldsToNull', 'id']

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (SObject) initialize(type, fieldsToNull = nil, id = nil)

Creates a new sObject with the given parameters and empty fields.



33
34
35
36
37
38
39
# File 'lib/rforce-wrapper/types/sobject.rb', line 33

def initialize(type, fieldsToNull = nil, id = nil)
  @type         = type
  @fieldsToNull = fieldsToNull
  @fieldsToNull = @fieldsToNull.join(", ") if @fieldsToNull.is_a? Array
  @id           = id
  @fields       = {}
end

Instance Attribute Details

- (String) fieldsToNull



13
14
15
# File 'lib/rforce-wrapper/types/sobject.rb', line 13

def fieldsToNull
  @fieldsToNull
end

- (String) id



16
17
18
# File 'lib/rforce-wrapper/types/sobject.rb', line 16

def id
  @id
end

- (String) type



7
8
9
# File 'lib/rforce-wrapper/types/sobject.rb', line 7

def type
  @type
end

Class Method Details

+ (String) make_indifferent_key(key)

Modifies the given string or symbol into a string with the first letter capitalized. For use with hashes to ensure all the keys are the same format.



112
113
114
115
116
117
# File 'lib/rforce-wrapper/types/sobject.rb', line 112

def self.make_indifferent_key(key)
  key = key.to_s
  key.sub /^(.){1}/ do |match|
    $1.capitalize
  end
end

Instance Method Details

- (String) [](key)

Returns the value of a field on the sObject. This method is subject to all the same features and restrictions of #[]=.



68
69
70
71
72
# File 'lib/rforce-wrapper/types/sobject.rb', line 68

def [](key)
  key = self.class.make_indifferent_key(key)
  return get_attribute(key) if INVALID_FIELDS.map{ |f| f.to_s.downcase }.include? key.downcase
  @fields[key]
end

- (nil) []=(key, value)

Sets the value of a field on the sObject. Setting type, id, or fieldsToNull with this method calls the appropriate attribute accessor on the sObject. The method is written such that the following four keys are equal to each other: 'FirstName', 'firstName', :FirstName, :firstName. The case of every character after the first, however, must match.



56
57
58
59
60
61
# File 'lib/rforce-wrapper/types/sobject.rb', line 56

def []=(key, value)
  key = self.class.make_indifferent_key(key)
  value = value.to_s unless value.is_a? String
  return set_attribute(key, value) if INVALID_FIELDS.map{ |f| f.to_s.downcase }.include? key.downcase
  @fields[key] = value
end

- (Object) get_attribute(key)

Returns the value of the given attribute on the object if the key is included in INVALID_FIELDS. Ensures proper case of the attribute. Used by #[].

See Also:



97
98
99
100
101
102
103
104
# File 'lib/rforce-wrapper/types/sobject.rb', line 97

def get_attribute(key)
  INVALID_FIELDS.each do |field|
    if field.downcase == key.downcase
      method = "#{field}".to_sym
      return self.send(method)
    end
  end
end

- (nil) set_attribute(key, value)

Sets the given attribute on the object to the given value if the key is included in INVALID_FIELDS. Ensures proper case of the attribute. Used by #[]=.

See Also:



81
82
83
84
85
86
87
88
# File 'lib/rforce-wrapper/types/sobject.rb', line 81

def set_attribute(key, value)
  INVALID_FIELDS.each do |field|
    if field.downcase == key.downcase
      method = "#{field}=".to_sym
      return self.send(method, value)
    end
  end
end

- (Hash) to_hash

Returns a hash representation of the SObject for use in RForce SOAP calls.



123
124
125
126
127
128
129
# File 'lib/rforce-wrapper/types/sobject.rb', line 123

def to_hash
  hash = @fields.clone
  hash.merge! :type => @type
  hash.merge! :Id => @id unless @id.nil?
  hash.merge! :fieldsToNull => @fieldsToNull unless @fieldsToNull.nil? || @fieldsToNull.empty?
  hash
end