Class: Sinatra::Rabbit::Operation

Inherits:
Object
  • Object
show all
Includes:
Deltacloud::BackendCapability, Deltacloud::Validation
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

Attributes included from Deltacloud::BackendCapability

#capability

Instance Method Summary collapse

Methods included from Deltacloud::Validation

#add_params, #each_param, #param, #params, #validate

Methods included from Deltacloud::BackendCapability

#check_capability, #has_capability?, #with_capability

Constructor Details

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

Returns a new instance of Operation.



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/sinatra/rabbit.rb', line 28

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

#methodObject (readonly)

Returns the value of attribute method.



15
16
17
# File 'lib/sinatra/rabbit.rb', line 15

def method
  @method
end

#nameObject (readonly)

Returns the value of attribute name.



15
16
17
# File 'lib/sinatra/rabbit.rb', line 15

def name
  @name
end

Instance Method Details

#control(&block) ⇒ Object



60
61
62
63
64
65
66
67
# File 'lib/sinatra/rabbit.rb', line 60

def control(&block)
  op = self
  @control = Proc.new do
    op.check_capability(Deltacloud::driver)
    op.validate(params)
    instance_eval(&block)
  end
end

#description(text = "") ⇒ Object



44
45
46
47
# File 'lib/sinatra/rabbit.rb', line 44

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

#generateObject



87
88
89
90
91
92
93
94
95
96
97
# File 'lib/sinatra/rabbit.rb', line 87

def generate
  ::Sinatra::Application.send(@method, path, {}, &@control)
  # Set up some Rails-like URL helpers
  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_documentationObject



49
50
51
52
53
54
55
56
57
58
# File 'lib/sinatra/rabbit.rb', line 49

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



74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/sinatra/rabbit.rb', line 74

def path(args = {})
  l_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



69
70
71
72
# File 'lib/sinatra/rabbit.rb', line 69

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

#standard?Boolean

Returns:

  • (Boolean)


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

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