Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

root/trunk/activesupport/lib/active_support/core_ext/array/conversions.rb

Revision 9226, 5.2 kB (checked in by pratik, 3 months ago)

Improve documentation.

Line 
1 require 'builder'
2
3 module ActiveSupport #:nodoc:
4   module CoreExtensions #:nodoc:
5     module Array #:nodoc:
6       module Conversions
7         # Converts the array to a comma-separated sentence where the last element is joined by the connector word. Options:
8         # * <tt>:connector</tt> - The word used to join the last element in arrays with two or more elements (default: "and")
9         # * <tt>:skip_last_comma</tt> - Set to true to return "a, b and c" instead of "a, b, and c".
10         def to_sentence(options = {})
11           options.assert_valid_keys(:connector, :skip_last_comma)
12           options.reverse_merge! :connector => 'and', :skip_last_comma => false
13           options[:connector] = "#{options[:connector]} " unless options[:connector].nil? || options[:connector].strip == ''
14
15           case length
16             when 0
17               ""
18             when 1
19               self[0].to_s
20             when 2
21               "#{self[0]} #{options[:connector]}#{self[1]}"
22             else
23               "#{self[0...-1].join(', ')}#{options[:skip_last_comma] ? '' : ','} #{options[:connector]}#{self[-1]}"
24           end
25         end
26
27         # Calls <tt>to_param</tt> on all its elements and joins the result with
28         # slashes. This is used by <tt>url_for</tt> in Action Pack.
29         def to_param
30           map(&:to_param).join '/'
31         end
32
33         # Converts an array into a string suitable for use as a URL query string,
34         # using the given +key+ as the param name.
35         #
36         #   ['Rails', 'coding'].to_query('hobbies') # => "hobbies%5B%5D=Rails&hobbies%5B%5D=coding"
37         def to_query(key)
38           collect { |value| value.to_query("#{key}[]") } * '&'
39         end
40
41         def self.included(base) #:nodoc:
42           base.class_eval do
43             alias_method :to_default_s, :to_s
44             alias_method :to_s, :to_formatted_s
45           end
46         end
47
48         # Converts a collection of elements into a formatted string by calling
49         # <tt>to_s</tt> on all elements and joining them:
50         #
51         #   Blog.find(:all).to_formatted_s # => "First PostSecond PostThird Post"
52         #
53         # Adding in the <tt>:db</tt> argument as the format yields a prettier
54         # output:
55         #
56         #   Blog.find(:all).to_formatted_s(:db) # => "First Post,Second Post,Third Post"
57         def to_formatted_s(format = :default)
58           case format
59             when :db
60               if respond_to?(:empty?) && self.empty?
61                 "null"
62               else
63                 collect { |element| element.id }.join(",")
64               end
65             else
66               to_default_s
67           end
68         end
69
70         # Returns a string that represents this array in XML by sending
71         # <tt>to_xml</tt> to each element.
72         #
73         # All elements are expected to respond to <tt>to_xml</tt>, if any of
74         # them does not an exception is raised.
75         #
76         # The root node reflects the class name of the first element in plural
77         # if all elements belong to the same type and that's not <tt>Hash</tt>.
78         # Otherwise the root element is "records".
79         #
80         # Root children have as node name the one of the root singularized.
81         #
82         #   [{:foo => 1, :bar => 2}, {:baz => 3}].to_xml
83         #
84         #   <?xml version="1.0" encoding="UTF-8"?>
85         #   <records type="array">
86         #     <record>
87         #       <bar type="integer">2</bar>
88         #       <foo type="integer">1</foo>
89         #     </record>
90         #     <record>
91         #       <baz type="integer">3</baz>
92         #     </record>
93         #   </records>
94         #
95         # The +options+ hash is passed downwards:
96         #
97         #   [Message.find(:first)].to_xml(:skip_types => true)
98         #
99         #   <?xml version="1.0" encoding="UTF-8"?>
100         #   <messages>
101         #     <message>
102         #       <created-at>2008-03-07T09:58:18+01:00</created-at>
103         #       <id>1</id>
104         #       <name>1</name>
105         #       <updated-at>2008-03-07T09:58:18+01:00</updated-at>
106         #       <user-id>1</user-id>
107         #     </message>
108         #   </messages>
109         #
110         def to_xml(options = {})
111           raise "Not all elements respond to to_xml" unless all? { |e| e.respond_to? :to_xml }
112
113           options[:root]     ||= all? { |e| e.is_a?(first.class) && first.class.to_s != "Hash" } ? first.class.to_s.underscore.pluralize : "records"
114           options[:children] ||= options[:root].singularize
115           options[:indent]   ||= 2
116           options[:builder]  ||= Builder::XmlMarkup.new(:indent => options[:indent])
117
118           root     = options.delete(:root).to_s
119           children = options.delete(:children)
120
121           if !options.has_key?(:dasherize) || options[:dasherize]
122             root = root.dasherize
123           end
124
125           options[:builder].instruct! unless options.delete(:skip_instruct)
126
127           opts = options.merge({ :root => children })
128
129           xml = options[:builder]
130           if empty?
131             xml.tag!(root, options[:skip_types] ? {} : {:type => "array"})
132           else
133             xml.tag!(root, options[:skip_types] ? {} : {:type => "array"}) {
134               yield xml if block_given?
135               each { |e| e.to_xml(opts.merge!({ :skip_instruct => true })) }
136             }
137           end
138         end
139
140       end
141     end
142   end
143 end
Note: See TracBrowser for help on using the browser.