C0 code coverage information
Generated on Sat Feb 02 17:44:26 +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 # Copyright (c) 2008 Michael Fellinger m.fellinger@gmail.com
2 # All files in this distribution are subject to the terms of the Ruby license.
3
4 module Ramaze
5 # LinkHelper is included into the Controller by default
6 #
7 # Usage is shown in spec/ramaze/helper/link.rb and the rdocs below.
8
9 module LinkHelper
10
11 private
12
13 # Builds a basic <a> tag.
14 #
15 # `text` is mandatory, the second hash of options will be transformed into
16 # arguments of the tag, :href is a special case and its segments will be
17 # CGI.escaped.
18 #
19 # If you pass no :href, the text will be run through Rs and its result is
20 # used instead. If you really want an empty href, use :href => ''
21 #
22 # Usage:
23 # A('text') #> <a href="/text">text</a>
24 # A('foo/bar') #> <a href="/foo/bar">foo/bar</a>
25 # A('/foo?x=y') #> <a href="/foo?x=y">/foo?x=y</a>
26 # A('text', :href => '/foo?x=y') #> <a href="/foo?x=y">text</a>
27 # A('Home', :href => Rs(:/)) #> <a href="/foo/bar">Home</a>
28
29 def A(*args)
30 hash = args.last.respond_to?(:to_hash) ? args.pop : {}
31
32 hash[:href] ||= Rs(*args)
33 text = hash.delete(:text) || args.last || hash[:title] || hash[:href]
34 hash[:href] = hash[:href].to_s.gsub(/[^\/?;=]+/) {|m| CGI.escape(m) }
35
36 args = ['']
37 hash.each {|k,v| args << %(#{k}="#{v}") if k and v }
38
39 %(<a#{args.join(' ')}>#{text}</a>)
40 end
41
42 # Builds links out of segments.
43 #
44 # Pass it strings, symbols, controllers and it will produce a link out of
45 # it. Paths to Controllers are obtained from Global.mapping.
46 #
47 # For brevity, the mapping for the example below is following:
48 # { MC => '/', OC => '/o', ODC => '/od' }
49 #
50 # Usage:
51 # R(MC) #=> '/'
52 # R(OC) #=> '/o'
53 # R(ODC) #=> '/od'
54 # R(MC, :foo) #=> '/foo'
55 # R(OC, :foo) #=> '/o/foo'
56 # R(ODC, :foo) #=> '/od/foo'
57 # R(MC, :foo, :bar => :x) #=> '/foo?bar=x'
58
59 def R(*atoms)
60 args, atoms = atoms.flatten.partition{|a| a.is_a?(Hash) }
61 args = args.flatten.inject{|s,v| s.merge!(v) }
62
63 map = Global.mapping.invert
64 atoms.map! do |atom|
65 if atom.is_a?(Ramaze::Controller)
66 map[atom.class] || atom
67 else
68 map[atom] || atom
69 end
70 end
71
72 front = atoms.join('/').squeeze('/')
73
74 if args
75 rear = args.inject('?'){|s,(k,v)| s << "#{k}=#{v};"}[0..-2]
76 front + rear
77 else
78 front
79 end
80 end
81
82 # Uses R with Controller.current as first element.
83
84 def Rs(*atoms)
85 atoms.unshift Controller.current unless atoms.first.is_a?(Controller)
86 R(*atoms)
87 end
88
89 # Give it a path with character to split at and one to join the crumbs with.
90 # It will generate a list of links that act as pointers to previous pages on
91 # this path.
92 #
93 # Example:
94 # breadcrumbs('/path/to/somewhere')
95 #
96 # # results in this, newlines added for readability:
97 #
98 # <a href="/path">path</a>/
99 # <a href="/path/to">to</a>/
100 # <a href="/path/to/somewhere">somewhere</a>
101 #
102 # Optionally a href prefix can be specified which generate link
103 # names a above, but with the prefix prepended to the href path.
104 #
105 # Example:
106 # breadcrumbs('/path/to/somewhere', '/', '/', '/mycontroller/action')
107 #
108 # # results in this, newlines added for readability:
109 #
110 # <a href="/mycontroller/action/path">path</a>/
111 # <a href="/mycontroller/action/path/to">to</a>/
112 # <a href="/mycontroller/action/path/to/somewhere">somewhere</a>
113
114 def breadcrumbs(path, split = '/', join = '/', href_prefix = '')
115 atoms = path.split(split).reject{|a| a.empty?}
116 crumbs = atoms.inject([]){|s,v| s << [s.last,v]}
117 bread = crumbs.map do |a|
118 href_path = href_prefix + a*'/'
119 A(a[-1], :href=>(href_path))
120 end
121 bread.join(join)
122 end
123 end
124 end
Generated using the rcov code coverage analysis tool for Ruby version 0.8.1.2.