# File lib/sinatra/rabbit/base.rb, line 220 def self.[](obj_id) collections.find { |c| c.collection_name == obj_id } || operation(obj_id) end
# File lib/sinatra/rabbit/base.rb, line 266 def self.action(action_name, opts={}, &block) opts.merge!(:http_method => :post) unless opts[:http_method] operation(action_name, opts, &block) end
# File lib/sinatra/rabbit/base.rb, line 196 def self.base_class @klass end
# File lib/sinatra/rabbit/base.rb, line 166 def self.base_class=(klass) @klass = klass end
Define new collection using the name
opts Define :id used if the collection is a subcollection
# File lib/sinatra/rabbit/base.rb, line 145 def self.collection(name, opts={}, &block) return collections.find { |c| c.collection_name == name } if not block_given? new_collection = BaseCollection.collection_class(name, self) do |c| c.base_class = self.base_class c.with_id!(opts.delete(:with_id)) if opts.has_key?(:with_id) c.no_member! if opts.has_key?(:no_member) c.generate(name, self, &block) end collections << new_collection end
# File lib/sinatra/rabbit/base.rb, line 200 def self.collection_name; @collection_name; end
# File lib/sinatra/rabbit/base.rb, line 207 def self.collections @collections ||= [] end
# File lib/sinatra/rabbit/base.rb, line 170 def self.control(*args) raise "The 'control' statement must be used only within context of Operation" end
# File lib/sinatra/rabbit/base.rb, line 215 def self.description(text=nil) return @description if text.nil? @description = text end
# File lib/sinatra/rabbit/base.rb, line 128 def self.docs_url root_path + 'docs/' + route_for(path) end
# File lib/sinatra/rabbit/base.rb, line 179 def self.feature(name) features.find { |f| f.name == name } end
# File lib/sinatra/rabbit/base.rb, line 174 def self.features return [] unless base_class.respond_to? :features base_class.features.select { |f| f.collection == collection_name } end
# File lib/sinatra/rabbit/base.rb, line 275 def self.features_for(operation_name) features.select { |f| f.operations.map { |o| o.name}.include?(operation_name) } end
# File lib/sinatra/rabbit/base.rb, line 211 def self.full_path root_path + route_for(path) end
# File lib/sinatra/rabbit/base.rb, line 105 def self.generate(name, parent_collection=nil, &block) @collection_name = name.to_sym @parent_collection = parent_collection class_eval(&block) generate_head_route unless Rabbit.disabled? :head_routes generate_option_route unless Rabbit.disabled? :options_routes generate_docs_route unless Rabbit.disabled? :docs self end
# File lib/sinatra/rabbit/base.rb, line 132 def self.generate_docs_route collection = self base_class.get docs_url do css_file = File.read(File.join(File.dirname(__FILE__), '..', 'docs', 'bootstrap.min.css')) collection_file = File.read(File.join(File.dirname(__FILE__), '..', 'docs', 'collection.haml')) haml collection_file, :locals => { :collection => collection, :css => css_file } end end
# File lib/sinatra/rabbit/base.rb, line 122 def self.generate_head_route base_class.head full_path do status 200 end end
# File lib/sinatra/rabbit/base.rb, line 115 def self.generate_option_route base_class.options '/docs' + full_path do header 'Allow' => operations.map { |o| o.operation_name }.join(',') status 200 end end
# File lib/sinatra/rabbit/base.rb, line 187 def self.no_member! @no_member = true end
# File lib/sinatra/rabbit/base.rb, line 224 def self.operation(operation_name, opts={}, &block) # Return operation when no block is given return operations.find { |o| o.operation_name == operation_name } unless block_given? # Check if current operation is not already registred if operation_registred?(operation_name) raise "Operation #{operation_name} already registered in #{self.name} collection" end # Create operation class new_operation = operation_class(self, operation_name).generate(self, operation_name, opts, &block) operations << new_operation # Add route conditions if defined if opts.has_key? :if base_class.send(:set, :if_true) do |value| condition do (value.kind_of?(Proc) ? value.call : value) == true end end opts[:if_true] = opts.delete(:if) end # Make the full_path method on operation return currect operation path new_operation.route = root_path + route_for(path, operation_name, :id_name => @with_id || ':id') # Change the HTTP method to POST automatically for 'action' operations new_operation.http_method = opts.delete(:http_method) if opts[:http_method] # Remove :with_capability from Sinatra route options route_options = opts.clone route_options.delete :with_capability # Define Sinatra route and generate OPTIONS route if enabled base_class.send(new_operation.http_method || http_method_for(operation_name), new_operation.full_path, route_options, &new_operation.control) new_operation.generate_option_route(root_path + route_for(path, operation_name, :no_id_and_member)) unless Rabbit.disabled?(:options_routes) new_operation.generate_head_route(root_path + route_for(path, operation_name, :member)) unless Rabbit.disabled?(:head_routes) new_operation.generate_docs_route(new_operation.docs_url) unless Rabbit.disabled?(:doc_routes) self end
# File lib/sinatra/rabbit/base.rb, line 271 def self.operations @operations ||= [] end
# File lib/sinatra/rabbit/base.rb, line 201 def self.parent_collection; @parent_collection; end
# File lib/sinatra/rabbit/base.rb, line 156 def self.parent_routes return '' if @parent_collection.nil? route = [ @parent_collection.collection_name.to_s ] current_parent = @parent_collection while current_parent = current_parent.parent_collection route << current_parent.collection_name end route.reverse.join('/')+'/' end
# File lib/sinatra/rabbit/base.rb, line 191 def self.path with_id_param = @with_id.nil? ? '' : ':id' + (@no_member ? '' : '/') parent_routes + with_id_param + ((@no_member) ? '' : collection_name.to_s) end
# File lib/sinatra/rabbit/base.rb, line 203 def self.subcollection? !parent_collection.nil? end
# File lib/sinatra/rabbit/base.rb, line 183 def self.with_id!(id) @with_id = ":#{id}" end