activesupport 3.2.22.5 → 4.0.0.beta1

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.

Files changed (214) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +325 -136
  3. data/MIT-LICENSE +1 -1
  4. data/README.rdoc +4 -2
  5. data/lib/active_support.rb +8 -21
  6. data/lib/active_support/backtrace_cleaner.rb +33 -25
  7. data/lib/active_support/basic_object.rb +7 -17
  8. data/lib/active_support/benchmarkable.rb +19 -15
  9. data/lib/active_support/buffered_logger.rb +9 -113
  10. data/lib/active_support/cache.rb +203 -171
  11. data/lib/active_support/cache/file_store.rb +12 -12
  12. data/lib/active_support/cache/mem_cache_store.rb +24 -30
  13. data/lib/active_support/cache/memory_store.rb +2 -0
  14. data/lib/active_support/callbacks.rb +195 -247
  15. data/lib/active_support/concern.rb +16 -23
  16. data/lib/active_support/concurrency/latch.rb +27 -0
  17. data/lib/active_support/configurable.rb +69 -12
  18. data/lib/active_support/core_ext.rb +1 -0
  19. data/lib/active_support/core_ext/array.rb +0 -1
  20. data/lib/active_support/core_ext/array/access.rb +17 -9
  21. data/lib/active_support/core_ext/array/conversions.rb +113 -55
  22. data/lib/active_support/core_ext/array/extract_options.rb +2 -2
  23. data/lib/active_support/core_ext/array/grouping.rb +21 -22
  24. data/lib/active_support/core_ext/array/uniq_by.rb +12 -9
  25. data/lib/active_support/core_ext/array/wrap.rb +11 -14
  26. data/lib/active_support/core_ext/big_decimal/conversions.rb +7 -24
  27. data/lib/active_support/core_ext/class/attribute.rb +12 -8
  28. data/lib/active_support/core_ext/class/attribute_accessors.rb +14 -12
  29. data/lib/active_support/core_ext/class/delegating_attributes.rb +15 -19
  30. data/lib/active_support/core_ext/class/subclasses.rb +11 -5
  31. data/lib/active_support/core_ext/date.rb +6 -0
  32. data/lib/active_support/core_ext/date/calculations.rb +34 -188
  33. data/lib/active_support/core_ext/date/conversions.rb +16 -38
  34. data/lib/active_support/core_ext/date/infinite_comparable.rb +5 -0
  35. data/lib/active_support/core_ext/date/zones.rb +25 -2
  36. data/lib/active_support/core_ext/date_and_time/calculations.rb +232 -0
  37. data/lib/active_support/core_ext/date_time.rb +5 -0
  38. data/lib/active_support/core_ext/date_time/acts_like.rb +0 -1
  39. data/lib/active_support/core_ext/date_time/calculations.rb +73 -65
  40. data/lib/active_support/core_ext/date_time/conversions.rb +21 -33
  41. data/lib/active_support/core_ext/date_time/infinite_comparable.rb +5 -0
  42. data/lib/active_support/core_ext/date_time/zones.rb +11 -8
  43. data/lib/active_support/core_ext/enumerable.rb +26 -73
  44. data/lib/active_support/core_ext/file.rb +0 -1
  45. data/lib/active_support/core_ext/file/atomic.rb +27 -11
  46. data/lib/active_support/core_ext/hash.rb +0 -1
  47. data/lib/active_support/core_ext/hash/conversions.rb +145 -79
  48. data/lib/active_support/core_ext/hash/deep_merge.rb +14 -8
  49. data/lib/active_support/core_ext/hash/diff.rb +5 -4
  50. data/lib/active_support/core_ext/hash/except.rb +1 -9
  51. data/lib/active_support/core_ext/hash/indifferent_access.rb +4 -5
  52. data/lib/active_support/core_ext/hash/keys.rb +108 -24
  53. data/lib/active_support/core_ext/hash/reverse_merge.rb +2 -3
  54. data/lib/active_support/core_ext/hash/slice.rb +12 -12
  55. data/lib/active_support/core_ext/infinite_comparable.rb +35 -0
  56. data/lib/active_support/core_ext/integer/inflections.rb +13 -1
  57. data/lib/active_support/core_ext/integer/time.rb +17 -12
  58. data/lib/active_support/core_ext/kernel/debugger.rb +2 -2
  59. data/lib/active_support/core_ext/kernel/reporting.rb +36 -22
  60. data/lib/active_support/core_ext/kernel/singleton_class.rb +0 -7
  61. data/lib/active_support/core_ext/load_error.rb +7 -5
  62. data/lib/active_support/core_ext/logger.rb +7 -23
  63. data/lib/active_support/core_ext/marshal.rb +19 -0
  64. data/lib/active_support/core_ext/module.rb +1 -3
  65. data/lib/active_support/core_ext/module/aliasing.rb +8 -9
  66. data/lib/active_support/core_ext/module/anonymous.rb +2 -7
  67. data/lib/active_support/core_ext/module/attr_internal.rb +0 -1
  68. data/lib/active_support/core_ext/module/attribute_accessors.rb +12 -10
  69. data/lib/active_support/core_ext/module/delegation.rb +57 -40
  70. data/lib/active_support/core_ext/module/deprecation.rb +19 -3
  71. data/lib/active_support/core_ext/module/introspection.rb +17 -27
  72. data/lib/active_support/core_ext/module/qualified_const.rb +8 -20
  73. data/lib/active_support/core_ext/module/remove_method.rb +1 -5
  74. data/lib/active_support/core_ext/numeric.rb +2 -0
  75. data/lib/active_support/core_ext/numeric/conversions.rb +135 -0
  76. data/lib/active_support/core_ext/numeric/infinite_comparable.rb +9 -0
  77. data/lib/active_support/core_ext/numeric/time.rb +6 -6
  78. data/lib/active_support/core_ext/object.rb +1 -0
  79. data/lib/active_support/core_ext/object/acts_like.rb +4 -4
  80. data/lib/active_support/core_ext/object/blank.rb +7 -23
  81. data/lib/active_support/core_ext/object/deep_dup.rb +46 -0
  82. data/lib/active_support/core_ext/object/duplicable.rb +1 -30
  83. data/lib/active_support/core_ext/object/inclusion.rb +6 -6
  84. data/lib/active_support/core_ext/object/instance_variables.rb +7 -12
  85. data/lib/active_support/core_ext/object/to_json.rb +8 -0
  86. data/lib/active_support/core_ext/object/to_param.rb +5 -2
  87. data/lib/active_support/core_ext/object/try.rb +46 -25
  88. data/lib/active_support/core_ext/object/with_options.rb +7 -8
  89. data/lib/active_support/core_ext/proc.rb +3 -0
  90. data/lib/active_support/core_ext/range.rb +0 -2
  91. data/lib/active_support/core_ext/range/conversions.rb +0 -2
  92. data/lib/active_support/core_ext/range/include_range.rb +1 -1
  93. data/lib/active_support/core_ext/range/overlaps.rb +1 -1
  94. data/lib/active_support/core_ext/string.rb +2 -2
  95. data/lib/active_support/core_ext/string/access.rb +95 -90
  96. data/lib/active_support/core_ext/string/conversions.rb +29 -38
  97. data/lib/active_support/core_ext/string/encoding.rb +6 -9
  98. data/lib/active_support/core_ext/string/filters.rb +24 -18
  99. data/lib/active_support/core_ext/string/indent.rb +43 -0
  100. data/lib/active_support/core_ext/string/inflections.rb +70 -60
  101. data/lib/active_support/core_ext/string/inquiry.rb +2 -2
  102. data/lib/active_support/core_ext/string/multibyte.rb +41 -64
  103. data/lib/active_support/core_ext/string/output_safety.rb +59 -51
  104. data/lib/active_support/core_ext/string/zones.rb +13 -0
  105. data/lib/active_support/core_ext/struct.rb +6 -0
  106. data/lib/active_support/core_ext/thread.rb +74 -0
  107. data/lib/active_support/core_ext/time.rb +6 -0
  108. data/lib/active_support/core_ext/time/calculations.rb +105 -193
  109. data/lib/active_support/core_ext/time/conversions.rb +27 -51
  110. data/lib/active_support/core_ext/time/infinite_comparable.rb +5 -0
  111. data/lib/active_support/core_ext/time/marshal.rb +0 -27
  112. data/lib/active_support/core_ext/time/zones.rb +27 -17
  113. data/lib/active_support/core_ext/uri.rb +13 -17
  114. data/lib/active_support/dependencies.rb +160 -141
  115. data/lib/active_support/dependencies/autoload.rb +47 -20
  116. data/lib/active_support/deprecation.rb +39 -14
  117. data/lib/active_support/deprecation/behaviors.rb +44 -30
  118. data/lib/active_support/deprecation/instance_delegator.rb +24 -0
  119. data/lib/active_support/deprecation/method_wrappers.rb +33 -18
  120. data/lib/active_support/deprecation/proxy_wrappers.rb +58 -13
  121. data/lib/active_support/deprecation/reporting.rb +40 -11
  122. data/lib/active_support/descendants_tracker.rb +34 -19
  123. data/lib/active_support/duration.rb +6 -8
  124. data/lib/active_support/file_update_checker.rb +63 -47
  125. data/lib/active_support/gzip.rb +11 -5
  126. data/lib/active_support/hash_with_indifferent_access.rb +112 -37
  127. data/lib/active_support/i18n.rb +4 -0
  128. data/lib/active_support/i18n_railtie.rb +5 -22
  129. data/lib/active_support/inflections.rb +14 -12
  130. data/lib/active_support/inflector/inflections.rb +108 -71
  131. data/lib/active_support/inflector/methods.rb +181 -160
  132. data/lib/active_support/inflector/transliterate.rb +16 -17
  133. data/lib/active_support/json/decoding.rb +18 -17
  134. data/lib/active_support/json/encoding.rb +93 -39
  135. data/lib/active_support/json/variable.rb +10 -1
  136. data/lib/active_support/key_generator.rb +75 -0
  137. data/lib/active_support/lazy_load_hooks.rb +21 -19
  138. data/lib/active_support/locale/en.yml +100 -3
  139. data/lib/active_support/log_subscriber.rb +56 -36
  140. data/lib/active_support/log_subscriber/test_helper.rb +18 -15
  141. data/lib/active_support/logger.rb +57 -0
  142. data/lib/active_support/logger_silence.rb +24 -0
  143. data/lib/active_support/message_encryptor.rb +32 -29
  144. data/lib/active_support/message_verifier.rb +8 -14
  145. data/lib/active_support/multibyte.rb +5 -28
  146. data/lib/active_support/multibyte/chars.rb +80 -333
  147. data/lib/active_support/multibyte/unicode.rb +74 -64
  148. data/lib/active_support/notifications.rb +57 -25
  149. data/lib/active_support/notifications/fanout.rb +105 -18
  150. data/lib/active_support/notifications/instrumenter.rb +32 -13
  151. data/lib/active_support/number_helper.rb +636 -0
  152. data/lib/active_support/ordered_hash.rb +8 -190
  153. data/lib/active_support/ordered_options.rb +21 -23
  154. data/lib/active_support/proxy_object.rb +13 -0
  155. data/lib/active_support/rails.rb +27 -0
  156. data/lib/active_support/railtie.rb +12 -32
  157. data/lib/active_support/rescuable.rb +9 -4
  158. data/lib/active_support/string_inquirer.rb +13 -8
  159. data/lib/active_support/tagged_logging.rb +51 -73
  160. data/lib/active_support/test_case.rb +46 -17
  161. data/lib/active_support/testing/assertions.rb +56 -26
  162. data/lib/active_support/testing/autorun.rb +5 -0
  163. data/lib/active_support/testing/constant_lookup.rb +52 -0
  164. data/lib/active_support/testing/declarative.rb +1 -1
  165. data/lib/active_support/testing/deprecation.rb +0 -19
  166. data/lib/active_support/testing/isolation.rb +25 -58
  167. data/lib/active_support/testing/pending.rb +5 -43
  168. data/lib/active_support/testing/setup_and_teardown.rb +6 -92
  169. data/lib/active_support/testing/tagged_logging.rb +25 -0
  170. data/lib/active_support/time.rb +6 -21
  171. data/lib/active_support/time_with_zone.rb +78 -43
  172. data/lib/active_support/values/time_zone.rb +77 -58
  173. data/lib/active_support/values/unicode_tables.dat +0 -0
  174. data/lib/active_support/version.rb +4 -4
  175. data/lib/active_support/xml_mini.rb +35 -17
  176. data/lib/active_support/xml_mini/jdom.rb +9 -17
  177. data/lib/active_support/xml_mini/libxml.rb +1 -2
  178. data/lib/active_support/xml_mini/libxmlsax.rb +1 -2
  179. data/lib/active_support/xml_mini/nokogiri.rb +1 -2
  180. data/lib/active_support/xml_mini/nokogirisax.rb +1 -2
  181. data/lib/active_support/xml_mini/rexml.rb +6 -8
  182. metadata +107 -77
  183. data/lib/active_support/base64.rb +0 -54
  184. data/lib/active_support/core_ext/array/random_access.rb +0 -30
  185. data/lib/active_support/core_ext/date/freeze.rb +0 -33
  186. data/lib/active_support/core_ext/exception.rb +0 -3
  187. data/lib/active_support/core_ext/file/path.rb +0 -5
  188. data/lib/active_support/core_ext/float.rb +0 -1
  189. data/lib/active_support/core_ext/float/rounding.rb +0 -19
  190. data/lib/active_support/core_ext/hash/deep_dup.rb +0 -18
  191. data/lib/active_support/core_ext/io.rb +0 -15
  192. data/lib/active_support/core_ext/module/method_names.rb +0 -14
  193. data/lib/active_support/core_ext/module/synchronization.rb +0 -45
  194. data/lib/active_support/core_ext/process.rb +0 -1
  195. data/lib/active_support/core_ext/process/daemon.rb +0 -23
  196. data/lib/active_support/core_ext/range/blockless_step.rb +0 -29
  197. data/lib/active_support/core_ext/range/cover.rb +0 -3
  198. data/lib/active_support/core_ext/rexml.rb +0 -46
  199. data/lib/active_support/core_ext/string/interpolation.rb +0 -2
  200. data/lib/active_support/core_ext/time/publicize_conversion_methods.rb +0 -10
  201. data/lib/active_support/memoizable.rb +0 -116
  202. data/lib/active_support/multibyte/exceptions.rb +0 -8
  203. data/lib/active_support/multibyte/utils.rb +0 -60
  204. data/lib/active_support/ruby/shim.rb +0 -22
  205. data/lib/active_support/security_utils.rb +0 -27
  206. data/lib/active_support/testing/mochaing.rb +0 -7
  207. data/lib/active_support/testing/performance.rb +0 -317
  208. data/lib/active_support/testing/performance/jruby.rb +0 -115
  209. data/lib/active_support/testing/performance/rubinius.rb +0 -113
  210. data/lib/active_support/testing/performance/ruby.rb +0 -152
  211. data/lib/active_support/testing/performance/ruby/mri.rb +0 -57
  212. data/lib/active_support/testing/performance/ruby/yarv.rb +0 -57
  213. data/lib/active_support/time/autoload.rb +0 -5
  214. data/lib/active_support/whiny_nil.rb +0 -24
