Class: SalesforceOrm::ObjectBase

Inherits:
OpenStruct
  • Object
show all
Extended by:
ObjectMaker
Defined in:
lib/salesforce-orm/object_base.rb

Direct Known Subclasses

SalesforceOrm::Object::RecordType

Constant Summary

Constants included from ObjectMaker

SalesforceOrm::ObjectMaker::DEFAULT_DATA_TYPE_MAP, SalesforceOrm::ObjectMaker::DEFAULT_FIELD_MAP

Constants included from RecordTypeManager

RecordTypeManager::FIELD_NAME

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ObjectMaker

data_type_map, data_type_map=, field_map, field_map=, object_name, object_name=

Methods included from RecordTypeManager

#record_type, #record_type=, #record_type_id

Class Method Details

.find(*args) ⇒ Object



38
39
40
# File 'lib/salesforce-orm/object_base.rb', line 38

def find(*args)
  find_by_id(*args)
end

.method_missing(method, *args, &block) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/salesforce-orm/object_base.rb', line 42

def method_missing(method, *args, &block)
  regex = /^find_by_(.+)$/
  if method =~ regex
    fields = method.to_s.match(regex).captures[0].split('_and_')
    condition = {}
    fields.each_with_index do |field, index|
      condition[field.to_sym] = args[index]
    end
    where(condition).first
  end
end

.ormObject



54
55
56
# File 'lib/salesforce-orm/object_base.rb', line 54

def orm
  Base.new(self)
end

Instance Method Details

#inspectObject



72
73
74
# File 'lib/salesforce-orm/object_base.rb', line 72

def inspect
  to_h
end

#marshal_dumpObject

marshal_dump and marshal_load is a fix for unable to cache object of this class. This is a temporary solution. Once Restforce::Mash fix this issue, we’ll revert this change



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/salesforce-orm/object_base.rb', line 79

def marshal_dump
  h = to_h
  if h[:attributes]
    h[:attributes] = h[:attributes].clone
    h[:attributes].instance_variable_set(:@client, nil)
  end

  if h[:original_object]
    h[:original_object] = h[:original_object].clone
    h[:original_object].instance_variable_set(:@client, nil)

    if h[:original_object]['attributes']
      h[:original_object]['attributes'] = h[:original_object]['attributes'].clone
      h[:original_object]['attributes'].instance_variable_set(:@client, nil)
    end
  end
  h
end

#marshal_load(data) ⇒ Object



98
99
100
101
102
103
104
105
106
# File 'lib/salesforce-orm/object_base.rb', line 98

def marshal_load(data)
  result = super(data)

  attributes.instance_variable_set(:@client, RestforceClient.instance) if attributes
  original_object.instance_variable_set(:@client, RestforceClient.instance) if original_object
  original_object.attributes.instance_variable_set(:@client, RestforceClient.instance) if original_object && original_object.attributes

  result
end

#to_hashObject



68
69
70
# File 'lib/salesforce-orm/object_base.rb', line 68

def to_hash
  to_h
end