Class: Toast::RootCollection

Inherits:
Resource show all
Defined in:
lib/toast/root_collection.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, subresource_name) ⇒ RootCollection

Returns a new instance of RootCollection.



4
5
6
7
8
9
10
11
12
13
14
# File 'lib/toast/root_collection.rb', line 4

def initialize model, subresource_name
  
  subresource_name ||= "all"
  
  unless model.toast_config.collections.include? subresource_name
    raise ResourceNotFound
  end

  @model = model
  @collection = subresource_name
end

Instance Method Details

#deleteObject

Raises:



54
55
56
# File 'lib/toast/root_collection.rb', line 54

def delete
  raise MethodNotAllowed
end

#getObject



16
17
18
19
20
21
22
# File 'lib/toast/root_collection.rb', line 16

def get
  records = @model.send(@collection)
  {
    :json => records.map{|r| r.exposed_attributes(:in_collection => true)},
    :status => :ok
  }
end

#post(payload) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/toast/root_collection.rb', line 28

def post payload
  if @collection != "all"
    raise MethodNotAllowed
  end

  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 = @model.create payload
  
  {
    :json => record.exposed_attributes,
    :location => record.uri,
    :status => :created
  }
end

#putObject

Raises:



24
25
26
# File 'lib/toast/root_collection.rb', line 24

def put
  raise MethodNotAllowed
end