@@ -1,4 +1,7 @@
1
1
  begin
2
+ require 'active_support/core_ext/hash/deep_merge'
3
+ require 'active_support/core_ext/hash/except'
4
+ require 'active_support/core_ext/hash/slice'
2
5
  require 'i18n'
3
6
  require 'active_support/lazy_load_hooks'
4
7
  rescue LoadError => e
@@ -6,4 +9,5 @@ rescue LoadError => e
6
9
  raise e
7
10
  end
8
11
 
12
+ ActiveSupport.run_load_hooks(:i18n)
9
13
  I18n.load_path << "#{File.dirname(__FILE__)}/locale/en.yml"
@@ -9,25 +9,6 @@ module I18n
9
9
  config.i18n.load_path = []
10
10
  config.i18n.fallbacks = ActiveSupport::OrderedOptions.new
11
11
 
12
- def self.reloader
13
- @reloader ||= ActiveSupport::FileUpdateChecker.new(reloader_paths){ I18n.reload! }
14
- end
15
-
16
- def self.reloader_paths
17
- @reloader_paths ||= []
18
- end
19
-
20
- # Add <tt>I18n::Railtie.reloader</tt> to ActionDispatch callbacks. Since, at this
21
- # point, no path was added to the reloader, I18n.reload! is not triggered
22
- # on to_prepare callbacks. This will only happen on the config.after_initialize
23
- # callback below.
24
- initializer "i18n.callbacks" do |app|
25
- app.reloaders << I18n::Railtie.reloader
26
- ActionDispatch::Reloader.to_prepare do
27
- I18n::Railtie.reloader.execute_if_updated
28
- end
29
- end
30
-
31
12
  # Set the i18n configuration after initialization since a lot of
