activesupport 5.0.7.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (236) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +1018 -0
  3. data/MIT-LICENSE +20 -0
  4. data/README.rdoc +39 -0
  5. data/lib/active_support.rb +99 -0
  6. data/lib/active_support/all.rb +3 -0
  7. data/lib/active_support/array_inquirer.rb +44 -0
  8. data/lib/active_support/backtrace_cleaner.rb +103 -0
  9. data/lib/active_support/benchmarkable.rb +49 -0
  10. data/lib/active_support/builder.rb +6 -0
  11. data/lib/active_support/cache.rb +701 -0
  12. data/lib/active_support/cache/file_store.rb +204 -0
  13. data/lib/active_support/cache/mem_cache_store.rb +207 -0
  14. data/lib/active_support/cache/memory_store.rb +167 -0
  15. data/lib/active_support/cache/null_store.rb +41 -0
  16. data/lib/active_support/cache/strategy/local_cache.rb +172 -0
  17. data/lib/active_support/cache/strategy/local_cache_middleware.rb +44 -0
  18. data/lib/active_support/callbacks.rb +791 -0
  19. data/lib/active_support/concern.rb +142 -0
  20. data/lib/active_support/concurrency/latch.rb +26 -0
  21. data/lib/active_support/concurrency/share_lock.rb +226 -0
  22. data/lib/active_support/configurable.rb +148 -0
  23. data/lib/active_support/core_ext.rb +4 -0
  24. data/lib/active_support/core_ext/array.rb +7 -0
  25. data/lib/active_support/core_ext/array/access.rb +90 -0
  26. data/lib/active_support/core_ext/array/conversions.rb +211 -0
  27. data/lib/active_support/core_ext/array/extract_options.rb +29 -0
  28. data/lib/active_support/core_ext/array/grouping.rb +107 -0
  29. data/lib/active_support/core_ext/array/inquiry.rb +17 -0
  30. data/lib/active_support/core_ext/array/prepend_and_append.rb +7 -0
  31. data/lib/active_support/core_ext/array/wrap.rb +46 -0
  32. data/lib/active_support/core_ext/benchmark.rb +14 -0
  33. data/lib/active_support/core_ext/big_decimal.rb +1 -0
  34. data/lib/active_support/core_ext/big_decimal/conversions.rb +14 -0
  35. data/lib/active_support/core_ext/class.rb +2 -0
  36. data/lib/active_support/core_ext/class/attribute.rb +128 -0
  37. data/lib/active_support/core_ext/class/attribute_accessors.rb +4 -0
  38. data/lib/active_support/core_ext/class/subclasses.rb +41 -0
  39. data/lib/active_support/core_ext/date.rb +5 -0
  40. data/lib/active_support/core_ext/date/acts_like.rb +8 -0
  41. data/lib/active_support/core_ext/date/blank.rb +12 -0
  42. data/lib/active_support/core_ext/date/calculations.rb +143 -0
  43. data/lib/active_support/core_ext/date/conversions.rb +95 -0
  44. data/lib/active_support/core_ext/date/zones.rb +6 -0
  45. data/lib/active_support/core_ext/date_and_time/calculations.rb +335 -0
  46. data/lib/active_support/core_ext/date_and_time/compatibility.rb +14 -0
  47. data/lib/active_support/core_ext/date_and_time/zones.rb +40 -0
  48. data/lib/active_support/core_ext/date_time.rb +5 -0
  49. data/lib/active_support/core_ext/date_time/acts_like.rb +14 -0
  50. data/lib/active_support/core_ext/date_time/blank.rb +12 -0
  51. data/lib/active_support/core_ext/date_time/calculations.rb +199 -0
  52. data/lib/active_support/core_ext/date_time/compatibility.rb +16 -0
  53. data/lib/active_support/core_ext/date_time/conversions.rb +105 -0
  54. data/lib/active_support/core_ext/digest/uuid.rb +51 -0
  55. data/lib/active_support/core_ext/enumerable.rb +146 -0
  56. data/lib/active_support/core_ext/file.rb +1 -0
  57. data/lib/active_support/core_ext/file/atomic.rb +68 -0
  58. data/lib/active_support/core_ext/hash.rb +9 -0
  59. data/lib/active_support/core_ext/hash/compact.rb +24 -0
  60. data/lib/active_support/core_ext/hash/conversions.rb +262 -0
  61. data/lib/active_support/core_ext/hash/deep_merge.rb +38 -0
  62. data/lib/active_support/core_ext/hash/except.rb +22 -0
  63. data/lib/active_support/core_ext/hash/indifferent_access.rb +23 -0
  64. data/lib/active_support/core_ext/hash/keys.rb +170 -0
  65. data/lib/active_support/core_ext/hash/reverse_merge.rb +22 -0
  66. data/lib/active_support/core_ext/hash/slice.rb +48 -0
  67. data/lib/active_support/core_ext/hash/transform_values.rb +29 -0
  68. data/lib/active_support/core_ext/integer.rb +3 -0
  69. data/lib/active_support/core_ext/integer/inflections.rb +29 -0
  70. data/lib/active_support/core_ext/integer/multiple.rb +10 -0
  71. data/lib/active_support/core_ext/integer/time.rb +29 -0
  72. data/lib/active_support/core_ext/kernel.rb +4 -0
  73. data/lib/active_support/core_ext/kernel/agnostics.rb +11 -0
  74. data/lib/active_support/core_ext/kernel/concern.rb +12 -0
  75. data/lib/active_support/core_ext/kernel/debugger.rb +3 -0
  76. data/lib/active_support/core_ext/kernel/reporting.rb +43 -0
  77. data/lib/active_support/core_ext/kernel/singleton_class.rb +6 -0
  78. data/lib/active_support/core_ext/load_error.rb +31 -0
  79. data/lib/active_support/core_ext/marshal.rb +22 -0
  80. data/lib/active_support/core_ext/module.rb +12 -0
  81. data/lib/active_support/core_ext/module/aliasing.rb +74 -0
  82. data/lib/active_support/core_ext/module/anonymous.rb +28 -0
  83. data/lib/active_support/core_ext/module/attr_internal.rb +36 -0
  84. data/lib/active_support/core_ext/module/attribute_accessors.rb +212 -0
  85. data/lib/active_support/core_ext/module/attribute_accessors_per_thread.rb +141 -0
  86. data/lib/active_support/core_ext/module/concerning.rb +135 -0
  87. data/lib/active_support/core_ext/module/delegation.rb +216 -0
  88. data/lib/active_support/core_ext/module/deprecation.rb +23 -0
  89. data/lib/active_support/core_ext/module/introspection.rb +68 -0
  90. data/lib/active_support/core_ext/module/method_transplanting.rb +3 -0
  91. data/lib/active_support/core_ext/module/qualified_const.rb +70 -0
  92. data/lib/active_support/core_ext/module/reachable.rb +8 -0
  93. data/lib/active_support/core_ext/module/remove_method.rb +35 -0
  94. data/lib/active_support/core_ext/name_error.rb +31 -0
  95. data/lib/active_support/core_ext/numeric.rb +4 -0
  96. data/lib/active_support/core_ext/numeric/bytes.rb +64 -0
  97. data/lib/active_support/core_ext/numeric/conversions.rb +144 -0
  98. data/lib/active_support/core_ext/numeric/inquiry.rb +26 -0
  99. data/lib/active_support/core_ext/numeric/time.rb +74 -0
  100. data/lib/active_support/core_ext/object.rb +14 -0
  101. data/lib/active_support/core_ext/object/acts_like.rb +10 -0
  102. data/lib/active_support/core_ext/object/blank.rb +143 -0
  103. data/lib/active_support/core_ext/object/conversions.rb +4 -0
  104. data/lib/active_support/core_ext/object/deep_dup.rb +53 -0
  105. data/lib/active_support/core_ext/object/duplicable.rb +124 -0
  106. data/lib/active_support/core_ext/object/inclusion.rb +27 -0
  107. data/lib/active_support/core_ext/object/instance_variables.rb +28 -0
  108. data/lib/active_support/core_ext/object/json.rb +205 -0
  109. data/lib/active_support/core_ext/object/to_param.rb +1 -0
  110. data/lib/active_support/core_ext/object/to_query.rb +84 -0
  111. data/lib/active_support/core_ext/object/try.rb +146 -0
  112. data/lib/active_support/core_ext/object/with_options.rb +69 -0
  113. data/lib/active_support/core_ext/range.rb +4 -0
  114. data/lib/active_support/core_ext/range/conversions.rb +31 -0
  115. data/lib/active_support/core_ext/range/each.rb +21 -0
  116. data/lib/active_support/core_ext/range/include_range.rb +23 -0
  117. data/lib/active_support/core_ext/range/overlaps.rb +8 -0
  118. data/lib/active_support/core_ext/regexp.rb +5 -0
  119. data/lib/active_support/core_ext/securerandom.rb +23 -0
  120. data/lib/active_support/core_ext/string.rb +13 -0
  121. data/lib/active_support/core_ext/string/access.rb +104 -0
  122. data/lib/active_support/core_ext/string/behavior.rb +6 -0
  123. data/lib/active_support/core_ext/string/conversions.rb +57 -0
  124. data/lib/active_support/core_ext/string/exclude.rb +11 -0
  125. data/lib/active_support/core_ext/string/filters.rb +102 -0
  126. data/lib/active_support/core_ext/string/indent.rb +43 -0
  127. data/lib/active_support/core_ext/string/inflections.rb +244 -0
  128. data/lib/active_support/core_ext/string/inquiry.rb +13 -0
  129. data/lib/active_support/core_ext/string/multibyte.rb +53 -0
  130. data/lib/active_support/core_ext/string/output_safety.rb +260 -0
  131. data/lib/active_support/core_ext/string/starts_ends_with.rb +4 -0
  132. data/lib/active_support/core_ext/string/strip.rb +23 -0
  133. data/lib/active_support/core_ext/string/zones.rb +14 -0
  134. data/lib/active_support/core_ext/struct.rb +3 -0
  135. data/lib/active_support/core_ext/time.rb +5 -0
  136. data/lib/active_support/core_ext/time/acts_like.rb +8 -0
  137. data/lib/active_support/core_ext/time/calculations.rb +290 -0
  138. data/lib/active_support/core_ext/time/compatibility.rb +14 -0
  139. data/lib/active_support/core_ext/time/conversions.rb +67 -0
  140. data/lib/active_support/core_ext/time/marshal.rb +3 -0
  141. data/lib/active_support/core_ext/time/zones.rb +111 -0
  142. data/lib/active_support/core_ext/uri.rb +24 -0
  143. data/lib/active_support/dependencies.rb +755 -0
  144. data/lib/active_support/dependencies/autoload.rb +77 -0
  145. data/lib/active_support/dependencies/interlock.rb +55 -0
  146. data/lib/active_support/deprecation.rb +43 -0
  147. data/lib/active_support/deprecation/behaviors.rb +90 -0
  148. data/lib/active_support/deprecation/instance_delegator.rb +37 -0
  149. data/lib/active_support/deprecation/method_wrappers.rb +70 -0
  150. data/lib/active_support/deprecation/proxy_wrappers.rb +149 -0
  151. data/lib/active_support/deprecation/reporting.rb +112 -0
  152. data/lib/active_support/descendants_tracker.rb +60 -0
  153. data/lib/active_support/duration.rb +235 -0
  154. data/lib/active_support/duration/iso8601_parser.rb +122 -0
  155. data/lib/active_support/duration/iso8601_serializer.rb +51 -0
  156. data/lib/active_support/evented_file_update_checker.rb +199 -0
  157. data/lib/active_support/execution_wrapper.rb +126 -0
  158. data/lib/active_support/executor.rb +6 -0
  159. data/lib/active_support/file_update_checker.rb +157 -0
  160. data/lib/active_support/gem_version.rb +15 -0
  161. data/lib/active_support/gzip.rb +36 -0
  162. data/lib/active_support/hash_with_indifferent_access.rb +329 -0
  163. data/lib/active_support/i18n.rb +13 -0
  164. data/lib/active_support/i18n_railtie.rb +115 -0
  165. data/lib/active_support/inflections.rb +70 -0
  166. data/lib/active_support/inflector.rb +7 -0
  167. data/lib/active_support/inflector/inflections.rb +242 -0
  168. data/lib/active_support/inflector/methods.rb +390 -0
  169. data/lib/active_support/inflector/transliterate.rb +112 -0
  170. data/lib/active_support/json.rb +2 -0
  171. data/lib/active_support/json/decoding.rb +74 -0
  172. data/lib/active_support/json/encoding.rb +127 -0
  173. data/lib/active_support/key_generator.rb +71 -0
  174. data/lib/active_support/lazy_load_hooks.rb +76 -0
  175. data/lib/active_support/locale/en.yml +135 -0
  176. data/lib/active_support/log_subscriber.rb +109 -0
  177. data/lib/active_support/log_subscriber/test_helper.rb +104 -0
  178. data/lib/active_support/logger.rb +106 -0
  179. data/lib/active_support/logger_silence.rb +28 -0
  180. data/lib/active_support/logger_thread_safe_level.rb +31 -0
  181. data/lib/active_support/message_encryptor.rb +114 -0
  182. data/lib/active_support/message_verifier.rb +134 -0
  183. data/lib/active_support/multibyte.rb +21 -0
  184. data/lib/active_support/multibyte/chars.rb +231 -0
  185. data/lib/active_support/multibyte/unicode.rb +413 -0
  186. data/lib/active_support/notifications.rb +212 -0
  187. data/lib/active_support/notifications/fanout.rb +157 -0
  188. data/lib/active_support/notifications/instrumenter.rb +91 -0
  189. data/lib/active_support/number_helper.rb +368 -0
  190. data/lib/active_support/number_helper/number_converter.rb +182 -0
  191. data/lib/active_support/number_helper/number_to_currency_converter.rb +44 -0
  192. data/lib/active_support/number_helper/number_to_delimited_converter.rb +28 -0
  193. data/lib/active_support/number_helper/number_to_human_converter.rb +68 -0
  194. data/lib/active_support/number_helper/number_to_human_size_converter.rb +62 -0
  195. data/lib/active_support/number_helper/number_to_percentage_converter.rb +12 -0
  196. data/lib/active_support/number_helper/number_to_phone_converter.rb +58 -0
  197. data/lib/active_support/number_helper/number_to_rounded_converter.rb +92 -0
  198. data/lib/active_support/option_merger.rb +25 -0
  199. data/lib/active_support/ordered_hash.rb +48 -0
  200. data/lib/active_support/ordered_options.rb +81 -0
  201. data/lib/active_support/per_thread_registry.rb +58 -0
  202. data/lib/active_support/proxy_object.rb +13 -0
  203. data/lib/active_support/rails.rb +27 -0
  204. data/lib/active_support/railtie.rb +51 -0
  205. data/lib/active_support/reloader.rb +129 -0
  206. data/lib/active_support/rescuable.rb +173 -0
  207. data/lib/active_support/security_utils.rb +27 -0
  208. data/lib/active_support/string_inquirer.rb +26 -0
  209. data/lib/active_support/subscriber.rb +120 -0
  210. data/lib/active_support/tagged_logging.rb +77 -0
  211. data/lib/active_support/test_case.rb +88 -0
  212. data/lib/active_support/testing/assertions.rb +99 -0
  213. data/lib/active_support/testing/autorun.rb +5 -0
  214. data/lib/active_support/testing/constant_lookup.rb +50 -0
  215. data/lib/active_support/testing/declarative.rb +26 -0
  216. data/lib/active_support/testing/deprecation.rb +36 -0
  217. data/lib/active_support/testing/file_fixtures.rb +34 -0
  218. data/lib/active_support/testing/isolation.rb +115 -0
  219. data/lib/active_support/testing/method_call_assertions.rb +41 -0
  220. data/lib/active_support/testing/setup_and_teardown.rb +50 -0
  221. data/lib/active_support/testing/stream.rb +42 -0
  222. data/lib/active_support/testing/tagged_logging.rb +25 -0
  223. data/lib/active_support/testing/time_helpers.rb +136 -0
  224. data/lib/active_support/time.rb +18 -0
  225. data/lib/active_support/time_with_zone.rb +511 -0
  226. data/lib/active_support/values/time_zone.rb +484 -0
  227. data/lib/active_support/values/unicode_tables.dat +0 -0
  228. data/lib/active_support/version.rb +8 -0
  229. data/lib/active_support/xml_mini.rb +209 -0
  230. data/lib/active_support/xml_mini/jdom.rb +181 -0
  231. data/lib/active_support/xml_mini/libxml.rb +77 -0
  232. data/lib/active_support/xml_mini/libxmlsax.rb +82 -0
  233. data/lib/active_support/xml_mini/nokogiri.rb +81 -0
  234. data/lib/active_support/xml_mini/nokogirisax.rb +85 -0
  235. data/lib/active_support/xml_mini/rexml.rb +128 -0
  236. metadata +349 -0
