Sha256: b4ab2867895bc8d6fd05262d4c32e55b50f1a6f4c544c00c68e2160fd43b5079

Contents?: true

Size: 1.06 KB

Versions: 7

Compression:

Stored size: 1.06 KB

Contents

module Restforce
  class SObject < Restforce::Mash

    def sobject_type
      self.attributes.type
    end

    # Public: Get the describe for this sobject type
    def describe
      @client.describe(sobject_type)
    end

    # Public: Persist the attributes to Salesforce.
    #
    # Examples
    #
    #   account = client.query('select Id, Name from Account').first
    #   account.Name = 'Foobar'
    #   account.save
    def save
      # Remove 'attributes' and parent/child relationships. We only want to
      # persist the attributes on the sobject.
      ensure_id
      attrs = self.to_hash.reject { |key, _| key =~ /.*__r/ || key =~ /^attributes$/ }
      @client.update(sobject_type, attrs)
    end

    # Public: Destroy this record.
    #
    # Examples
    #
    #   account = client.query('select Id, Name from Account').first
    #   account.destroy
    def destroy
      ensure_id
      @client.destroy(sobject_type, self.Id)
    end

  private

    def ensure_id
      raise 'You need to query the Id for the record first.' unless self.Id?
    end

  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
restforce-0.1.1 lib/restforce/sobject.rb
restforce-0.1.0 lib/restforce/sobject.rb
restforce-0.0.8 lib/restforce/sobject.rb
restforce-0.0.7 lib/restforce/sobject.rb
restforce-0.0.6 lib/restforce/sobject.rb
restforce-0.0.5 lib/restforce/sobject.rb
restforce-0.0.4 lib/restforce/sobject.rb