C0 code coverage information
Generated on Sat Feb 02 17:44:24 +0100 2008 with rcov 0.8.1.2
Code reported as executed by Ruby looks like this...
and this: this line is also marked as covered.
Lines considered as run by rcov, but not reported by Ruby, look like this,
and this: these lines were inferred by rcov (using simple heuristics).
Finally, here's a line marked as not executed.
1 # auto_params.rb
2 #
3 # AutoParams implements action parameterization ala merb 0.4 and Nitro
4 # using ruby2ruby and ParseTree
5 #
6 # Usage:
7 #
8 # Ramaze.contrib :auto_params
9 #
10 # Then, for example,
11 #
12 # def search(query) end
13 #
14 # can be accessed via
15 #
16 # - /search?query=findthis, which calls search('findthis')
17 # - /search/findthis search('findthis')
18 # - /search/findthis?query=andthis search(['findthis', 'andthis'])
19 #
20 # For more examples, take a look at spec/contrib/auto_params.rb
21 #
22 # Note: A simpler alternative for similar functionality would be:
23 #
24 # def search(query = request['query']) end
25 #
26
27 require __DIR__/:auto_params/:get_args
28
29 module Ramaze
30
31 module Contrib
32 class AutoParams
33 def self.startup
34 Ramaze::Cache.add :args
35 end
36 end
37 end
38
39 class Action
40
41 # with parameterization, params may include
42 # arrays: /num?n=1&n=2 becomes [['1','2']] for def num(n) end
43 # nil: /calc?w=10&d=2 becomes ['10', nil, '2'] for def calc(w, h, d) end
44
45 def params=(*par)
46 par = *par
47 self[:params] = par.map{ |pa|
48 case pa
49 when Array
50 pa.map{|p| CGI.unescape(p.to_s)}
51 when nil
52 nil
53 else
54 CGI.unescape(pa.to_s)
55 end
56 } unless par.nil?
57 self[:params] ||= []
58 end
59
60 end
61
62 class Controller
63
64 # ignore cache when request.params is interesting
65
66 def self.cached(path)
67 if found = Cache.resolved[path]
68 if found.respond_to?(:relaxed_hash)
69
70 # don't use cache if we need to add request.params entries to the Action
71 if args = Cache.args[found.method]
72 param_keys = request.params.keys
73 return nil if args.find{|k,v| param_keys.include?(k.to_s) }
74 end
75
76 return found.dup
77 else
78 Inform.warn("Found faulty `#{path}' in Cache.resolved, deleting it for sanity.")
79 Cache.resolved.delete path
80 end
81 end
82
83 nil
84 end
85
86 # use Method#get_args to insert values from request.params into Action#params
87
88 def self.resolve_method(name, *params)
89 if method = [ name, name.gsub('__','/') ].find{|n|
90 cached_action_methods.include?(n) }
91 meth = instance_method(method)
92 arity = meth.arity
93
94 if meth.respond_to? :get_args
95 unless args = Cache.args[name]
96 if args = meth.get_args
97 args = args.select{|e| e.to_s !~ /^\*/}
98 else
99 args = []
100 end
101 Cache.args[name] = args
102 end
103
104 param_keys = request.params.keys
105
106 # if there are missing args, or keys in request.params that match expected args
107 if args.size > params.size or args.find{|k,v| param_keys.include?(k.to_s) }
108 args.each_with_index do |(name, val), i|
109 r_params = request.params[name.to_s]
110 if params[i] and r_params and r_params.size > 0
111 params[i] = [params[i], r_params].flatten
112 else
113 params[i] ||= r_params
114 end
115 end
116
117 # strip trailing nils so default argument values are used
118 params.reverse.each{|e| if e.nil? then params.pop else break end }
119 end
120
121 argity = args.select{|e| e.size==1}.size..args.size
122 else
123 argity = arity..arity
124 end
125
126 if arity < 0 or argity.include? params.size
127 return method, params
128 end
129 end
130 return nil, []
131 end
132
133 end
134
135 end
Generated using the rcov code coverage analysis tool for Ruby version 0.8.1.2.