activesupport 1.3.1 → 1.4.0
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of activesupport might be problematic. Click here for more details.
- data/CHANGELOG +232 -2
- data/README +43 -0
- data/lib/active_support.rb +4 -1
- data/lib/active_support/breakpoint.rb +5 -0
- data/lib/active_support/core_ext/array.rb +2 -16
- data/lib/active_support/core_ext/array/conversions.rb +30 -4
- data/lib/active_support/core_ext/array/grouping.rb +55 -0
- data/lib/active_support/core_ext/bigdecimal.rb +3 -0
- data/lib/active_support/core_ext/bigdecimal/formatting.rb +7 -0
- data/lib/active_support/core_ext/class/inheritable_attributes.rb +6 -1
- data/lib/active_support/core_ext/date/conversions.rb +13 -7
- data/lib/active_support/core_ext/enumerable.rb +41 -10
- data/lib/active_support/core_ext/exception.rb +2 -2
- data/lib/active_support/core_ext/hash/conversions.rb +123 -12
- data/lib/active_support/core_ext/hash/indifferent_access.rb +18 -9
- data/lib/active_support/core_ext/integer/inflections.rb +10 -4
- data/lib/active_support/core_ext/load_error.rb +3 -3
- data/lib/active_support/core_ext/module.rb +2 -0
- data/lib/active_support/core_ext/module/aliasing.rb +58 -0
- data/lib/active_support/core_ext/module/attr_internal.rb +31 -0
- data/lib/active_support/core_ext/module/delegation.rb +27 -2
- data/lib/active_support/core_ext/name_error.rb +20 -0
- data/lib/active_support/core_ext/string.rb +2 -0
- data/lib/active_support/core_ext/string/access.rb +5 -5
- data/lib/active_support/core_ext/string/inflections.rb +93 -4
- data/lib/active_support/core_ext/string/unicode.rb +42 -0
- data/lib/active_support/core_ext/symbol.rb +1 -1
- data/lib/active_support/core_ext/time/calculations.rb +7 -5
- data/lib/active_support/core_ext/time/conversions.rb +1 -2
- data/lib/active_support/dependencies.rb +417 -50
- data/lib/active_support/deprecation.rb +201 -0
- data/lib/active_support/inflections.rb +1 -2
- data/lib/active_support/inflector.rb +117 -19
- data/lib/active_support/json.rb +14 -3
- data/lib/active_support/json/encoders/core.rb +21 -18
- data/lib/active_support/multibyte.rb +7 -0
- data/lib/active_support/multibyte/chars.rb +129 -0
- data/lib/active_support/multibyte/generators/generate_tables.rb +149 -0
- data/lib/active_support/multibyte/handlers/passthru_handler.rb +9 -0
- data/lib/active_support/multibyte/handlers/utf8_handler.rb +453 -0
- data/lib/active_support/multibyte/handlers/utf8_handler_proc.rb +44 -0
- data/lib/active_support/option_merger.rb +3 -3
- data/lib/active_support/ordered_options.rb +24 -23
- data/lib/active_support/reloadable.rb +39 -5
- data/lib/active_support/values/time_zone.rb +1 -1
- data/lib/active_support/values/unicode_tables.dat +0 -0
- data/lib/active_support/vendor/builder/blankslate.rb +16 -6
- data/lib/active_support/vendor/builder/xchar.rb +112 -0
- data/lib/active_support/vendor/builder/xmlbase.rb +12 -10
- data/lib/active_support/vendor/builder/xmlmarkup.rb +26 -7
- data/lib/active_support/vendor/xml_simple.rb +1021 -0
- data/lib/active_support/version.rb +2 -2
- data/lib/active_support/whiny_nil.rb +1 -1
- metadata +26 -4
- data/lib/active_support/core_ext/hash/conversions.rb.rej +0 -28
data/CHANGELOG
CHANGED
@@ -1,4 +1,234 @@
|
|
1
|
-
*1.
|
1
|
+
*1.4.0* (January 16th, 2007)
|
2
|
+
|
3
|
+
* Document Inflector.ordinalize and merge docs from String inflections. #7023 [smeade]
|
4
|
+
|
5
|
+
* Unbundle flexmock. [Jeremy Kemper]
|
6
|
+
|
7
|
+
* Fix Dependencies.autoloaded? to ignore anonymous modules. Closes #6561. [Nicholas Seckar]
|
8
|
+
|
9
|
+
* Update load once paths to prevent nested once constants from being detected and claimed by an external non-once load. [Nicholas Seckar]
|
10
|
+
|
11
|
+
* Deprecation: silence warnings when reporting test errors. [Jeremy Kemper]
|
12
|
+
|
13
|
+
* Fix remove_constant to correctly handle constant names of the form "::A::...". References #6720. [Nicholas Seckar]
|
14
|
+
|
15
|
+
* Fixed Array#to_xml when it contains a series of hashes (each piece would get its own XML declaration) #6610 [thkarcher/cyu]
|
16
|
+
|
17
|
+
* Hash#to_xml handles keys with the same name as Kernel methods. #6613 [Catfish]
|
18
|
+
|
19
|
+
* Don't quote hash keys in Hash#to_json if they're valid JavaScript identifiers. Disable this with ActiveSupport::JSON.unquote_hash_key_identifiers = false if you need strict JSON compliance. [Sam Stephenson]
|
20
|
+
|
21
|
+
* Lazily load the Unicode Database in the UTF-8 Handler [Rick Olson]
|
22
|
+
|
23
|
+
* Update dependencies to delete partially loaded constants. [Nicholas Seckar]
|
24
|
+
|
25
|
+
* Fix unicode JSON regexp for Onigurama compatibility. #6494 [whitley]
|
26
|
+
|
27
|
+
* Update XmlSimple to 1.0.10. Closes #6532. [nicksieger]
|
28
|
+
|
29
|
+
* Update dependencies to allow constants to be defined alongside their siblings. A common case for this is AR model classes with STI; user.rb might define User, Administrator and Guest for example. [Nicholas Seckar]
|
30
|
+
|
31
|
+
* next_week respects DST changes. #6483, #5617, #2353, #2509, #4551 [marclove, rabiedenharn, rails@roetzel.de, jsolson@damogran.org, drbrain@segment7.net]
|
32
|
+
|
33
|
+
* Expose methods added to Enumerable in the documentation, such as group_by. Closes #6170. [sergeykojin@gmail.com, Marcel Molina Jr.]
|
34
|
+
|
35
|
+
* Ensure Chars#tidy_bytes only tidies broken bytes. Closes #6397 [Manfred Stienstra]
|
36
|
+
|
37
|
+
* Add 'unloadable', a method used to mark any constant as requiring an unload after each request. [Nicholas Seckar]
|
38
|
+
|
39
|
+
* Make core_ext/string/access.rb multibyte safe. Closes #6388 [Manfred Stienstra]
|
40
|
+
|
41
|
+
* Make String#chars slicing behaviour consistent with String. Closes #6387 [Manfred Stienstra]
|
42
|
+
|
43
|
+
* Pull in latest multibye patch. Closes #6346 [Manfred Stienstra]
|
44
|
+
|
45
|
+
* Add ActiveSupport::Multibyte. Provides String#chars which lets you deal with strings as a sequence of chars, not of bytes. Closes #6242 [Julian Tarkhanov, Manfred Stienstra, Thijs van der Vossen & Jan Behrens]
|
46
|
+
|
47
|
+
* Fix issue with #class_inheritable_accessor saving updates to the parent class when initialized with an Array or Hash [mojombo]
|
48
|
+
|
49
|
+
* Hash#to_xml supports Bignum and BigDecimal. #6313 [edibiase]
|
50
|
+
|
51
|
+
* Don't undefine #class in OptionMerger [Rick]
|
52
|
+
|
53
|
+
* Hash.create_from_xml has been renamed to Hash.from_xml, alias will exist until Rails 2.0 [DHH]
|
54
|
+
|
55
|
+
* alias_method_chain works with accessor= methods also. #6153 [Caio Chassot]
|
56
|
+
|
57
|
+
* Fix loadable_constants_for_path to handle load paths that do not end with a slash. [Nicholas Seckar]
|
58
|
+
|
59
|
+
* Fix logic error in determining what was loaded by a given file. Closes #6039. [Nicholas Seckar]
|
60
|
+
|
61
|
+
* Equate Kernel.const_missing with Object.const_missing. Fixes #5988. [Nicholas Seckar]
|
62
|
+
|
63
|
+
* Don't pad remaining places with in_groups_of if specified padding value is false. [Marcel Molina Jr.]
|
64
|
+
|
65
|
+
* Fix cases where empty xml nodes weren't being translated to nil in Hash.create_from_xml [Rick Olso n]
|
66
|
+
|
67
|
+
<written-on type="date"></written-on> # => { :type => 'date' } # WRONG
|
68
|
+
<written-on type="date"></written-on> # => nil # RIGHT
|
69
|
+
|
70
|
+
* Tighten rescue clauses. #5985 [james@grayproductions.net]
|
71
|
+
|
72
|
+
* Inflections: don't singularize -ies plurals. [foamdino@gmail.com, Mark Van Holstyn]
|
73
|
+
|
74
|
+
* Update Initializer to use load_once_paths to avoid plugin reloading. References #5852. [Nicholas Seckar]
|
75
|
+
|
76
|
+
* Use Array#assoc in ActiveSupport::OrderedHash. [Mauricio Fernandez]
|
77
|
+
|
78
|
+
* Greatly increased performance of String.to_json, which speeds up RJS considerably on large pages, fixes #3473 [Shugo Maeda]
|
79
|
+
|
80
|
+
* Detect missing_constants calls from removed modules and fail accordingly. [Nicholas Seckar]
|
81
|
+
|
82
|
+
* Stop using defined? in Dependencies.qualified_const_defined? since defined? may invoke const_missing. [Nicholas Seckar]
|
83
|
+
|
84
|
+
* Dependencies can autoload directories of nested classes. [Jeremy Kemper]
|
85
|
+
Example:
|
86
|
+
invoice.rb class Invoice
|
87
|
+
invoice/lineitem.rb class Invoice::Lineitem
|
88
|
+
|
89
|
+
* Add Deprecation.silence so that Reloadable does not scold itself. [Nicholas Seckar]
|
90
|
+
|
91
|
+
* Add debugging logging to Dependencies. Currently can be enabled with Dependencies.log_activity = true; adding to Initializer and documenting is forthcoming. [Nicholas Seckar]
|
92
|
+
|
93
|
+
* Replace Reloadable with improvements to the Dependencies mechanism. [Nicholas Seckar]
|
94
|
+
|
95
|
+
* DateTime#to_time gives hour/minute/second resolution. #5747 [jon.evans@pobox.com]
|
96
|
+
|
97
|
+
* attr_internal to support namespacing and deprecation. Like attr_* except backed by internally-named instance variable. Set attr_internal_naming_format to change the format from the default '@_%s'. [Jeremy Kemper]
|
98
|
+
# def foo() @foo__rofl end
|
99
|
+
# def foo=(v) @foo__rofl = v end
|
100
|
+
self.attr_internal_naming_format = '@%s__rofl'
|
101
|
+
attr_internal :foo
|
102
|
+
|
103
|
+
* Raise fully qualified names upon name errors. #5533 [lars@pinds.com, Nicholas Seckar]
|
104
|
+
|
105
|
+
* Add extention to obtain the missing constant from NameError instances. [Nicholas Seckar]
|
106
|
+
|
107
|
+
* Added Module#alias_attribute [Jamis/DHH]. Example:
|
108
|
+
|
109
|
+
class Content < ActiveRecord::Base
|
110
|
+
# has a title attribute
|
111
|
+
end
|
112
|
+
|
113
|
+
class Email < ActiveRecord::Base
|
114
|
+
alias_attribute :subject, :title
|
115
|
+
end
|
116
|
+
|
117
|
+
e = Email.find(1)
|
118
|
+
e.title # => "Superstars"
|
119
|
+
e.subject # => "Superstars"
|
120
|
+
e.subject? # => true
|
121
|
+
e.subject = "Megastars"
|
122
|
+
e.title # => "Megastars"
|
123
|
+
|
124
|
+
* Deprecation: easier to work with warning behavior as procs; default behaviors for each environment so users needn't update env.rb; and testing pleasure with assert_deprecated, assert_not_deprecated. [Jeremy Kemper]
|
125
|
+
By default, test prints to $stderr, dev logs, production ignores.
|
126
|
+
Provide your own per-environment in e.g. config/environments/development.rb:
|
127
|
+
ActiveSupport::Deprecation.behavior = Proc.new { |message| raise message }
|
128
|
+
|
129
|
+
* First cut of the Rails Deprecation system. [Koz]
|
130
|
+
|
131
|
+
* Strip boolean XML content before checking for 'true' [Rick Olson]
|
132
|
+
|
133
|
+
* Customize default BigDecimal formatting. References #5672 [dave@pragprog.com]
|
134
|
+
|
135
|
+
* Correctly convert <foo nil="true"> to nil when using Hash.create_from_xml. [Rick]
|
136
|
+
|
137
|
+
* Optional identity for Enumerable#sum defaults to zero. #5657 [gensym@mac.com]
|
138
|
+
|
139
|
+
* HashWithIndifferentAccess shouldn't confuse false and nil. #5601 [shugo@ruby-lang.org]
|
140
|
+
|
141
|
+
* Fixed HashWithIndifferentAccess#default #5586 [chris@seagul.co.uk]
|
142
|
+
|
143
|
+
* More compatible Hash.create_from_xml. #5523 [nunemaker@gmail.com]
|
144
|
+
|
145
|
+
* Added Enumerable#sum for calculating a sum from the elements [DHH, jonathan@daikini.com]. Examples:
|
146
|
+
|
147
|
+
[1, 2, 3].sum
|
148
|
+
payments.sum { |p| p.price * p.tax_rate }
|
149
|
+
payments.sum(&:price)
|
150
|
+
|
151
|
+
This is instead of payments.inject(0) { |sum, p| sum + p.price }
|
152
|
+
|
153
|
+
* Correct and clarify Array#to_sentence docs. #5458 [brad@madriska.com]
|
154
|
+
|
155
|
+
* alias_method_chain preserves method punctuation so foo, foo?, and foo! may be chained with the same feature. [Jeremy Kemper]
|
156
|
+
Example:
|
157
|
+
alias_method_chain :save!, :validation
|
158
|
+
is equivalent to
|
159
|
+
alias_method :save_without_validation!, :save!
|
160
|
+
alias_method :save!, :save_with_validation!
|
161
|
+
|
162
|
+
* Enhance Symbol#to_proc so it works with list objects, such as multi-dimensional arrays. Closes #5295 [nov@yo.rim.or.jp]. Example:
|
163
|
+
|
164
|
+
{1 => "one", 2 => "two", 3 => "three"}.sort_by(&:first).map(&:last)
|
165
|
+
#=> ["one", "two", "three"]
|
166
|
+
|
167
|
+
* Added Hash.create_from_xml(string) which will create a hash from a XML string and even typecast if possible [DHH]. Example:
|
168
|
+
|
169
|
+
Hash.create_from_xml <<-EOT
|
170
|
+
<note>
|
171
|
+
<title>This is a note</title>
|
172
|
+
<created-at type="date">2004-10-10</created-at>
|
173
|
+
</note>
|
174
|
+
EOT
|
175
|
+
|
176
|
+
...would return:
|
177
|
+
|
178
|
+
{ :note => { :title => "This is a note", :created_at => Date.new(2004, 10, 10) } }
|
179
|
+
|
180
|
+
* Added Jim Weirich's excellent FlexMock class to vendor (Copyright 2003, 2004 by Jim Weirich (jim@weriichhouse.org)) -- it's not automatically required, though, so require 'flexmock' is still necessary [DHH]
|
181
|
+
|
182
|
+
* Fixed that Module#alias_method_chain should work with both foo? foo! and foo at the same time #4954 [anna@wota.jp]
|
183
|
+
|
184
|
+
* to_xml fixes, features, and speedup: introduce :dasherize option that converts updated_at to updated-at if true (the existing default); binary columns get encoding="base64" attribute; nil values get nil="true" attribute to distinguish empty values; add type information for float columns; allow arbitrarily deep :include; include SQL type information as the type attribute. #4989 [Blair Zajac <blair@orcaware.com>]
|
185
|
+
|
186
|
+
* Add OrderedHash#values. [Sam Stephenson]
|
187
|
+
|
188
|
+
* Added Array#to_s(:db) that'll produce a comma-separated list of ids [DHH]. Example:
|
189
|
+
|
190
|
+
Purchase.find(:all, :conditions => "product_id IN (#{shops.products.to_s(:db)})"
|
191
|
+
|
192
|
+
* Normalize classify's argument to a String so that it plays nice with Symbols. [Marcel Molina Jr.]
|
193
|
+
|
194
|
+
* Strip out leading schema name in classify. References #5139. [schoenm@earthlink.net]
|
195
|
+
|
196
|
+
* Remove Enumerable#first_match since break(value) handles the use case well enough. [Nicholas Seckar]
|
197
|
+
|
198
|
+
Enumerable#first_match was like detect, but instead of returning the matching element, the yielded value returned. For example:
|
199
|
+
|
200
|
+
user_xml = adapters(:from => User, :to => Xml).first_match do |adapter|
|
201
|
+
adapter.adapt @user
|
202
|
+
end
|
203
|
+
|
204
|
+
But this is just as easily done with:
|
205
|
+
|
206
|
+
user_xml = adapters(:from => User, :to => Xml).each do
|
207
|
+
break adapter.adapt(@user)
|
208
|
+
end
|
209
|
+
|
210
|
+
* Make Array#in_groups_of just return the grouped collection if a block isn't given. [Marcel Molina Jr.]
|
211
|
+
|
212
|
+
* Don't destroy a HashWithIndifferentAccess if symbolize_keys! or stringify_keys! is called on it. Closes #5076. [Marcel Molina Jr., guy.naor@famundo.com]
|
213
|
+
|
214
|
+
* Document Module::delegate. #5002 [pergesu@gmail.com]
|
215
|
+
|
216
|
+
* Replace alias method chaining with Module#alias_method_chain. [Marcel Molina Jr.]
|
217
|
+
|
218
|
+
* Strip out punctuation on predicates or bang methods being aliased with alias_method_chain since target?_without_feature is not a valid method name. Add tests for Module#alias_method_chain. [Marcel Molina Jr.]
|
219
|
+
|
220
|
+
* Replace Ruby's deprecated append_features in favor of included. [Marcel Molina Jr.]
|
221
|
+
|
222
|
+
* Allow default options in with_options to be overridden. Closes #4480. [murphy@cYcnus.de]
|
223
|
+
|
224
|
+
* Added Module#alias_method_chain [Jamis Buck]
|
225
|
+
|
226
|
+
* Updated to Builder 2.0 [DHH]
|
227
|
+
|
228
|
+
* Add Array#split for dividing arrays into one or more subarrays by value or block. [Sam Stephenson]
|
229
|
+
|
230
|
+
|
231
|
+
*1.3.1* (April 6th, 2006)
|
2
232
|
|
3
233
|
* Clean paths inside of exception messages and traces. [Nicholas Seckar]
|
4
234
|
|
@@ -7,7 +237,7 @@
|
|
7
237
|
* provide an empty Dependencies::LoadingModule.load which prints deprecation warnings. Lets 1.0 applications function with .13-style environment.rb.
|
8
238
|
|
9
239
|
|
10
|
-
*1.3.0* (March 27th,
|
240
|
+
*1.3.0* (March 27th, 2006)
|
11
241
|
|
12
242
|
* When possible, avoid incorrectly obtaining constants from parent modules. Fixes #4221. [Nicholas Seckar]
|
13
243
|
|
data/README
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
= Active Support -- Utility classes and standard library extensions from Rails
|
2
|
+
|
3
|
+
Active Support is a collection of various utility classes and standard library extensions that were found useful
|
4
|
+
for Rails. All these additions have hence been collected in this bundle as way to gather all that sugar that makes
|
5
|
+
Ruby sweeter.
|
6
|
+
|
7
|
+
|
8
|
+
== Download
|
9
|
+
|
10
|
+
The latest version of Active Support can be found at
|
11
|
+
|
12
|
+
* http://rubyforge.org/project/showfiles.php?group_id=182
|
13
|
+
|
14
|
+
Documentation can be found at
|
15
|
+
|
16
|
+
* http://as.rubyonrails.com
|
17
|
+
|
18
|
+
|
19
|
+
== Installation
|
20
|
+
|
21
|
+
The preferred method of installing Active Support is through its GEM file. You'll need to have
|
22
|
+
RubyGems[http://rubygems.rubyforge.org/wiki/wiki.pl] installed for that, though. If you have it,
|
23
|
+
then use:
|
24
|
+
|
25
|
+
% [sudo] gem install activesupport-1.0.0.gem
|
26
|
+
|
27
|
+
|
28
|
+
== License
|
29
|
+
|
30
|
+
Active Support is released under the MIT license.
|
31
|
+
|
32
|
+
|
33
|
+
== Support
|
34
|
+
|
35
|
+
The Active Support homepage is http://www.rubyonrails.com. You can find the Active Support
|
36
|
+
RubyForge page at http://rubyforge.org/projects/activesupport. And as Jim from Rake says:
|
37
|
+
|
38
|
+
Feel free to submit commits or feature requests. If you send a patch,
|
39
|
+
remember to update the corresponding unit tests. If fact, I prefer
|
40
|
+
new feature to be submitted in the form of new unit tests.
|
41
|
+
|
42
|
+
For other information, feel free to ask on the ruby-talk mailing list
|
43
|
+
(which is mirrored to comp.lang.ruby) or contact mailto:david@loudthinking.com.
|
data/lib/active_support.rb
CHANGED
@@ -32,10 +32,13 @@ require 'active_support/core_ext'
|
|
32
32
|
require 'active_support/clean_logger'
|
33
33
|
require 'active_support/dependencies'
|
34
34
|
require 'active_support/reloadable'
|
35
|
+
require 'active_support/deprecation'
|
35
36
|
|
36
37
|
require 'active_support/ordered_options'
|
37
38
|
require 'active_support/option_merger'
|
38
39
|
|
39
40
|
require 'active_support/values/time_zone'
|
40
41
|
|
41
|
-
require 'active_support/json'
|
42
|
+
require 'active_support/json'
|
43
|
+
|
44
|
+
require 'active_support/multibyte'
|
@@ -1,21 +1,7 @@
|
|
1
1
|
require File.dirname(__FILE__) + '/array/conversions'
|
2
|
+
require File.dirname(__FILE__) + '/array/grouping'
|
2
3
|
|
3
4
|
class Array #:nodoc:
|
4
5
|
include ActiveSupport::CoreExtensions::Array::Conversions
|
5
|
-
|
6
|
-
# Iterate over an array in groups of a certain size, padding any remaining
|
7
|
-
# slots with specified value (<tt>nil</tt> by default).
|
8
|
-
#
|
9
|
-
# E.g.
|
10
|
-
#
|
11
|
-
# %w(1 2 3 4 5 6 7).in_groups_of(3) {|g| p g}
|
12
|
-
# ["1", "2", "3"]
|
13
|
-
# ["4", "5", "6"]
|
14
|
-
# ["7", nil, nil]
|
15
|
-
def in_groups_of(number, fill_with = nil, &block)
|
16
|
-
require 'enumerator'
|
17
|
-
collection = dup
|
18
|
-
collection << fill_with until collection.size.modulo(number).zero?
|
19
|
-
collection.each_slice(number, &block)
|
20
|
-
end
|
6
|
+
include ActiveSupport::CoreExtensions::Array::Grouping
|
21
7
|
end
|
@@ -3,8 +3,8 @@ module ActiveSupport #:nodoc:
|
|
3
3
|
module Array #:nodoc:
|
4
4
|
module Conversions
|
5
5
|
# Converts the array to comma-seperated sentence where the last element is joined by the connector word. Options:
|
6
|
-
# * <tt>:connector</tt>: The word used to join the last element in arrays with
|
7
|
-
# * <tt>:skip_last_comma</tt>: Set to true to return "a, b
|
6
|
+
# * <tt>:connector</tt>: The word used to join the last element in arrays with two or more elements (default: "and")
|
7
|
+
# * <tt>:skip_last_comma</tt>: Set to true to return "a, b and c" instead of "a, b, and c".
|
8
8
|
def to_sentence(options = {})
|
9
9
|
options.assert_valid_keys(:connector, :skip_last_comma)
|
10
10
|
options.reverse_merge! :connector => 'and', :skip_last_comma => false
|
@@ -26,6 +26,24 @@ module ActiveSupport #:nodoc:
|
|
26
26
|
join '/'
|
27
27
|
end
|
28
28
|
|
29
|
+
def self.included(klass) #:nodoc:
|
30
|
+
klass.send(:alias_method, :to_default_s, :to_s)
|
31
|
+
klass.send(:alias_method, :to_s, :to_formatted_s)
|
32
|
+
end
|
33
|
+
|
34
|
+
def to_formatted_s(format = :default)
|
35
|
+
case format
|
36
|
+
when :db
|
37
|
+
if respond_to?(:empty?) && self.empty?
|
38
|
+
"null"
|
39
|
+
else
|
40
|
+
collect { |element| element.id }.join(",")
|
41
|
+
end
|
42
|
+
else
|
43
|
+
to_default_s
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
29
47
|
def to_xml(options = {})
|
30
48
|
raise "Not all elements respond to to_xml" unless all? { |e| e.respond_to? :to_xml }
|
31
49
|
|
@@ -34,12 +52,20 @@ module ActiveSupport #:nodoc:
|
|
34
52
|
options[:indent] ||= 2
|
35
53
|
options[:builder] ||= Builder::XmlMarkup.new(:indent => options[:indent])
|
36
54
|
|
37
|
-
root = options.delete(:root)
|
55
|
+
root = options.delete(:root).to_s
|
38
56
|
children = options.delete(:children)
|
39
57
|
|
58
|
+
if !options.has_key?(:dasherize) || options[:dasherize]
|
59
|
+
root = root.dasherize
|
60
|
+
end
|
61
|
+
|
40
62
|
options[:builder].instruct! unless options.delete(:skip_instruct)
|
41
|
-
|
63
|
+
|
64
|
+
opts = options.merge({ :root => children })
|
65
|
+
|
66
|
+
options[:builder].tag!(root) { each { |e| e.to_xml(opts.merge!({ :skip_instruct => true })) } }
|
42
67
|
end
|
68
|
+
|
43
69
|
end
|
44
70
|
end
|
45
71
|
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
module ActiveSupport #:nodoc:
|
2
|
+
module CoreExtensions #:nodoc:
|
3
|
+
module Array #:nodoc:
|
4
|
+
module Grouping
|
5
|
+
# Iterate over an array in groups of a certain size, padding any remaining
|
6
|
+
# slots with specified value (<tt>nil</tt> by default) unless it is
|
7
|
+
# <tt>false</tt>.
|
8
|
+
#
|
9
|
+
# E.g.
|
10
|
+
#
|
11
|
+
# %w(1 2 3 4 5 6 7).in_groups_of(3) {|g| p g}
|
12
|
+
# ["1", "2", "3"]
|
13
|
+
# ["4", "5", "6"]
|
14
|
+
# ["7", nil, nil]
|
15
|
+
#
|
16
|
+
# %w(1 2 3).in_groups_of(2, ' ') {|g| p g}
|
17
|
+
# ["1", "2"]
|
18
|
+
# ["3", " "]
|
19
|
+
#
|
20
|
+
# %w(1 2 3).in_groups_of(2, false) {|g| p g}
|
21
|
+
# ["1", "2"]
|
22
|
+
# ["3"]
|
23
|
+
def in_groups_of(number, fill_with = nil, &block)
|
24
|
+
require 'enumerator'
|
25
|
+
collection = dup
|
26
|
+
collection << fill_with until collection.size.modulo(number).zero? unless fill_with == false
|
27
|
+
grouped_collection = [] unless block_given?
|
28
|
+
collection.each_slice(number) do |group|
|
29
|
+
block_given? ? yield(group) : grouped_collection << group
|
30
|
+
end
|
31
|
+
grouped_collection unless block_given?
|
32
|
+
end
|
33
|
+
|
34
|
+
# Divide the array into one or more subarrays based on a delimiting +value+
|
35
|
+
# or the result of an optional block.
|
36
|
+
#
|
37
|
+
# ex.
|
38
|
+
#
|
39
|
+
# [1, 2, 3, 4, 5].split(3) # => [[1, 2], [4, 5]]
|
40
|
+
# (1..10).to_a.split { |i| i % 3 == 0 } # => [[1, 2], [4, 5], [7, 8], [10]]
|
41
|
+
def split(value = nil, &block)
|
42
|
+
block ||= Proc.new { |e| e == value }
|
43
|
+
inject([[]]) do |results, element|
|
44
|
+
if block.call(element)
|
45
|
+
results << []
|
46
|
+
else
|
47
|
+
results.last << element
|
48
|
+
end
|
49
|
+
results
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|