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

#base_uri, #payload_content_type, #prefered_media_type

Instance Method Summary collapse

Methods inherited from Resource

#apply, build, get_class_by_resource_name

Constructor Details

#initialize(model, subresource_name, params, config_in, config_out) ⇒ Single

Returns a new instance of Single.

Raises:



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/toast/single.rb', line 16

def initialize model, subresource_name, params, config_in, config_out
  @config_in = config_in
  @config_out = config_out

  unless @config_out.singles.include? subresource_name
    raise ResourceNotFound
  end

  @model = model
  @params = params
  @format = params[:format]

  unless @model.respond_to?(subresource_name)
    raise "Toast Error: Cannot find class method '#{@model}.#{subresource_name}', which is configured in 'acts_as_resource > singles'."
  end

  @record = if @config_out.pass_params_to.include?(subresource_name)
              if @model.method(subresource_name).arity != 1
                raise "Toast Error: Class method '#{@model}.#{subresource_name}' must accept one parameter, as configured by 'acts_as_resource > pass_params_to'."
              end
              @model.send(subresource_name, @params)
            else
              if(@model.method(subresource_name).arity < -1 or 
                 @model.method(subresource_name).arity > 0)
                raise "Toast Error: Class method '#{@model}.#{subresource_name}' must be callable w/o parameters"
              end
              @model.send(subresource_name)
            end
  
  raise ResourceNotFound if @record.nil?      
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:



77
78
79
# File 'lib/toast/single.rb', line 77

def delete
  raise MethodNotAllowed
end

#getObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/toast/single.rb', line 48

def get
  case @format
  when "html"
    {
      :template => "resources/#{model.to_s.underscore}",
      :locals => { model.to_s.pluralize.underscore.to_sym => @record }
    }
  when "json"
    {
      :json => @record.represent( @config_out.exposed_attributes,
                                  @config_out.exposed_associations,
                                  @base_uri,
                                  @config_out.media_type),
      :status => :ok
    }
  else
    raise ResourceNotFound
  end
end

Raises:



81
82
83
# File 'lib/toast/single.rb', line 81

def link l
  raise MethodNotAllowed
end

#post(payload) ⇒ Object

Raises:



73
74
75
# File 'lib/toast/single.rb', line 73

def post payload
  raise MethodNotAllowed
end

#putObject

Raises:



69
70
71
# File 'lib/toast/single.rb', line 69

def put
  raise MethodNotAllowed
end

Raises:



85
86
87
# File 'lib/toast/single.rb', line 85

def unlink l
  raise MethodNotAllowed
end