Class: Sinatra::Rabbit::Operation

Inherits:
Object
  • Object
show all
Defined in:
lib/sinatra/rabbit.rb

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

Constructor Details

#initialize(coll, name, opts, &block) ⇒ Operation

Returns a new instance of Operation.



23
24
25
26
27
28
29
30
31
32
# File 'lib/sinatra/rabbit.rb', line 23

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?
end

Instance Attribute Details

#methodObject (readonly)

Returns the value of attribute method.



13
14
15
# File 'lib/sinatra/rabbit.rb', line 13

def method
  @method
end

#nameObject (readonly)

Returns the value of attribute name.



13
14
15
# File 'lib/sinatra/rabbit.rb', line 13

def name
  @name
end

Instance Method Details

#authz(&blk) ⇒ Object



95
96
# File 'lib/sinatra/rabbit.rb', line 95

def authz(&blk)
end

#control(&block) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/sinatra/rabbit.rb', line 55

def control(&block)
  op = self
  @control = Proc.new do
    #op.validate(params)
    instance_eval(&block)
  end
end

#description(text = "") ⇒ Object



38
39
40
41
# File 'lib/sinatra/rabbit.rb', line 38

def description(text="")
  return @description if text.blank?
  @description = text
end

#generate(app) ⇒ Object

Raises:

  • (ArgumentError)


81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/sinatra/rabbit.rb', line 81

def generate(app)
  raise ArgumentError unless app < Sinatra::Base
  
  app.send(@method, path, {}, &@control)
  # Set up some Rails-like URL helpers
  if name == :index
    gen_route "#{@collection.name}_url", app
  elsif name == :show
    gen_route "#{@collection.name.to_s.singularize}_url", app
  else
    gen_route "#{name}_#{@collection.name.to_s.singularize}_url", app
  end
end

#generate_documentation(app) ⇒ Object

Raises:

  • (ArgumentError)


43
44
45
46
47
48
49
50
51
52
53
# File 'lib/sinatra/rabbit.rb', line 43

def generate_documentation(app)
  raise ArgumentError unless app < Sinatra::Base
  coll, oper = @collection, self
  app.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



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/sinatra/rabbit.rb', line 68

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

#prefixObject



63
64
65
66
# File 'lib/sinatra/rabbit.rb', line 63

def prefix
  # FIXME: Make the /api prefix configurable
  "/api"
end

#standard?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/sinatra/rabbit.rb', line 34

def standard?
  STANDARD.keys.include?(name)
end