Class: Sinatra::Rabbit::BaseCollection

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

Direct Known Subclasses

Collection, Collection::Operation

Class Method Summary collapse

Methods inherited from Base

options

Class Method Details

.collection_class(name, parent_class = nil) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/sinatra/rabbit/base_collection.rb', line 51

def self.collection_class(name, parent_class=nil)
  if base_module = Rabbit::configuration[:base_module]
    begin
      base_module = base_module.const_get 'Rabbit'
    rescue NameError
      base_module = base_module.const_set('Rabbit', Module.new)
    end
  else
    base_module = Sinatra::Rabbit
  end
  klass = parent_class || base_module
  begin
    yield k = klass.const_get(name.to_s.camelize + 'Collection')
  rescue NameError
    yield k = klass.const_set(name.to_s.camelize + 'Collection', Class.new(Collection))
  end
  return k
end

.http_method_for(operation_name) ⇒ Object



46
47
48
49
# File 'lib/sinatra/rabbit/base_collection.rb', line 46

def self.http_method_for(operation_name)
  o = Sinatra::Rabbit::STANDARD_OPERATIONS[operation_name]
  (o && o[:method]) ? o[:method] : :get
end

.root_pathObject



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

def self.root_path
  Sinatra::Rabbit.configuration[:root_path] || '/'
end

.route_for(collection, operation_name = nil, member = :member) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/sinatra/rabbit/base_collection.rb', line 24

def self.route_for(collection, operation_name=nil, member=:member)
  unless operation_name.nil?
    o = Sinatra::Rabbit::STANDARD_OPERATIONS[operation_name]
    if o
      o = o.clone
      o[:member] = false if member == :no_member
      o[:collection] = true if member == :no_id
      if member == :no_id_and_member or member == :docs
        o[:collection] = true
        o[:member] = true
      end
    end
    operation_path = (o && o[:member]) ? operation_name.to_s : nil
    operation_path = operation_name.to_s unless o
    id_param = (o && o[:collection]) ? nil : (member.kind_of?(Hash) ? member[:id_name] : ":id")
    id_param.gsub!(':', '') if id_param and member  == :docs
    [route_for(collection), id_param, operation_path].compact.join('/')
  else
    collection.to_s
  end
end