Class: ChainGang::Proxy
- Inherits:
-
Object
- Object
- ChainGang::Proxy
- Defined in:
- lib/chaingang/proxy.rb
Instance Attribute Summary collapse
-
#find_scope ⇒ Object
Returns the value of attribute find_scope.
Instance Method Summary collapse
-
#and ⇒ Object
Simply improves readability.
-
#execute_with_format ⇒ Object
(also: #execute)
execute the request in specified format, then revert it back after the request.
-
#format(fmt) ⇒ Object
allow active resource to execute request with custom xml/json format from default.
-
#from(f) ⇒ Object
Specify a custom url path to retrieve send the service call to.
-
#initialize(client, id = nil) ⇒ Proxy
constructor
A new instance of Proxy.
-
#method_missing(method_name, *args, &block) ⇒ Object
Set the query string parameters on your request by calling them as methods with a question mark at the end.
-
#param?(name, value) ⇒ Boolean
Suppose you have a parameter name that collides with an existing method.
-
#where ⇒ Object
Simply improves readability.
Constructor Details
#initialize(client, id = nil) ⇒ Proxy
Returns a new instance of Proxy.
5 6 7 8 9 |
# File 'lib/chaingang/proxy.rb', line 5 def initialize(client, id=nil) @client = client @params = {} @find_scope = id || :all end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &block) ⇒ Object
Set the query string parameters on your request by calling them as methods with a question mark at the end.
Article.find(:all).where.("moonmaster9000") #/articles.xml?author=moonmaster9000
42 43 44 45 46 47 48 49 |
# File 'lib/chaingang/proxy.rb', line 42 def method_missing(method_name, *args, &block) if args.length == 1 && question?(method_name) @params[unquestion method_name] = args.first self else self.execute.send method_name, *args, &block end end |
Instance Attribute Details
#find_scope ⇒ Object
Returns the value of attribute find_scope.
3 4 5 |
# File 'lib/chaingang/proxy.rb', line 3 def find_scope @find_scope end |
Instance Method Details
#and ⇒ Object
Simply improves readability. Does nothing but return self.
Article.find.where.("moonmaster9000").and.published?(true)
18 |
# File 'lib/chaingang/proxy.rb', line 18 def and; self; end |
#execute_with_format ⇒ Object Also known as: execute
execute the request in specified format, then revert it back after the request.
62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/chaingang/proxy.rb', line 62 def execute_with_format fmt = @format if fmt && fmt.to_s != @client.format.extension old = @client.format.extension @client.format = fmt.to_sym result = execute_without_format @client.format = old.to_sym return result else execute_without_format end end |
#format(fmt) ⇒ Object
allow active resource to execute request with custom xml/json format from default.
Article.find(:all).by?("moonmaster9000").format(:json)
34 35 36 37 |
# File 'lib/chaingang/proxy.rb', line 34 def format(fmt) @format = fmt.to_sym self end |
#from(f) ⇒ Object
Specify a custom url path to retrieve send the service call to.
Article.find(:all).from :published
14 |
# File 'lib/chaingang/proxy.rb', line 14 def from(f); @from = f; self; end |
#param?(name, value) ⇒ Boolean
Suppose you have a parameter name that collides with an existing method. Simply set it with this param? method:
Article.find(:all).param?(:from, "New York Times")
27 28 29 30 |
# File 'lib/chaingang/proxy.rb', line 27 def param?(name, value) @params[name] = value self end |
#where ⇒ Object
Simply improves readability. Does nothing but return self.
Article.find.where.("moonmaster9000").and.published?(true)
22 |
# File 'lib/chaingang/proxy.rb', line 22 def where; self; end |