module Sinatra::Rabbit

Public Class Methods

URLFor(collections) click to toggle source
# File lib/deltacloud/helpers/rabbit_helper.rb, line 43
def self.URLFor(collections)
  collections.each do |c|
    c.operations.each do |operation|
      URLHelper.instance_eval(&generate_url_helper_for(c, operation)[0])
    end
  end
  URLHelper
end
generate_url_helper_for(collection, operation) click to toggle source
# File lib/deltacloud/helpers/rabbit_helper.rb, line 52
def self.generate_url_helper_for(collection, operation)
  operation_name = operation.operation_name.to_s
  collection_name = collection.collection_name.to_s

  # Construct OPERATION_COLLECTION_URL helper
  # The :index and :create operation does not get any prefix
  #

  helper_method_name = case operation_name
                       when 'index' then collection_name
                       when 'show' then collection_name.singularize
                       else operation_name + '_' + collection_name.singularize
                       end

  helper_method_name += '_url'

  [Proc.new do
    define_method helper_method_name do |*args|
      if (opts = args.first).kind_of? Hash
        path = operation.full_path.convert_query_params(opts)
      elsif !args.empty? and (obj_id = args.first)
        path = operation.full_path.convert_query_params(:id => obj_id)
      elsif operation_name == 'show'
        path = collection.operation(:index).full_path
      else
        path = operation.full_path
      end
      path.slice!(root_url)
      url(path)
    end unless respond_to?(helper_method_name)
  end, helper_method_name]
end