Module: Sinatra::Resource

Defined in:
lib/sinatra/resource.rb

Constant Summary collapse

DEFAULT_FORMAT =
:html

Instance Method Summary collapse

Instance Method Details

#delete_with_formats(path, options = {}, &block) ⇒ Object



20
21
22
# File 'lib/sinatra/resource.rb', line 20

def delete_with_formats(path, options = {}, &block)
  route_method_for_formats :delete, path, options, &block
end

#get_with_formats(path, options = {}, &block) ⇒ Object



8
9
10
# File 'lib/sinatra/resource.rb', line 8

def get_with_formats(path, options = {}, &block)
  route_method_for_formats :get, path, options, &block
end

#post_with_formats(path, options = {}, &block) ⇒ Object



12
13
14
# File 'lib/sinatra/resource.rb', line 12

def post_with_formats(path, options = {}, &block)
  route_method_for_formats :post, path, options, &block
end

#put_with_formats(path, options = {}, &block) ⇒ Object



16
17
18
# File 'lib/sinatra/resource.rb', line 16

def put_with_formats(path, options = {}, &block)
  route_method_for_formats :put, path, options, &block
end

#resource(model, options = {}, &block) ⇒ Object



24
25
26
27
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
# File 'lib/sinatra/resource.rb', line 24

def resource(model, options = {}, &block)
  klass = Class.new

  klass.class_eval <<-"end_eval", __FILE__, __LINE__
    attr_accessor :app, :wants
    delegate :request, :response, :params, :session, :to => :app
    # Link in the various view renderers
    delegate :haml, :sass, :erb, :erubris, :builder, :less, :to => :app

    def self.controller_name
      "#{model.to_s.demodulize.downcase.pluralize}"
    end

    def self.model_name
      "#{model.to_s.demodulize.downcase}"
    end

    def self.model
      model
    end

    def respond_to
      yield wants
    end

    include ResourceMapper::Controller
  end_eval
  klass.class_eval &block if block_given?
  setup_routes(model, klass)
end