@@ -0,0 +1,27 @@
1
+ class Object
2
+ # Returns true if this object is included in the argument. Argument must be
3
+ # any object which responds to +#include?+. Usage:
4
+ #
5
+ # characters = ["Konata", "Kagami", "Tsukasa"]
6
+ # "Konata".in?(characters) # => true
7
+ #
8
+ # This will throw an +ArgumentError+ if the argument doesn't respond
9
+ # to +#include?+.
10
+ def in?(another_object)
11
+ another_object.include?(self)
12
+ rescue NoMethodError
13
+ raise ArgumentError.new("The parameter passed to #in? must respond to #include?")
14
+ end
15
+
16
+ # Returns the receiver if it's included in the argument otherwise returns +nil+.
17
+ # Argument must be any object which responds to +#include?+. Usage:
18
+ #
19
+ # params[:bucket_type].presence_in %w( project calendar )
20
+ #
21
+ # This will throw an +ArgumentError+ if the argument doesn't respond to +#include?+.
22
+ #
23
+ # @return [Object]
24
+ def presence_in(another_object)
25
+ self.in?(another_object) ? self : nil
26
+ end
27
+ end
@@ -0,0 +1,28 @@
1
+ class Object
2
+ # Returns a hash with string keys that maps instance variable names without "@" to their
3
+ # corresponding values.
4
+ #
5
+ # class C
6
+ # def initialize(x, y)
7
+ # @x, @y = x, y
8
+ # end
9
+ # end
10
+ #
11
+ # C.new(0, 1).instance_values # => {"x" => 0, "y" => 1}
12
+ def instance_values
13
+ Hash[instance_variables.map { |name| [name[1..-1], instance_variable_get(name)] }]
14
+ end
15
+
16
+ # Returns an array of instance variable names as strings including "@".
17
+ #
18
+ # class C
19
+ # def initialize(x, y)
20
+ # @x, @y = x, y
21
+ # end
22
+ # end
23
+ #
24
+ # C.new(0, 1).instance_variable_names # => ["@y", "@x"]
25
+ def instance_variable_names
26
+ instance_variables.map(&:to_s)
27
+ end
28
+ end
@@ -0,0 +1,205 @@
1
+ # Hack to load json gem first so we can overwrite its to_json.
2
+ require 'json'
3
+ require 'bigdecimal'
4
+ require 'active_support/core_ext/big_decimal/conversions' # for #to_s
5
+ require 'active_support/core_ext/hash/except'
6
+ require 'active_support/core_ext/hash/slice'
7
+ require 'active_support/core_ext/object/instance_variables'
8
+ require 'time'
9
+ require 'active_support/core_ext/time/conversions'
10
+ require 'active_support/core_ext/date_time/conversions'
11
+ require 'active_support/core_ext/date/conversions'
12
+
13
+ # The JSON gem adds a few modules to Ruby core classes containing :to_json definition, overwriting
14
+ # their default behavior. That said, we need to define the basic to_json method in all of them,
15
+ # otherwise they will always use to_json gem implementation, which is backwards incompatible in
16
+ # several cases (for instance, the JSON implementation for Hash does not work) with inheritance
17
+ # and consequently classes as ActiveSupport::OrderedHash cannot be serialized to json.
18
+ #
19
+ # On the other hand, we should avoid conflict with ::JSON.{generate,dump}(obj). Unfortunately, the
20
+ # JSON gem's encoder relies on its own to_json implementation to encode objects. Since it always
21
+ # passes a ::JSON::State object as the only argument to to_json, we can detect that and forward the
22
+ # calls to the original to_json method.
23
+ #
24
+ # It should be noted that when using ::JSON.{generate,dump} directly, ActiveSupport's encoder is
25
+ # bypassed completely. This means that as_json won't be invoked and the JSON gem will simply
26
+ # ignore any options it does not natively understand. This also means that ::JSON.{generate,dump}
27
+ # should give exactly the same results with or without active support.
28
+
29
+ module ActiveSupport
30
+ module ToJsonWithActiveSupportEncoder # :nodoc:
31
+ def to_json(options = nil)
32
+ if options.is_a?(::JSON::State)
33
+ # Called from JSON.{generate,dump}, forward it to JSON gem's to_json
34
+ super(options)
35
+ else
36
+ # to_json is being invoked directly, use ActiveSupport's encoder
37
+ ActiveSupport::JSON.encode(self, options)
38
+ end
39
+ end
40
+ end
41
+ end
42
+
43
+ [Object, Array, FalseClass, Float, Hash, Integer, NilClass, String, TrueClass, Enumerable].reverse_each do |klass|
44
+ klass.prepend(ActiveSupport::ToJsonWithActiveSupportEncoder)
45
+ end
46
+
47
+ class Object
48
+ def as_json(options = nil) #:nodoc:
49
+ if respond_to?(:to_hash)
50
+ to_hash.as_json(options)
51
+ else
52
+ instance_values.as_json(options)
53
+ end
54
+ end
55
+ end
56
+
57
+ class Struct #:nodoc:
58
+ def as_json(options = nil)
59
+ Hash[members.zip(values)].as_json(options)
60
+ end
61
+ end
62
+
63
+ class TrueClass
64
+ def as_json(options = nil) #:nodoc:
65
+ self
66
+ end
67
+ end
68
+
69
+ class FalseClass
70
+ def as_json(options = nil) #:nodoc:
71
+ self
72
+ end
73
+ end
74
+
75
+ class NilClass
76
+ def as_json(options = nil) #:nodoc:
77
+ self
78
+ end
79
+ end
80
+
81
+ class String
82
+ def as_json(options = nil) #:nodoc:
83
+ self
84
+ end
85
+ end
86
+
87
+ class Symbol
88
+ def as_json(options = nil) #:nodoc:
89
+ to_s
90
+ end
91
+ end
92
+
93
+ class Numeric
94
+ def as_json(options = nil) #:nodoc:
95
+ self
96
+ end
97
+ end
98
+
99
+ class Float
100
+ # Encoding Infinity or NaN to JSON should return "null". The default returns
101
+ # "Infinity" or "NaN" which are not valid JSON.
102
+ def as_json(options = nil) #:nodoc:
103
+ finite? ? self : nil
104
+ end
105
+ end
106
+
107
+ class BigDecimal
108
+ # A BigDecimal would be naturally represented as a JSON number. Most libraries,
109
+ # however, parse non-integer JSON numbers directly as floats. Clients using
110
+ # those libraries would get in general a wrong number and no way to recover
111
+ # other than manually inspecting the string with the JSON code itself.
112
+ #
113
+ # That's why a JSON string is returned. The JSON literal is not numeric, but
114
+ # if the other end knows by contract that the data is supposed to be a
115
+ # BigDecimal, it still has the chance to post-process the string and get the
116
+ # real value.
117
+ def as_json(options = nil) #:nodoc:
118
+ finite? ? to_s : nil
119
+ end
120
+ end
121
+
122
+ class Regexp
123
+ def as_json(options = nil) #:nodoc:
124
+ to_s
125
+ end
126
+ end
127
+
128
+ module Enumerable
129
+ def as_json(options = nil) #:nodoc:
130
+ to_a.as_json(options)
131
+ end
132
+ end
133
+
134
+ class Range
135
+ def as_json(options = nil) #:nodoc:
136
+ to_s
137
+ end
138
+ end
139
+
140
+ class Array
141
+ def as_json(options = nil) #:nodoc:
142
+ map { |v| options ? v.as_json(options.dup) : v.as_json }
143
+ end
144
+ end
145
+
146
+ class Hash
147
+ def as_json(options = nil) #:nodoc:
148
+ # create a subset of the hash by applying :only or :except
149
+ subset = if options
150
+ if attrs = options[:only]
151
+ slice(*Array(attrs))
152
+ elsif attrs = options[:except]
153
+ except(*Array(attrs))
154
+ else
155
+ self
156
+ end
157
+ else
158
+ self
159
+ end
160
+
161
+ Hash[subset.map { |k, v| [k.to_s, options ? v.as_json(options.dup) : v.as_json] }]
162
+ end
163
+ end
164
+
165
+ class Time
166
+ def as_json(options = nil) #:nodoc:
167
+ if ActiveSupport::JSON::Encoding.use_standard_json_time_format
168
+ xmlschema(ActiveSupport::JSON::Encoding.time_precision)
169
+ else
170
+ %(#{strftime("%Y/%m/%d %H:%M:%S")} #{formatted_offset(false)})
171
+ end
172
+ end
173
+ end
174
+
175
+ class Date
176
+ def as_json(options = nil) #:nodoc:
177
+ if ActiveSupport::JSON::Encoding.use_standard_json_time_format
178
+ strftime("%Y-%m-%d")
179
+ else
180
+ strftime("%Y/%m/%d")
181
+ end
182
+ end
183
+ end
184
+
185
+ class DateTime
186
+ def as_json(options = nil) #:nodoc:
187
+ if ActiveSupport::JSON::Encoding.use_standard_json_time_format
188
+ xmlschema(ActiveSupport::JSON::Encoding.time_precision)
189
+ else
190
+ strftime('%Y/%m/%d %H:%M:%S %z')
191
+ end
192
+ end
193
+ end
194
+
195
+ class Process::Status #:nodoc:
196
+ def as_json(options = nil)
197
+ { :exitstatus => exitstatus, :pid => pid }
198
+ end
199
+ end
200
+
201
+ class Exception
202
+ def as_json(options = nil)
203
+ to_s
204
+ end
205
+ end
@@ -0,0 +1 @@
1
+ require 'active_support/core_ext/object/to_query'
@@ -0,0 +1,84 @@
1
+ require 'cgi'
2
+
3
+ class Object
4
+ # Alias of <tt>to_s</tt>.
5
+ def to_param
6
+ to_s
7
+ end
8
+
9
+ # Converts an object into a string suitable for use as a URL query string,
10
+ # using the given <tt>key</tt> as the param name.
11
+ def to_query(key)
12
+ "#{CGI.escape(key.to_param)}=#{CGI.escape(to_param.to_s)}"
13
+ end
14
+ end
15
+
16
+ class NilClass
17
+ # Returns +self+.
18
+ def to_param
19
+ self
20
+ end
21
+ end
22
+
23
+ class TrueClass
24
+ # Returns +self+.
25
+ def to_param
26
+ self
27
+ end
28
+ end
29
+
30
+ class FalseClass
31
+ # Returns +self+.
32
+ def to_param
33
+ self
34
+ end
35
+ end
36
+
37
+ class Array
38
+ # Calls <tt>to_param</tt> on all its elements and joins the result with
39
+ # slashes. This is used by <tt>url_for</tt> in Action Pack.
40
+ def to_param
41
+ collect(&:to_param).join '/'
42
+ end
43
+
44
+ # Converts an array into a string suitable for use as a URL query string,
45
+ # using the given +key+ as the param name.
46
+ #
47
+ # ['Rails', 'coding'].to_query('hobbies') # => "hobbies%5B%5D=Rails&hobbies%5B%5D=coding"
48
+ def to_query(key)
49
+ prefix = "#{key}[]"
50
+
51
+ if empty?
52
+ nil.to_query(prefix)
53
+ else
54
+ collect { |value| value.to_query(prefix) }.join '&'
55
+ end
56
+ end
57
+ end
58
+
59
+ class Hash
60
+ # Returns a string representation of the receiver suitable for use as a URL
61
+ # query string:
62
+ #
63
+ # {name: 'David', nationality: 'Danish'}.to_query
64
+ # # => "name=David&nationality=Danish"
65
+ #
66
+ # An optional namespace can be passed to enclose key names:
67
+ #
68
+ # {name: 'David', nationality: 'Danish'}.to_query('user')
69
+ # # => "user%5Bname%5D=David&user%5Bnationality%5D=Danish"
70
+ #
71
+ # The string pairs "key=value" that conform the query string
72
+ # are sorted lexicographically in ascending order.
73
+ #
74
+ # This method is also aliased as +to_param+.
75
+ def to_query(namespace = nil)
76
+ collect do |key, value|
77
+ unless (value.is_a?(Hash) || value.is_a?(Array)) && value.empty?
78
+ value.to_query(namespace ? "#{namespace}[#{key}]" : key)
79
+ end
80
+ end.compact.sort! * '&'
81
+ end
82
+
83
+ alias_method :to_param, :to_query
84
+ end
@@ -0,0 +1,146 @@
1
+ require 'delegate'
2
+
3
+ module ActiveSupport
4
+ module Tryable #:nodoc:
5
+ def try(*a, &b)
6
+ try!(*a, &b) if a.empty? || respond_to?(a.first)
7
+ end
8
+
9
+ def try!(*a, &b)
10
+ if a.empty? && block_given?
11
+ if b.arity == 0
12
+ instance_eval(&b)
13
+ else
14
+ yield self
15
+ end
16
+ else
17
+ public_send(*a, &b)
18
+ end
19
+ end
20
+ end
21
+ end
22
+
23
+ class Object
24
+ include ActiveSupport::Tryable
25
+
26
+ ##
27
+ # :method: try
28
+ #
29
+ # :call-seq:
30
+ # try(*a, &b)
31
+ #
32
+ # Invokes the public method whose name goes as first argument just like
33
+ # +public_send+ does, except that if the receiver does not respond to it the
34
+ # call returns +nil+ rather than raising an exception.
35
+ #
36
+ # This method is defined to be able to write
37
+ #
38
+ # @person.try(:name)
39
+ #
40
+ # instead of
41
+ #
42
+ # @person.name if @person
43
+ #
44
+ # +try+ calls can be chained:
45
+ #
46
+ # @person.try(:spouse).try(:name)
47
+ #
48
+ # instead of
49
+ #
50
+ # @person.spouse.name if @person && @person.spouse
51
+ #
52
+ # +try+ will also return +nil+ if the receiver does not respond to the method:
53
+ #
54
+ # @person.try(:non_existing_method) # => nil
55
+ #
56
+ # instead of
57
+ #
58
+ # @person.non_existing_method if @person.respond_to?(:non_existing_method) # => nil
59
+ #
60
+ # +try+ returns +nil+ when called on +nil+ regardless of whether it responds
61
+ # to the method:
62
+ #
63
+ # nil.try(:to_i) # => nil, rather than 0
64
+ #
65
+ # Arguments and blocks are forwarded to the method if invoked:
66
+ #
67
+ # @posts.try(:each_slice, 2) do |a, b|
68
+ # ...
69
+ # end
70
+ #
71
+ # The number of arguments in the signature must match. If the object responds
72
+ # to the method the call is attempted and +ArgumentError+ is still raised
73
+ # in case of argument mismatch.
74
+ #
75
+ # If +try+ is called without arguments it yields the receiver to a given
76
+ # block unless it is +nil+:
77
+ #
78
+ # @person.try do |p|
79
+ # ...
80
+ # end
81
+ #
82
+ # You can also call try with a block without accepting an argument, and the block
83
+ # will be instance_eval'ed instead:
84
+ #
85
+ # @person.try { upcase.truncate(50) }
86
+ #
87
+ # Please also note that +try+ is defined on +Object+. Therefore, it won't work
88
+ # with instances of classes that do not have +Object+ among their ancestors,
89
+ # like direct subclasses of +BasicObject+.
90
+
91
+ ##
92
+ # :method: try!
93
+ #
94
+ # :call-seq:
95
+ # try!(*a, &b)
96
+ #
97
+ # Same as #try, but raises a +NoMethodError+ exception if the receiver is
98
+ # not +nil+ and does not implement the tried method.
99
+ #
100
+ # "a".try!(:upcase) # => "A"
101
+ # nil.try!(:upcase) # => nil
102
+ # 123.try!(:upcase) # => NoMethodError: undefined method `upcase' for 123:Integer
103
+ end
104
+
105
+ class Delegator
106
+ include ActiveSupport::Tryable
107
+
108
+ ##
109
+ # :method: try
110
+ #
111
+ # :call-seq:
112
+ # try(a*, &b)
113
+ #
114
+ # See Object#try
115
+
116
+ ##
117
+ # :method: try!
118
+ #
119
+ # :call-seq:
120
+ # try!(a*, &b)
121
+ #
122
+ # See Object#try!
123
+ end
124
+
125
+ class NilClass
126
+ # Calling +try+ on +nil+ always returns +nil+.
127
+ # It becomes especially helpful when navigating through associations that may return +nil+.
128
+ #
129
+ # nil.try(:name) # => nil
130
+ #
131
+ # Without +try+
132
+ # @person && @person.children.any? && @person.children.first.name
133
+ #
134
+ # With +try+
135
+ # @person.try(:children).try(:first).try(:name)
136
+ def try(*args)
137
+ nil
138
+ end
139
+
140
+ # Calling +try!+ on +nil+ always returns +nil+.
141
+ #
142
+ # nil.try!(:name) # => nil
143
+ def try!(*args)
144
+ nil
145
+ end
146
+ end