Class: ActiveResource::Simulator

Inherits:
Object
  • Object
show all
Defined in:
lib/active_resource/simulator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model) ⇒ Simulator

Returns a new instance of Simulator.



5
6
7
8
# File 'lib/active_resource/simulator.rb', line 5

def initialize(model)
  @model = model
  @@simulations = []
end

Instance Attribute Details

#modelObject (readonly)

Returns the value of attribute model.



10
11
12
# File 'lib/active_resource/simulator.rb', line 10

def model
  @model
end

#simulationsObject (readonly)

Returns the value of attribute simulations.



10
11
12
# File 'lib/active_resource/simulator.rb', line 10

def simulations
  @simulations
end

Instance Method Details

#create(body = nil, options = {}) ⇒ Object



21
22
23
24
25
# File 'lib/active_resource/simulator.rb', line 21

def create(body=nil, options={})
  path = options.delete(:path) || model.collection_path
  options.reverse_merge!({:status => 201})
  simulate_request(:post, path, body, options)
end

#destroy(param, options = {}) ⇒ Object



39
40
41
42
43
# File 'lib/active_resource/simulator.rb', line 39

def destroy(param, options={})
  path = options.delete(:path) || model.element_path(param)
  options.reverse_merge!({:status => 200})
  simulate_request(:delete, path, options[:body], options)
end

#show(param, body = nil, options = {}) ⇒ Object



27
28
29
30
31
# File 'lib/active_resource/simulator.rb', line 27

def show(param, body=nil, options={})
  path = options.delete(:path) || model.element_path(param)
  options.reverse_merge!({:status => 200})
  simulate_request(:get, path, body, options)
end

#simulate_request(method, path, body = nil, options = {}) ⇒ Object Also known as: simulate



13
14
15
16
# File 'lib/active_resource/simulator.rb', line 13

def simulate_request(method, path, body=nil, options={})
  @@simulations << prepare_simulation(method, path, body, options)
  load_simulations!
end

#update(param, options = {}) ⇒ Object



33
34
35
36
37
# File 'lib/active_resource/simulator.rb', line 33

def update(param, options={})
  path = options.delete(:path) || model.element_path(param)
  options.reverse_merge!({:status => 204})
  simulate_request(:put, path, options[:body], options)
end