Module: RestforceMock::Sandbox

Included in:
Client
Defined in:
lib/restforce_mock/sandbox.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.add_object(name, id, values) ⇒ Object



4
5
6
7
8
9
# File 'lib/restforce_mock/sandbox.rb', line 4

def self.add_object(name, id, values)
  if storage[name] && !storage[name][id].nil?
    raise "Object #{name} with #{id} exists"
  end
  storage[name].merge!({ id  => values })
end

.clientObject



67
68
69
# File 'lib/restforce_mock/sandbox.rb', line 67

def self.client
  ::Restforce.new
end

.find_object(query) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/restforce_mock/sandbox.rb', line 27

def self.find_object(query)
  # This will only support making queries in this format:
  # client.query("Select Id FROM Contact WHERE Email = '[email protected]'")

  split_query = query.split

  storage_name = split_query[3]
  key = "#{split_query[5]}".to_sym
  val = split_query.last
              .gsub(/^'|'$/, '')
              .gsub('\\', '')

  if storage[storage_name].has_value?( key => val)
    return storage[storage_name].keys.first
  end
end

.get_object(name, id) ⇒ Object



19
20
21
# File 'lib/restforce_mock/sandbox.rb', line 19

def self.get_object(name, id)
  storage[name][id]
end

.initializeObject



71
72
73
74
75
76
77
78
# File 'lib/restforce_mock/sandbox.rb', line 71

def self.initialize
  storage = Hash.new do |hash, object|
    hash[object]={}
  end
  storage[:schema] = Hash.new do |hash, object|
    hash[object]={}
  end
end

.reset!Object



53
54
55
# File 'lib/restforce_mock/sandbox.rb', line 53

def self.reset!
  $restforce_mock_storage = initialize
end

.storageObject



57
58
59
# File 'lib/restforce_mock/sandbox.rb', line 57

def self.storage
  $restforce_mock_storage ||= initialize
end

.update_object(name, id, attrs) ⇒ Object



44
45
46
47
# File 'lib/restforce_mock/sandbox.rb', line 44

def self.update_object(name, id, attrs)
  current = storage[name][id]
  storage[name][id] = current.merge(attrs)
end

.update_schema(object_name) ⇒ Object

Private



62
63
64
65
# File 'lib/restforce_mock/sandbox.rb', line 62

def self.update_schema(object_name)
  s = RestforceMock::SchemaManager.new
  storage[:schema][object_name] = s.get_schema(object_name)
end

.validate_all_present_fields!(current, attrs) ⇒ Object



80
81
82
83
84
85
86
87
88
# File 'lib/restforce_mock/sandbox.rb', line 80

def self.validate_all_present_fields!(current, attrs)
  missing = attrs.keys - current.keys
  unless missing.length == 0
    raise Faraday::Error::ResourceNotFound.new(
      "INVALID_FIELD_FOR_INSERT_UPDATE: Unable to create/update fields: #{missing}." +
      " Please check the security settings of this field and verify that it is " +
      "read/write for your profile or permission set")
  end
end

Instance Method Details

#add_object(name, id, values) ⇒ Object



11
12
13
# File 'lib/restforce_mock/sandbox.rb', line 11

def add_object(name, id, values)
  RestforceMock::Sandbox.add_object(name, id, values)
end

#get_object(name, id) ⇒ Object



15
16
17
# File 'lib/restforce_mock/sandbox.rb', line 15

def get_object(name, id)
  RestforceMock::Sandbox.get_object(name, id)
end

#get_object_from_query(query) ⇒ Object



23
24
25
# File 'lib/restforce_mock/sandbox.rb', line 23

def get_object_from_query(query)
  RestforceMock::Sandbox.find_object(query)
end

#update_object(name, id, attrs) ⇒ Object



49
50
51
# File 'lib/restforce_mock/sandbox.rb', line 49

def update_object(name, id, attrs)
  RestforceMock::Sandbox.update_object(name, id, attrs)
end