Class: Sinatra::Rabbit::Operation
Constant Summary
collapse
- STANDARD =
{
:index => { :method => :get, :member => false },
:show => { :method => :get, :member => true },
:create => { :method => :post, :member => false },
:update => { :method => :put, :member => true },
:destroy => { :method => :delete, :member => true }
}
Instance Attribute Summary collapse
Instance Method Summary
collapse
#add_params, #each_param, #param, #params, #validate
Constructor Details
#initialize(coll, name, opts, &block) ⇒ Operation
Returns a new instance of Operation.
26
27
28
29
30
31
32
33
34
35
36
|
# File 'lib/sinatra/rabbit.rb', line 26
def initialize(coll, name, opts, &block)
@name = name.to_sym
opts = STANDARD[@name].merge(opts) if standard?
@collection = coll
raise "No method for operation #{name}" unless opts[:method]
@method = opts[:method].to_sym
@member = opts[:member]
@description = ""
instance_eval(&block) if block_given?
generate_documentation
end
|
Instance Attribute Details
#method ⇒ Object
Returns the value of attribute method.
14
15
16
|
# File 'lib/sinatra/rabbit.rb', line 14
def method
@method
end
|
#name ⇒ Object
Returns the value of attribute name.
14
15
16
|
# File 'lib/sinatra/rabbit.rb', line 14
def name
@name
end
|
Instance Method Details
#control(&block) ⇒ Object
58
59
60
61
62
63
64
|
# File 'lib/sinatra/rabbit.rb', line 58
def control(&block)
op = self
@control = Proc.new do
op.validate(params)
instance_eval(&block)
end
end
|
#description(text = "") ⇒ Object
42
43
44
45
|
# File 'lib/sinatra/rabbit.rb', line 42
def description(text="")
return @description if text.blank?
@description = text
end
|
#generate ⇒ Object
84
85
86
87
88
89
90
91
92
93
94
|
# File 'lib/sinatra/rabbit.rb', line 84
def generate
::Sinatra::Application.send(@method, path, {}, &@control)
if name == :index
gen_route "#{@collection.name}_url"
elsif name == :show
gen_route "#{@collection.name.to_s.singularize}_url"
else
gen_route "#{name}_#{@collection.name.to_s.singularize}_url"
end
end
|
#generate_documentation ⇒ Object
47
48
49
50
51
52
53
54
55
56
|
# File 'lib/sinatra/rabbit.rb', line 47
def generate_documentation
coll, oper = @collection, self
::Sinatra::Application.get("/api/docs/#{@collection.name}/#{@name}") do
@collection, @operation = coll, oper
respond_to do |format|
format.html { haml :'docs/operation' }
format.xml { haml :'docs/operation' }
end
end
end
|
#path(args = {}) ⇒ Object
71
72
73
74
75
76
77
78
79
80
81
82
|
# File 'lib/sinatra/rabbit.rb', line 71
def path(args = {})
l_prefix = args[:prefix] ? args[:prefix] : prefix
if @member
if standard?
"#{l_prefix}/#{@collection.name}/:id"
else
"#{l_prefix}/#{@collection.name}/:id/#{name}"
end
else
"#{l_prefix}/#{@collection.name}"
end
end
|
#prefix ⇒ Object
66
67
68
69
|
# File 'lib/sinatra/rabbit.rb', line 66
def prefix
"/api"
end
|
#standard? ⇒ Boolean
38
39
40
|
# File 'lib/sinatra/rabbit.rb', line 38
def standard?
STANDARD.keys.include?(name)
end
|