removing log dir from .gitignore
[monkeycharger.git] / vendor / rails / actionwebservice / lib / action_web_service / container / direct_container.rb
blob8818d8f45951a76bc5321fd9ba7bc732e53c6ad5
1 module ActionWebService # :nodoc:
2   module Container # :nodoc:
3     module Direct # :nodoc:
4       class ContainerError < ActionWebServiceError # :nodoc:
5       end
7       def self.included(base) # :nodoc:
8         base.extend(ClassMethods)
9       end
10   
11       module ClassMethods
12         # Attaches ActionWebService API +definition+ to the calling class.
13         #
14         # Action Controllers can have a default associated API, removing the need
15         # to call this method if you follow the Action Web Service naming conventions.
16         #
17         # A controller with a class name of GoogleSearchController will
18         # implicitly load <tt>app/apis/google_search_api.rb</tt>, and expect the
19         # API definition class to be named <tt>GoogleSearchAPI</tt> or
20         # <tt>GoogleSearchApi</tt>.
21         #
22         # ==== Service class example
23         #
24         #   class MyService < ActionWebService::Base
25         #     web_service_api MyAPI
26         #   end
27         #
28         #   class MyAPI < ActionWebService::API::Base
29         #     ...
30         #   end
31         #
32         # ==== Controller class example
33         #
34         #   class MyController < ActionController::Base
35         #     web_service_api MyAPI
36         #   end
37         #
38         #   class MyAPI < ActionWebService::API::Base
39         #     ...
40         #   end
41         def web_service_api(definition=nil)
42           if definition.nil?
43             read_inheritable_attribute("web_service_api")
44           else
45             if definition.is_a?(Symbol)
46               raise(ContainerError, "symbols can only be used for #web_service_api inside of a controller")
47             end
48             unless definition.respond_to?(:ancestors) && definition.ancestors.include?(ActionWebService::API::Base)
49               raise(ContainerError, "#{definition.to_s} is not a valid API definition")
50             end
51             write_inheritable_attribute("web_service_api", definition)
52             call_web_service_api_callbacks(self, definition)
53           end
54         end
55   
56         def add_web_service_api_callback(&block) # :nodoc:
57           write_inheritable_array("web_service_api_callbacks", [block])
58         end
59   
60         private
61           def call_web_service_api_callbacks(container_class, definition)
62             (read_inheritable_attribute("web_service_api_callbacks") || []).each do |block|
63               block.call(container_class, definition)
64             end
65           end
66       end
67     end
68   end
69 end