Class: Toast::Single

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

Overview

GET is the only allowed verb. To make changes the URI with ID has to be used.

Instance Attribute Summary collapse

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, params) ⇒ Single

Returns a new instance of Single.



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/toast/single.rb', line 16

def initialize model, subresource_name, params
        
  unless model.toast_config.singles.include? subresource_name
    raise ResourceNotFound
  end

  @model = model
  @single_finder = subresource_name
  @params = params
  @format = params[:format]
end

Instance Attribute Details

#modelObject (readonly)

Returns the value of attribute model.



14
15
16
# File 'lib/toast/single.rb', line 14

def model
  @model
end

Instance Method Details

#deleteObject

Raises:



65
66
67
# File 'lib/toast/single.rb', line 65

def delete
  raise MethodNotAllowed
end

#getObject

Raises:



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
53
54
55
# File 'lib/toast/single.rb', line 28

def get
  if @model.toast_config.in_collection.disallow_methods.include? "get"
    raise MethodNotAllowed 
  end
   
  record = if @model.toast_config.pass_params_to.include?(@single_finder) 
              @model.send(@single_finder, @params)
            else
              @model.send(@single_finder)
            end

  raise ResourceNotFound if record.nil?
  
  case @format
  when "html"
    {
      :template => "resources/#{model.to_s.underscore}",
      :locals => { model.to_s.pluralize.underscore.to_sym => record } 
    }
  when "json"        
    {
      :json => record.exposed_attributes,
      :status => :ok
    }
  else
    raise ResourceNotFound
  end
end

#post(payload) ⇒ Object

Raises:



61
62
63
# File 'lib/toast/single.rb', line 61

def post payload
  raise MethodNotAllowed
end

#putObject

Raises:



57
58
59
# File 'lib/toast/single.rb', line 57

def put
  raise MethodNotAllowed
end