Module: Sinatra::Rabbit

Included in:
CIMI::Collections::Base, Deltacloud::Collections::Base
Defined in:
lib/deltacloud/helpers/rabbit_helper.rb

Defined Under Namespace

Modules: URLHelper

Class Method Summary collapse

Class Method Details

.generate_url_helper_for(collection, operation) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# 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) if path.start_with?(root_url)
      url(path)
    end unless respond_to?(helper_method_name)
  end, helper_method_name]
end

.URLFor(collections) ⇒ Object



43
44
45
46
47
48
49
50
# 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