Class: Toast::Record

Inherits:
Resource show all
Defined in:
lib/toast/record.rb

Instance Attribute Summary

Attributes inherited from Resource

#media_type

Instance Method Summary collapse

Methods inherited from Resource

#apply, build, get_class_by_resource_name

Constructor Details

#initialize(model, id) ⇒ Record

Returns a new instance of Record.



4
5
6
7
# File 'lib/toast/record.rb', line 4

def initialize model, id
  @model = model      
  @record = model.find(id) rescue raise(ResourceNotFound.new)
end

Instance Method Details

#deleteObject



44
45
46
47
48
49
# File 'lib/toast/record.rb', line 44

def delete
  @record.destroy
  {
    :status => :ok
  }
end

#getObject



37
38
39
40
41
42
# File 'lib/toast/record.rb', line 37

def get 
  {
    :json => @record.exposed_attributes,
    :status => :ok
  }
end

#post(payload) ⇒ Object

Raises:



9
10
11
# File 'lib/toast/record.rb', line 9

def post payload
  raise MethodNotAllowed
end

#put(payload) ⇒ Object

get, put, delete, post return a Hash that can be used as argument for ActionController#render



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/toast/record.rb', line 16

def put payload       
  if self.media_type != @model.toast_config.media_type
    raise UnsupportedMediaType
  end

  unless payload.is_a? Hash
    raise PayloadFormatError
  end

  if payload.keys.to_set != @model.toast_config.exposed_attributes.to_set
    raise PayloadInvalid
  end
  
  @record.update_attributes payload
  {
    :json => @record.exposed_attributes,
    :status => :ok,
    :location => @record.uri
  }
end