32
13
  # configuration is still usually done in application initializers.
33
14
  config.after_initialize do |app|
@@ -35,7 +16,7 @@ module I18n
35
16
  end
36
17
 
37
18
  # Trigger i18n config before any eager loading has happened
38
- # so it's ready if any classes require it when eager loaded
19
+ # so it's ready if any classes require it when eager loaded.
39
20
  config.before_eager_load do |app|
40
21
  I18n::Railtie.initialize_i18n(app)
41
22
  end
@@ -44,7 +25,7 @@ module I18n
44
25
 
45
26
  @i18n_inited = false
46
27
 
47
- # Setup i18n configuration
28
+ # Setup i18n configuration.
48
29
  def self.initialize_i18n(app)
49
30
  return if @i18n_inited
50
31
 
@@ -63,7 +44,9 @@ module I18n
63
44
 
64
45
  init_fallbacks(fallbacks) if fallbacks && validate_fallbacks(fallbacks)
65
46
 
66
- reloader_paths.concat I18n.load_path
47
+ reloader = ActiveSupport::FileUpdateChecker.new(I18n.load_path.dup){ I18n.reload! }
48
+ app.reloaders << reloader
49
+ ActionDispatch::Reloader.to_prepare { reloader.execute_if_updated }
67
50
  reloader.execute
68
51
 
69
52
  @i18n_inited = true
@@ -1,10 +1,10 @@
1
1
  require 'active_support/inflector/inflections'
2
2
 
3
3
  module ActiveSupport
4
- Inflector.inflections do |inflect|
4
+ Inflector.inflections(:en) do |inflect|
5
5
  inflect.plural(/$/, 's')
6
6
  inflect.plural(/s$/i, 's')
7
- inflect.plural(/(ax|test)is$/i, '\1es')
7
+ inflect.plural(/^(ax|test)is$/i, '\1es')
8
8
  inflect.plural(/(octop|vir)us$/i, '\1i')
9
9
  inflect.plural(/(octop|vir)i$/i, '\1i')
10
10
  inflect.plural(/(alias|status)$/i, '\1es')
@@ -18,17 +18,18 @@ module ActiveSupport
18
18
  inflect.plural(/([^aeiouy]|qu)y$/i, '\1ies')
19
19
  inflect.plural(/(x|ch|ss|sh)$/i, '\1es')
20
20
  inflect.plural(/(matr|vert|ind)(?:ix|ex)$/i, '\1ices')
21
- inflect.plural(/(m|l)ouse$/i, '\1ice')
22
- inflect.plural(/(m|l)ice$/i, '\1ice')
21
+ inflect.plural(/^(m|l)ouse$/i, '\1ice')
22
+ inflect.plural(/^(m|l)ice$/i, '\1ice')
23
23
  inflect.plural(/^(ox)$/i, '\1en')
24
24
  inflect.plural(/^(oxen)$/i, '\1')
25
25
  inflect.plural(/(quiz)$/i, '\1zes')
26
26
 
27
27
  inflect.singular(/s$/i, '')
28
+ inflect.singular(/(ss)$/i, '\1')
28
29
  inflect.singular(/(n)ews$/i, '\1ews')
29
30
  inflect.singular(/([ti])a$/i, '\1um')
