Module: Sinatra::REST

Defined in:
lib/sinatra/rest.rb

Defined Under Namespace

Modules: Helpers Classes: CustomController

Instance Method Summary collapse

Instance Method Details

#rest(model_class, options = {}, &block) ⇒ Object

adds restful routes and url helpers for the model



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

def rest(model_class, options={}, &block)
  parse_args(model_class, options)
  read_config('rest/rest.yaml')

  # register model specific helpers
  helpers generate_helpers
  
  # create an own module, to override the template with custom methods
  # this way, you can still use #super# in the overridden methods
  controller = generate_controller
  if block_given?
    custom = CustomController.new(@plural)
    custom.instance_eval &block
    custom.module.send :include, controller
    controller = custom.module
  end
  helpers controller
  
  # register routes as DSL extension
  instance_eval generate_routes
end