Class: ChainGang::Proxy

Inherits:
Object
  • Object
show all
Defined in:
lib/chaingang/proxy.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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 an exclamation point at the end.

Article.find(:all).where.author!("moonmaster9000") #/articles.xml?author=moonmaster9000


35
36
37
38
39
40
41
42
# File 'lib/chaingang/proxy.rb', line 35

def method_missing(method_name, *args, &block)
  if args.length == 1 && exclamatory?(method_name)
    @params[unexclaim method_name] = args.first
    self
  else
    self.execute.send method_name, *args, &block
  end
end

Instance Attribute Details

#find_scopeObject

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

#andObject

Simply improves readability. Does nothing but return self.

Article.find.where.author!("moonmaster9000").and.published!(true)


18
# File 'lib/chaingang/proxy.rb', line 18

def and;    self; end

#executeObject

Make the service call immediately.

Article.find(:all).execute


46
47
48
49
50
51
# File 'lib/chaingang/proxy.rb', line 46

def execute
  options = {}
  options[:from] = @from if @from
  options[:params] = @params
  @client.find_without_chaingang(@find_scope, options)
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) ⇒ Object

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

#whereObject

Simply improves readability. Does nothing but return self.

Article.find.where.author!("moonmaster9000").and.published!(true)


22
# File 'lib/chaingang/proxy.rb', line 22

def where;  self; end