30
- inflect.singular(/((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/i, '\1\2sis')
31
- inflect.singular(/(^analy)ses$/i, '\1sis')
31
+ inflect.singular(/((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)(sis|ses)$/i, '\1sis')
32
+ inflect.singular(/(^analy)(sis|ses)$/i, '\1sis')
32
33
  inflect.singular(/([^f])ves$/i, '\1fe')
33
34
  inflect.singular(/(hive)s$/i, '\1')
34
35
  inflect.singular(/(tive)s$/i, '\1')
@@ -37,13 +38,14 @@ module ActiveSupport
37
38
  inflect.singular(/(s)eries$/i, '\1eries')
38
39
  inflect.singular(/(m)ovies$/i, '\1ovie')
39
40
  inflect.singular(/(x|ch|ss|sh)es$/i, '\1')
40
- inflect.singular(/(m|l)ice$/i, '\1ouse')
41
- inflect.singular(/(bus)es$/i, '\1')
41
+ inflect.singular(/^(m|l)ice$/i, '\1ouse')
42
+ inflect.singular(/(bus)(es)?$/i, '\1')
42
43
  inflect.singular(/(o)es$/i, '\1')
43
44
  inflect.singular(/(shoe)s$/i, '\1')
44
- inflect.singular(/(cris|ax|test)es$/i, '\1is')
45
- inflect.singular(/(octop|vir)i$/i, '\1us')
46
- inflect.singular(/(alias|status)es$/i, '\1')
45
+ inflect.singular(/(cris|test)(is|es)$/i, '\1is')
46
+ inflect.singular(/^(a)x[ie]s$/i, '\1xis')
47
+ inflect.singular(/(octop|vir)(us|i)$/i, '\1us')
48
+ inflect.singular(/(alias|status)(es)?$/i, '\1')
47
49
  inflect.singular(/^(ox)en/i, '\1')
48
50
  inflect.singular(/(vert|ind)ices$/i, '\1ex')
49
51
  inflect.singular(/(matr)ices$/i, '\1ix')
@@ -58,6 +60,6 @@ module ActiveSupport
58
60
  inflect.irregular('cow', 'kine')
59
61
  inflect.irregular('zombie', 'zombies')
60
62
 
61
- inflect.uncountable(%w(equipment information rice money species series fish sheep jeans))
63
+ inflect.uncountable(%w(equipment information rice money species series fish sheep jeans police))
62
64
  end
63
65
  end
@@ -1,25 +1,34 @@
1
+ require 'thread_safe'
2
+ require 'active_support/core_ext/array/prepend_and_append'
3
+ require 'active_support/i18n'
4
+
1
5
  module ActiveSupport
2
6
  module Inflector
3
7
  extend self
4
8
 
5
- # A singleton instance of this class is yielded by Inflector.inflections, which can then be used to specify additional
6
- # inflection rules. Examples:
9
+ # A singleton instance of this class is yielded by Inflector.inflections,
10
+ # which can then be used to specify additional inflection rules. If passed
11
+ # an optional locale, rules for other languages can be specified. The
12
+ # default locale is <tt>:en</tt>. Only rules for English are provided.
7
13
  #
8
- # ActiveSupport::Inflector.inflections do |inflect|
14
+ # ActiveSupport::Inflector.inflections(:en) do |inflect|
9
15
  # inflect.plural /^(ox)$/i, '\1\2en'
10
16
  # inflect.singular /^(ox)en/i, '\1'
11
17
  #
12
18
  # inflect.irregular 'octopus', 'octopi'
13
19
  #
14
- # inflect.uncountable "equipment"
20
+ # inflect.uncountable 'equipment'
15
21
  # end
16
22
  #
17
- # New rules are added at the top. So in the example above, the irregular rule for octopus will now be the first of the
18
- # pluralization and singularization rules that is runs. This guarantees that your rules run before any of the rules that may
19
- # already have been loaded.
23
+ # New rules are added at the top. So in the example above, the irregular
24
+ # rule for octopus will now be the first of the pluralization and
25
+ # singularization rules that is runs. This guarantees that your rules run
26
+ # before any of the rules that may already have been loaded.
20
27
  class Inflections
21
- def self.instance
22
- @__instance__ ||= new
28
+ @__instance__ = ThreadSafe::Cache.new
29
+
30
+ def self.instance(locale = :en)
31
+ @__instance__[locale] ||= new
23
32
  end
24
33
 
25
34
  attr_reader :plurals, :singulars, :uncountables, :humans, :acronyms, :acronym_regex
@@ -28,31 +37,41 @@ module ActiveSupport
28
37
  @plurals, @singulars, @uncountables, @humans, @acronyms, @acronym_regex = [], [], [], [], {}, /(?=a)b/
29
38
  end
30
39
 
31
- # Specifies a new acronym. An acronym must be specified as it will appear in a camelized string. An underscore
32
- # string that contains the acronym will retain the acronym when passed to `camelize`, `humanize`, or `titleize`.
33
- # A camelized string that contains the acronym will maintain the acronym when titleized or humanized, and will
34
- # convert the acronym into a non-delimited single lowercase word when passed to +underscore+.
40
+ # Private, for the test suite.
41
+ def initialize_dup(orig) # :nodoc:
42
+ %w(plurals singulars uncountables humans acronyms acronym_regex).each do |scope|
43
+ instance_variable_set("@#{scope}", orig.send(scope).dup)
44
+ end
45
+ end
46
+
47
+ # Specifies a new acronym. An acronym must be specified as it will appear
48
+ # in a camelized string. An underscore string that contains the acronym
49
+ # will retain the acronym when passed to +camelize+, +humanize+, or
50
+ # +titleize+. A camelized string that contains the acronym will maintain
51
+ # the acronym when titleized or humanized, and will convert the acronym
52
+ # into a non-delimited single lowercase word when passed to +underscore+.
35
53
  #
36
- # Examples:
37
54
  # acronym 'HTML'
38
- # titleize 'html' #=> 'HTML'
39
- # camelize 'html' #=> 'HTML'
55
+ # titleize 'html' #=> 'HTML'
56
+ # camelize 'html' #=> 'HTML'
40
57
  # underscore 'MyHTML' #=> 'my_html'
41
58
  #
42
- # The acronym, however, must occur as a delimited unit and not be part of another word for conversions to recognize it:
59
+ # The acronym, however, must occur as a delimited unit and not be part of
60
+ # another word for conversions to recognize it:
43
61
  #
44
62
  # acronym 'HTTP'
45
63
  # camelize 'my_http_delimited' #=> 'MyHTTPDelimited'
46
- # camelize 'https' #=> 'Https', not 'HTTPs'
47
- # underscore 'HTTPS' #=> 'http_s', not 'https'
64
+ # camelize 'https' #=> 'Https', not 'HTTPs'
65
+ # underscore 'HTTPS' #=> 'http_s', not 'https'
48
66
  #
49
67
  # acronym 'HTTPS'
50
- # camelize 'https' #=> 'HTTPS'
68
+ # camelize 'https' #=> 'HTTPS'
51
69
  # underscore 'HTTPS' #=> 'https'
52
70
  #
53
- # Note: Acronyms that are passed to `pluralize` will no longer be recognized, since the acronym will not occur as
54
- # a delimited unit in the pluralized result. To work around this, you must specify the pluralized form as an
55
- # acronym as well:
71
+ # Note: Acronyms that are passed to +pluralize+ will no longer be
72
+ # recognized, since the acronym will not occur as a delimited unit in the
73
+ # pluralized result. To work around this, you must specify the pluralized
74
+ # form as an acronym as well:
56
75
  #
57
76
  # acronym 'API'
58
77
  # camelize(pluralize('api')) #=> 'Apis'
@@ -60,90 +79,107 @@ module ActiveSupport
60
79
  # acronym 'APIs'
61
80
  # camelize(pluralize('api')) #=> 'APIs'
62
81
  #
63
- # `acronym` may be used to specify any word that contains an acronym or otherwise needs to maintain a non-standard
64
- # capitalization. The only restriction is that the word must begin with a capital letter.
82
+ # +acronym+ may be used to specify any word that contains an acronym or
83
+ # otherwise needs to maintain a non-standard capitalization. The only
84
+ # restriction is that the word must begin with a capital letter.
65
85
  #
66
- # Examples:
67
86
  # acronym 'RESTful'
68
- # underscore 'RESTful' #=> 'restful'
87
+ # underscore 'RESTful' #=> 'restful'
69
88
  # underscore 'RESTfulController' #=> 'restful_controller'
70
- # titleize 'RESTfulController' #=> 'RESTful Controller'
71
- # camelize 'restful' #=> 'RESTful'
72
- # camelize 'restful_controller' #=> 'RESTfulController'
89
+ # titleize 'RESTfulController' #=> 'RESTful Controller'
90
+ # camelize 'restful' #=> 'RESTful'
91
+ # camelize 'restful_controller' #=> 'RESTfulController'
73
92
  #
74
93
  # acronym 'McDonald'
75
94
  # underscore 'McDonald' #=> 'mcdonald'
76
- # camelize 'mcdonald' #=> 'McDonald'
95
+ # camelize 'mcdonald' #=> 'McDonald'
77
96
  def acronym(word)
78
97
  @acronyms[word.downcase] = word
79
98
  @acronym_regex = /#{@acronyms.values.join("|")}/
80
99
  end
81
100
 
82
- # Specifies a new pluralization rule and its replacement. The rule can either be a string or a regular expression.
83
- # The replacement should always be a string that may include references to the matched data from the rule.
101
+ # Specifies a new pluralization rule and its replacement. The rule can
102
+ # either be a string or a regular expression. The replacement should
103
+ # always be a string that may include references to the matched data from
104
+ # the rule.
84
105
  def plural(rule, replacement)
85
106
  @uncountables.delete(rule) if rule.is_a?(String)
86
107
  @uncountables.delete(replacement)
87
- @plurals.insert(0, [rule, replacement])
108
+ @plurals.prepend([rule, replacement])
88
109
  end
89
110
 
90
- # Specifies a new singularization rule and its replacement. The rule can either be a string or a regular expression.
91
- # The replacement should always be a string that may include references to the matched data from the rule.
111
+ # Specifies a new singularization rule and its replacement. The rule can
112
+ # either be a string or a regular expression. The replacement should
113
+ # always be a string that may include references to the matched data from
114
+ # the rule.
92
115
  def singular(rule, replacement)
93
116
  @uncountables.delete(rule) if rule.is_a?(String)
94
117
  @uncountables.delete(replacement)
95
- @singulars.insert(0, [rule, replacement])
118
+ @singulars.prepend([rule, replacement])
96
119
  end
97
120
 
98
- # Specifies a new irregular that applies to both pluralization and singularization at the same time. This can only be used
99
- # for strings, not regular expressions. You simply pass the irregular in singular and plural form.
121
+ # Specifies a new irregular that applies to both pluralization and
122
+ # singularization at the same time. This can only be used for strings, not
123
+ # regular expressions. You simply pass the irregular in singular and
124
+ # plural form.
100
125
  #
101
- # Examples:
102
126
  # irregular 'octopus', 'octopi'
103
127
  # irregular 'person', 'people'
104
128
  def irregular(singular, plural)
105
129
  @uncountables.delete(singular)
106
130
  @uncountables.delete(plural)
107
- if singular[0,1].upcase == plural[0,1].upcase
108
- plural(Regexp.new("(#{singular[0,1]})#{singular[1..-1]}$", "i"), '\1' + plural[1..-1])
109
- plural(Regexp.new("(#{plural[0,1]})#{plural[1..-1]}$", "i"), '\1' + plural[1..-1])
110
- singular(Regexp.new("(#{plural[0,1]})#{plural[1..-1]}$", "i"), '\1' + singular[1..-1])
131
+
132
+ s0 = singular[0]
133
+ srest = singular[1..-1]
134
+
135
+ p0 = plural[0]
136
+ prest = plural[1..-1]
137
+
138
+ if s0.upcase == p0.upcase
139
+ plural(/(#{s0})#{srest}$/i, '\1' + prest)
140
+ plural(/(#{p0})#{prest}$/i, '\1' + prest)
141
+
142
+ singular(/(#{s0})#{srest}$/i, '\1' + srest)
143
+ singular(/(#{p0})#{prest}$/i, '\1' + srest)
111
144
  else
112
- plural(Regexp.new("#{singular[0,1].upcase}(?i)#{singular[1..-1]}$"), plural[0,1].upcase + plural[1..-1])
113
- plural(Regexp.new("#{singular[0,1].downcase}(?i)#{singular[1..-1]}$"), plural[0,1].downcase + plural[1..-1])
114
- plural(Regexp.new("#{plural[0,1].upcase}(?i)#{plural[1..-1]}$"), plural[0,1].upcase + plural[1..-1])
115
- plural(Regexp.new("#{plural[0,1].downcase}(?i)#{plural[1..-1]}$"), plural[0,1].downcase + plural[1..-1])
116
- singular(Regexp.new("#{plural[0,1].upcase}(?i)#{plural[1..-1]}$"), singular[0,1].upcase + singular[1..-1])
117
- singular(Regexp.new("#{plural[0,1].downcase}(?i)#{plural[1..-1]}$"), singular[0,1].downcase + singular[1..-1])
145
+ plural(/#{s0.upcase}(?i)#{srest}$/, p0.upcase + prest)
146
+ plural(/#{s0.downcase}(?i)#{srest}$/, p0.downcase + prest)
147
+ plural(/#{p0.upcase}(?i)#{prest}$/, p0.upcase + prest)
148
+ plural(/#{p0.downcase}(?i)#{prest}$/, p0.downcase + prest)
149
+
150
+ singular(/#{s0.upcase}(?i)#{srest}$/, s0.upcase + srest)
151
+ singular(/#{s0.downcase}(?i)#{srest}$/, s0.downcase + srest)
152
+ singular(/#{p0.upcase}(?i)#{prest}$/, s0.upcase + srest)
153
+ singular(/#{p0.downcase}(?i)#{prest}$/, s0.downcase + srest)
118
154
  end
119
155
  end
120
156
 
121
157
  # Add uncountable words that shouldn't be attempted inflected.
122
158
  #
123
- # Examples:
124
- # uncountable "money"
125
- # uncountable "money", "information"
159
+ # uncountable 'money'
160
+ # uncountable 'money', 'information'
126
161
  # uncountable %w( money information rice )
127
162
  def uncountable(*words)
128
163
  (@uncountables << words).flatten!
129
164
  end
130
165
 
131
- # Specifies a humanized form of a string by a regular expression rule or by a string mapping.
132
- # When using a regular expression based replacement, the normal humanize formatting is called after the replacement.
133
- # When a string is used, the human form should be specified as desired (example: 'The name', not 'the_name')
166
+ # Specifies a humanized form of a string by a regular expression rule or
167
+ # by a string mapping. When using a regular expression based replacement,
168
+ # the normal humanize formatting is called after the replacement. When a
169
+ # string is used, the human form should be specified as desired (example:
170
+ # 'The name', not 'the_name').
134
171
  #
135
- # Examples:
136
172
  # human /_cnt$/i, '\1_count'
137
- # human "legacy_col_person_name", "Name"
173
+ # human 'legacy_col_person_name', 'Name'
138
174
  def human(rule, replacement)
139
- @humans.insert(0, [rule, replacement])
175
+ @humans.prepend([rule, replacement])
140
176
  end
141
177
 
142
- # Clears the loaded inflections within a given scope (default is <tt>:all</tt>).
143
- # Give the scope as a symbol of the inflection type, the options are: <tt>:plurals</tt>,
144
- # <tt>:singulars</tt>, <tt>:uncountables</tt>, <tt>:humans</tt>.
178
+ # Clears the loaded inflections within a given scope (default is
179
+ # <tt>:all</tt>). Give the scope as a symbol of the inflection type, the
180
+ # options are: <tt>:plurals</tt>, <tt>:singulars</tt>, <tt>:uncountables</tt>,
181
+ # <tt>:humans</tt>.
145
182
  #
146
- # Examples:
147
183
  # clear :all
148
184
  # clear :plurals
149
185
  def clear(scope = :all)
@@ -156,18 +192,19 @@ module ActiveSupport
156
192
  end
157
193
  end
158
194
 
159
- # Yields a singleton instance of Inflector::Inflections so you can specify additional
160
- # inflector rules.
195
+ # Yields a singleton instance of Inflector::Inflections so you can specify
196
+ # additional inflector rules. If passed an optional locale, rules for other
197
+ # languages can be specified. If not specified, defaults to <tt>:en</tt>.
198
+ # Only rules for English are provided.
161
199
  #
162
- # Example:
163
- # ActiveSupport::Inflector.inflections do |inflect|
164
- # inflect.uncountable "rails"
200
+ # ActiveSupport::Inflector.inflections(:en) do |inflect|
201
+ # inflect.uncountable 'rails'
165
202
  # end
166
- def inflections
203
+ def inflections(locale = :en)
167
204
  if block_given?
168
- yield Inflections.instance
205
+ yield Inflections.instance(locale)
169
206
  else
170
- Inflections.instance
207
+ Inflections.instance(locale)
171
208
  end
172
209
  end
173
210
  end
@@ -1,57 +1,71 @@
1
+ # encoding: utf-8
2
+
1
3
  require 'active_support/inflector/inflections'
2
4
  require 'active_support/inflections'
3
5
 
4
6
  module ActiveSupport
5
- # The Inflector transforms words from singular to plural, class names to table names, modularized class names to ones without,
6
- # and class names to foreign keys. The default inflections for pluralization, singularization, and uncountable words are kept
7
- # in inflections.rb.
7
+ # The Inflector transforms words from singular to plural, class names to table
8
+ # names, modularized class names to ones without, and class names to foreign
9
+ # keys. The default inflections for pluralization, singularization, and
10
+ # uncountable words are kept in inflections.rb.
8
11
  #
9
- # The Rails core team has stated patches for the inflections library will not be accepted
10
- # in order to avoid breaking legacy applications which may be relying on errant inflections.
11
- # If you discover an incorrect inflection and require it for your application, you'll need
12
- # to correct it yourself (explained below).
12
+ # The Rails core team has stated patches for the inflections library will not
13
+ # be accepted in order to avoid breaking legacy applications which may be
14
+ # relying on errant inflections. If you discover an incorrect inflection and
15
+ # require it for your application or wish to define rules for languages other
16
+ # than English, please correct or add them yourself (explained below).
13
17
  module Inflector
14
18
  extend self
15
19
 
16
20
  # Returns the plural form of the word in the string.
17
21
  #
18
- # Examples:
19
- # "post".pluralize # => "posts"
20
- # "octopus".pluralize # => "octopi"
21
- # "sheep".pluralize # => "sheep"
22
- # "words".pluralize # => "words"
23
- # "CamelOctopus".pluralize # => "CamelOctopi"
24
- def pluralize(word)
25
- apply_inflections(word, inflections.plurals)
22
+ # If passed an optional +locale+ parameter, the word will be
23
+ # pluralized using rules defined for that language. By default,
24
+ # this parameter is set to <tt>:en</tt>.
25
+ #
26
+ # 'post'.pluralize # => "posts"
27
+ # 'octopus'.pluralize # => "octopi"
28
+ # 'sheep'.pluralize # => "sheep"
29
+ # 'words'.pluralize # => "words"
30
+ # 'CamelOctopus'.pluralize # => "CamelOctopi"
31
+ # 'ley'.pluralize(:es) # => "leyes"
32
+ def pluralize(word, locale = :en)
33
+ apply_inflections(word, inflections(locale).plurals)
26
34
  end
27
35
 
28
- # The reverse of +pluralize+, returns the singular form of a word in a string.
29
- #
30
- # Examples:
31
- # "posts".singularize # => "post"
32
- # "octopi".singularize # => "octopus"
33
- # "sheep".singularize # => "sheep"
34
- # "word".singularize # => "word"
35
- # "CamelOctopi".singularize # => "CamelOctopus"
36
- def singularize(word)
37
- apply_inflections(word, inflections.singulars)
36
+ # The reverse of +pluralize+, returns the singular form of a word in a
37
+ # string.
38
+ #
39
+ # If passed an optional +locale+ parameter, the word will be
40
+ # pluralized using rules defined for that language. By default,
41
+ # this parameter is set to <tt>:en</tt>.
42
+ #
43
+ # 'posts'.singularize # => "post"
44
+ # 'octopi'.singularize # => "octopus"
45
+ # 'sheep'.singularize # => "sheep"
46
+ # 'word'.singularize # => "word"
47
+ # 'CamelOctopi'.singularize # => "CamelOctopus"
48
+ # 'leyes'.singularize(:es) # => "ley"
49
+ def singularize(word, locale = :en)
50
+ apply_inflections(word, inflections(locale).singulars)
38
51
  end
39
52
 
40
- # By default, +camelize+ converts strings to UpperCamelCase. If the argument to +camelize+
41
- # is set to <tt>:lower</tt> then +camelize+ produces lowerCamelCase.
53
+ # By default, +camelize+ converts strings to UpperCamelCase. If the argument
54
+ # to +camelize+ is set to <tt>:lower</tt> then +camelize+ produces
55
+ # lowerCamelCase.
42
56
  #
43
- # +camelize+ will also convert '/' to '::' which is useful for converting paths to namespaces.
57
+ # +camelize+ will also convert '/' to '::' which is useful for converting
58
+ # paths to namespaces.
44
59
  #
45
- # Examples:
46
- # "active_model".camelize # => "ActiveModel"
47
- # "active_model".camelize(:lower) # => "activeModel"
48
- # "active_model/errors".camelize # => "ActiveModel::Errors"
49
- # "active_model/errors".camelize(:lower) # => "activeModel::Errors"
60
+ # 'active_model'.camelize # => "ActiveModel"
61
+ # 'active_model'.camelize(:lower) # => "activeModel"
62
+ # 'active_model/errors'.camelize # => "ActiveModel::Errors"
63
+ # 'active_model/errors'.camelize(:lower) # => "activeModel::Errors"
50
64
  #
51
- # As a rule of thumb you can think of +camelize+ as the inverse of +underscore+,
52
- # though there are cases where that does not hold:
65
+ # As a rule of thumb you can think of +camelize+ as the inverse of
66
+ # +underscore+, though there are cases where that does not hold:
53
67
  #
54
- # "SSLError".underscore.camelize # => "SslError"
68
+ # 'SSLError'.underscore.camelize # => "SslError"
55
69
  def camelize(term, uppercase_first_letter = true)
56
70
  string = term.to_s
57
71
  if uppercase_first_letter
@@ -66,17 +80,16 @@ module ActiveSupport
66
80
  #
67
81
  # Changes '::' to '/' to convert namespaces to paths.
68
82
  #
69
- # Examples:
70
- # "ActiveModel".underscore # => "active_model"
71
- # "ActiveModel::Errors".underscore # => "active_model/errors"
83
+ # 'ActiveModel'.underscore # => "active_model"
84
+ # 'ActiveModel::Errors'.underscore # => "active_model/errors"
72
85
  #
73
- # As a rule of thumb you can think of +underscore+ as the inverse of +camelize+,
74
- # though there are cases where that does not hold:
86
+ # As a rule of thumb you can think of +underscore+ as the inverse of
87
+ # +camelize+, though there are cases where that does not hold:
75
88
  #
76
- # "SSLError".underscore.camelize # => "SslError"
89
+ # 'SSLError'.underscore.camelize # => "SslError"
77
90
  def underscore(camel_cased_word)
78
91
  word = camel_cased_word.to_s.dup
79
- word.gsub!(/::/, '/')
92
+ word.gsub!('::', '/')
80
93
  word.gsub!(/(?:([A-Za-z\d])|^)(#{inflections.acronym_regex})(?=\b|[^a-z])/) { "#{$1}#{$1 && '_'}#{$2.downcase}" }
81
94
  word.gsub!(/([A-Z\d]+)([A-Z][a-z])/,'\1_\2')
82
95
  word.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
@@ -86,57 +99,55 @@ module ActiveSupport
86
99
  end
87
100
 
88
101
  # Capitalizes the first word and turns underscores into spaces and strips a
89
- # trailing "_id", if any. Like +titleize+, this is meant for creating pretty output.
102
+ # trailing "_id", if any. Like +titleize+, this is meant for creating pretty
103
+ # output.
90
104
  #
91
- # Examples:
92
- # "employee_salary" # => "Employee salary"
93
- # "author_id" # => "Author"
105
+ # 'employee_salary'.humanize # => "Employee salary"
106
+ # 'author_id'.humanize # => "Author"
94
107
  def humanize(lower_case_and_underscored_word)
95
108
  result = lower_case_and_underscored_word.to_s.dup
96
- inflections.humans.each { |(rule, replacement)| break if result.gsub!(rule, replacement) }
109
+ inflections.humans.each { |(rule, replacement)| break if result.sub!(rule, replacement) }
97
110
  result.gsub!(/_id$/, "")
98
- result.gsub!(/_/, ' ')
111
+ result.tr!('_', ' ')
99
112
  result.gsub(/([a-z\d]*)/i) { |match|
100
113
  "#{inflections.acronyms[match] || match.downcase}"
101
114
  }.gsub(/^\w/) { $&.upcase }
102
115
  end
103
116
 
104
- # Capitalizes all the words and replaces some characters in the string to create
105
- # a nicer looking title. +titleize+ is meant for creating pretty output. It is not
106
- # used in the Rails internals.
117
+ # Capitalizes all the words and replaces some characters in the string to
118
+ # create a nicer looking title. +titleize+ is meant for creating pretty
119
+ # output. It is not used in the Rails internals.
107
120
  #
108
- # +titleize+ is also aliased as as +titlecase+.
121
+ # +titleize+ is also aliased as +titlecase+.
109
122
  #
110
- # Examples:
111
- # "man from the boondocks".titleize # => "Man From The Boondocks"
112
- # "x-men: the last stand".titleize # => "X Men: The Last Stand"
113
- # "TheManWithoutAPast".titleize # => "The Man Without A Past"
114
- # "raiders_of_the_lost_ark".titleize # => "Raiders Of The Lost Ark"
123
+ # 'man from the boondocks'.titleize # => "Man From The Boondocks"
124
+ # 'x-men: the last stand'.titleize # => "X Men: The Last Stand"
125
+ # 'TheManWithoutAPast'.titleize # => "The Man Without A Past"
126
+ # 'raiders_of_the_lost_ark'.titleize # => "Raiders Of The Lost Ark"
115
127
  def titleize(word)
116
- humanize(underscore(word)).gsub(/\b('?[a-z])/) { $1.capitalize }
128
+ humanize(underscore(word)).gsub(/\b(?<!['’`])[a-z]/) { $&.capitalize }
117
129
  end
118
130
 
119
- # Create the name of a table like Rails does for models to table names. This method
120
- # uses the +pluralize+ method on the last word in the string.
131
+ # Create the name of a table like Rails does for models to table names. This
132
+ # method uses the +pluralize+ method on the last word in the string.
121
133
  #
122
- # Examples
123
- # "RawScaledScorer".tableize # => "raw_scaled_scorers"
124
- # "egg_and_ham".tableize # => "egg_and_hams"
125
- # "fancyCategory".tableize # => "fancy_categories"
134
+ # 'RawScaledScorer'.tableize # => "raw_scaled_scorers"
135
+ # 'egg_and_ham'.tableize # => "egg_and_hams"
136
+ # 'fancyCategory'.tableize # => "fancy_categories"
126
137
  def tableize(class_name)
127
138
  pluralize(underscore(class_name))
128
139
  end
129
140
 
130
- # Create a class name from a plural table name like Rails does for table names to models.
131
- # Note that this returns a string and not a Class. (To convert to an actual class
132
- # follow +classify+ with +constantize+.)
141
+ # Create a class name from a plural table name like Rails does for table
142
+ # names to models. Note that this returns a string and not a Class (To
143
+ # convert to an actual class follow +classify+ with +constantize+).
133
144
  #
134
- # Examples:
135
- # "egg_and_hams".classify # => "EggAndHam"
136
- # "posts".classify # => "Post"
145
+ # 'egg_and_hams'.classify # => "EggAndHam"
146
+ # 'posts'.classify # => "Post"
137
147
  #
138
148
  # Singular names are not handled correctly:
139
- # "business".classify # => "Busines"
149
+ #
150
+ # 'business'.classify # => "Busines"
140
151
  def classify(table_name)
141
152
  # strip out any leading schema name
142
153
  camelize(singularize(table_name.to_s.sub(/.*\./, '')))
@@ -144,16 +155,15 @@ module ActiveSupport
144
155
 
145
156
  # Replaces underscores with dashes in the string.
146
157
  #
147
- # Example:
148
- # "puni_puni" # => "puni-puni"
158
+ # 'puni_puni'.dasherize # => "puni-puni"
149
159
  def dasherize(underscored_word)
150
- underscored_word.gsub(/_/, '-')
160
+ underscored_word.tr('_', '-')
151
161
  end
152
162
 
153
- # Removes the module part from the expression in the string:
163
+ # Removes the module part from the expression in the string.
154
164
  #
155
- # "ActiveRecord::CoreExtensions::String::Inflections".demodulize # => "Inflections"
156
- # "Inflections".demodulize # => "Inflections"
165
+ # 'ActiveRecord::CoreExtensions::String::Inflections'.demodulize # => "Inflections"
166
+ # 'Inflections'.demodulize # => "Inflections"
157
167
  #
158
168
  # See also +deconstantize+.
159
169
  def demodulize(path)
@@ -165,13 +175,13 @@ module ActiveSupport
165
175
  end
166
176
  end
167
177
 
168
- # Removes the rightmost segment from the constant expression in the string:
178
+ # Removes the rightmost segment from the constant expression in the string.
169
179
  #
170
- # "Net::HTTP".deconstantize # => "Net"
171
- # "::Net::HTTP".deconstantize # => "::Net"
172
- # "String".deconstantize # => ""
173
- # "::String".deconstantize # => ""
174
- # "".deconstantize # => ""
180
+ # 'Net::HTTP'.deconstantize # => "Net"
181
+ # '::Net::HTTP'.deconstantize # => "::Net"
182
+ # 'String'.deconstantize # => ""
183
+ # '::String'.deconstantize # => ""
184
+ # ''.deconstantize # => ""
175
185
  #
176
186
  # See also +demodulize+.
177
187
  def deconstantize(path)
@@ -182,94 +192,115 @@ module ActiveSupport
182
192
  # +separate_class_name_and_id_with_underscore+ sets whether
183
193
  # the method should put '_' between the name and 'id'.
184
194
  #
185
- # Examples:
186
- # "Message".foreign_key # => "message_id"
187
- # "Message".foreign_key(false) # => "messageid"
188
- # "Admin::Post".foreign_key # => "post_id"
195
+ # 'Message'.foreign_key # => "message_id"
196
+ # 'Message'.foreign_key(false) # => "messageid"
197
+ # 'Admin::Post'.foreign_key # => "post_id"
189
198
  def foreign_key(class_name, separate_class_name_and_id_with_underscore = true)
190
199
  underscore(demodulize(class_name)) + (separate_class_name_and_id_with_underscore ? "_id" : "id")
191
200
  end
192
201
 
193
- # Ruby 1.9 introduces an inherit argument for Module#const_get and
194
- # #const_defined? and changes their default behavior.
195
- if Module.method(:const_get).arity == 1
196
- # Tries to find a constant with the name specified in the argument string:
197
- #
198
- # "Module".constantize # => Module
199
- # "Test::Unit".constantize # => Test::Unit
200
- #
201
- # The name is assumed to be the one of a top-level constant, no matter whether
202
- # it starts with "::" or not. No lexical context is taken into account:
203
- #
204
- # C = 'outside'
205
- # module M
206
- # C = 'inside'
207
- # C # => 'inside'
208
- # "C".constantize # => 'outside', same as ::C
209
- # end
210
- #
211
- # NameError is raised when the name is not in CamelCase or the constant is
212
- # unknown.
213
- def constantize(camel_cased_word)
214
- names = camel_cased_word.split('::')
215
- names.shift if names.empty? || names.first.empty?
202
+ # Tries to find a constant with the name specified in the argument string.
203
+ #
204
+ # 'Module'.constantize # => Module
205
+ # 'Test::Unit'.constantize # => Test::Unit
206
+ #
207
+ # The name is assumed to be the one of a top-level constant, no matter
208
+ # whether it starts with "::" or not. No lexical context is taken into
209
+ # account:
210
+ #
211
+ # C = 'outside'
212
+ # module M
213
+ # C = 'inside'
214
+ # C # => 'inside'
215
+ # 'C'.constantize # => 'outside', same as ::C
216
+ # end
217
+ #
218
+ # NameError is raised when the name is not in CamelCase or the constant is
219
+ # unknown.
220
+ def constantize(camel_cased_word)
221
+ names = camel_cased_word.split('::')
222
+ names.shift if names.empty? || names.first.empty?
223
+
224
+ names.inject(Object) do |constant, name|
225
+ if constant == Object
226
+ constant.const_get(name)
227
+ else
228
+ candidate = constant.const_get(name)
229
+ next candidate if constant.const_defined?(name, false)
230
+ next candidate unless Object.const_defined?(name)
216
231
 
217
- constant = Object
218
- names.each do |name|
219
- constant = constant.const_defined?(name) ? constant.const_get(name) : constant.const_missing(name)
220
- end
221
- constant
222
- end
223
- else
224
- def constantize(camel_cased_word) #:nodoc:
225
- names = camel_cased_word.split('::')
226
- names.shift if names.empty? || names.first.empty?
232
+ # Go down the ancestors to check it it's owned
233
+ # directly before we reach Object or the end of ancestors.
234
+ constant = constant.ancestors.inject do |const, ancestor|
235
+ break const if ancestor == Object
236
+ break ancestor if ancestor.const_defined?(name, false)
237
+ const
238
+ end
227
239
 
228
- constant = Object
229
- names.each do |name|
230
- constant = constant.const_defined?(name, false) ? constant.const_get(name) : constant.const_missing(name)
240
+ # owner is in Object, so raise
241
+ constant.const_get(name, false)
231
242
  end
232
- constant
233
243
  end
234
244
  end
235
245
 
236
- # Tries to find a constant with the name specified in the argument string:
246
+ # Tries to find a constant with the name specified in the argument string.
237
247
  #
238
- # "Module".safe_constantize # => Module
239
- # "Test::Unit".safe_constantize # => Test::Unit
248
+ # 'Module'.safe_constantize # => Module
249
+ # 'Test::Unit'.safe_constantize # => Test::Unit
240
250
  #
241
- # The name is assumed to be the one of a top-level constant, no matter whether
242
- # it starts with "::" or not. No lexical context is taken into account:
251
+ # The name is assumed to be the one of a top-level constant, no matter
252
+ # whether it starts with "::" or not. No lexical context is taken into
253
+ # account:
243
254
  #
244
255
  # C = 'outside'
245
256
  # module M
246
257
  # C = 'inside'
247
258
  # C # => 'inside'
248
- # "C".safe_constantize # => 'outside', same as ::C
259
+ # 'C'.safe_constantize # => 'outside', same as ::C
249
260
  # end
250
261
  #
251
- # nil is returned when the name is not in CamelCase or the constant (or part of it) is
252
- # unknown.
253
- #
254
- # "blargle".safe_constantize # => nil
255
- # "UnknownModule".safe_constantize # => nil
256
- # "UnknownModule::Foo::Bar".safe_constantize # => nil
262
+ # +nil+ is returned when the name is not in CamelCase or the constant (or
263
+ # part of it) is unknown.
257
264
  #
265
+ # 'blargle'.safe_constantize # => nil
266
+ # 'UnknownModule'.safe_constantize # => nil
267
+ # 'UnknownModule::Foo::Bar'.safe_constantize # => nil
258
268
  def safe_constantize(camel_cased_word)
259
- begin
260
- constantize(camel_cased_word)
261
- rescue NameError => e
262
- raise unless e.message =~ /(uninitialized constant|wrong constant name) #{const_regexp(camel_cased_word)}$/ ||
263
- e.name.to_s == camel_cased_word.to_s
264
- rescue ArgumentError => e
265
- raise unless e.message =~ /not missing constant #{const_regexp(camel_cased_word)}\!$/
269
+ constantize(camel_cased_word)
270
+ rescue NameError => e
271
+ raise unless e.message =~ /(uninitialized constant|wrong constant name) #{const_regexp(camel_cased_word)}$/ ||
272
+ e.name.to_s == camel_cased_word.to_s
273
+ rescue ArgumentError => e
274
+ raise unless e.message =~ /not missing constant #{const_regexp(camel_cased_word)}\!$/
275
+ end
276
+
277
+ # Returns the suffix that should be added to a number to denote the position
278
+ # in an ordered sequence such as 1st, 2nd, 3rd, 4th.
279
+ #
280
+ # ordinal(1) # => "st"
281
+ # ordinal(2) # => "nd"
282
+ # ordinal(1002) # => "nd"
283
+ # ordinal(1003) # => "rd"
284
+ # ordinal(-11) # => "th"
285
+ # ordinal(-1021) # => "st"
286
+ def ordinal(number)
287
+ abs_number = number.to_i.abs
288
+
289
+ if (11..13).include?(abs_number % 100)
290
+ "th"
291
+ else
292
+ case abs_number % 10
293
+ when 1; "st"
294
+ when 2; "nd"
295
+ when 3; "rd"
296
+ else "th"
297
+ end
266
298
  end
267
299
  end
268
300
 
269
301
  # Turns a number into an ordinal string used to denote the position in an
270
302
  # ordered sequence such as 1st, 2nd, 3rd, 4th.
271
303
  #
272
- # Examples:
273
304
  # ordinalize(1) # => "1st"
274
305
  # ordinalize(2) # => "2nd"
275
306
  # ordinalize(1002) # => "1002nd"
@@ -277,16 +308,7 @@ module ActiveSupport
277
308
  # ordinalize(-11) # => "-11th"
278
309
  # ordinalize(-1021) # => "-1021st"
279
310
  def ordinalize(number)
280
- if (11..13).include?(number.to_i.abs % 100)
281
- "#{number}th"
282
- else
283
- case number.to_i.abs % 10
284
- when 1; "#{number}st"
285
- when 2; "#{number}nd"
286
- when 3; "#{number}rd"
287
- else "#{number}th"
288
- end
289
- end
311
+ "#{number}#{ordinal(number)}"
290
312
  end
291
313
 
292
314
  private
@@ -304,16 +326,15 @@ module ActiveSupport
304
326
 
305
327
  # Applies inflection rules for +singularize+ and +pluralize+.
306
328
  #
307
- # Examples:
308
- # apply_inflections("post", inflections.plurals) # => "posts"
309
- # apply_inflections("posts", inflections.singulars) # => "post"
329
+ # apply_inflections('post', inflections.plurals) # => "posts"
330
+ # apply_inflections('posts', inflections.singulars) # => "post"
310
331
  def apply_inflections(word, rules)
311
332
  result = word.to_s.dup
312
333
 
313
- if word.empty? || inflections.uncountables.any? { |inflection| result =~ /\b#{inflection}\Z/i }
334
+ if word.empty? || inflections.uncountables.include?(result.downcase[/\b\w+\Z/])
314
335
  result
315
336
  else
316
- rules.each { |(rule, replacement)| break if result.gsub!(rule, replacement) }
337
+ rules.each { |(rule, replacement)| break if result.sub!(rule, replacement) }
317
338
  result
318
339
  end
319
340
  end