swivel 0.0.146 → 0.0.149

Sign up to get free protection for your applications and to get access to all the features.
Files changed (183) hide show
  1. data/README +4 -1
  2. data/Rakefile +1 -1
  3. data/lib/swivel.rb +1 -1
  4. data/lib/swivel2/benchmarking.rb +1 -0
  5. data/lib/swivel2/config.rb +45 -0
  6. data/lib/swivel2/connection.rb +89 -0
  7. data/lib/swivel2/formats.rb +11 -0
  8. data/lib/swivel2/logging.rb +1 -0
  9. data/lib/swivel2/performance.rb +21 -0
  10. data/lib/swivel2/response.rb +5 -0
  11. data/lib/swivel2/swivelrc.default +5 -0
  12. data/vendor/activeresource-2.0.2-/CHANGELOG +223 -0
  13. data/vendor/activeresource-2.0.2-/README +165 -0
  14. data/vendor/activeresource-2.0.2-/Rakefile +133 -0
  15. data/vendor/activeresource-2.0.2-/lib/active_resource.rb +47 -0
  16. data/vendor/activeresource-2.0.2-/lib/active_resource/base.rb +872 -0
  17. data/vendor/activeresource-2.0.2-/lib/active_resource/connection.rb +172 -0
  18. data/vendor/activeresource-2.0.2-/lib/active_resource/custom_methods.rb +105 -0
  19. data/vendor/activeresource-2.0.2-/lib/active_resource/formats.rb +14 -0
  20. data/vendor/activeresource-2.0.2-/lib/active_resource/formats/json_format.rb +23 -0
  21. data/vendor/activeresource-2.0.2-/lib/active_resource/formats/xml_format.rb +34 -0
  22. data/vendor/activeresource-2.0.2-/lib/active_resource/http_mock.rb +147 -0
  23. data/vendor/activeresource-2.0.2-/lib/active_resource/validations.rb +288 -0
  24. data/vendor/activeresource-2.0.2-/lib/active_resource/version.rb +9 -0
  25. data/vendor/activeresource-2.0.2-/lib/activeresource.rb +1 -0
  26. data/vendor/activeresource-2.0.2-/test/abstract_unit.rb +10 -0
  27. data/vendor/activeresource-2.0.2-/test/authorization_test.rb +82 -0
  28. data/vendor/activeresource-2.0.2-/test/base/custom_methods_test.rb +96 -0
  29. data/vendor/activeresource-2.0.2-/test/base/equality_test.rb +43 -0
  30. data/vendor/activeresource-2.0.2-/test/base/load_test.rb +111 -0
  31. data/vendor/activeresource-2.0.2-/test/base_errors_test.rb +48 -0
  32. data/vendor/activeresource-2.0.2-/test/base_test.rb +454 -0
  33. data/vendor/activeresource-2.0.2-/test/connection_test.rb +170 -0
  34. data/vendor/activeresource-2.0.2-/test/fixtures/beast.rb +14 -0
  35. data/vendor/activeresource-2.0.2-/test/fixtures/person.rb +3 -0
  36. data/vendor/activeresource-2.0.2-/test/fixtures/street_address.rb +4 -0
  37. data/vendor/activeresource-2.0.2-/test/format_test.rb +42 -0
  38. data/vendor/activeresource-2.0.2-/test/setter_trap.rb +27 -0
  39. data/vendor/activesupport-2.0.2-/CHANGELOG +986 -0
  40. data/vendor/activesupport-2.0.2-/README +43 -0
  41. data/vendor/activesupport-2.0.2-/lib/active_support.rb +49 -0
  42. data/vendor/activesupport-2.0.2-/lib/active_support/basic_object.rb +5 -0
  43. data/vendor/activesupport-2.0.2-/lib/active_support/buffered_logger.rb +107 -0
  44. data/vendor/activesupport-2.0.2-/lib/active_support/clean_logger.rb +127 -0
  45. data/vendor/activesupport-2.0.2-/lib/active_support/core_ext.rb +4 -0
  46. data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/array.rb +13 -0
  47. data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/array/access.rb +28 -0
  48. data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/array/conversions.rb +94 -0
  49. data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/array/extract_options.rb +19 -0
  50. data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/array/grouping.rb +68 -0
  51. data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/array/random_access.rb +12 -0
  52. data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/bigdecimal.rb +2 -0
  53. data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/bigdecimal/conversions.rb +6 -0
  54. data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/blank.rb +50 -0
  55. data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/cgi.rb +5 -0
  56. data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/cgi/escape_skipping_slashes.rb +14 -0
  57. data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/class.rb +4 -0
  58. data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/class/attribute_accessors.rb +48 -0
  59. data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/class/delegating_attributes.rb +40 -0
  60. data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/class/inheritable_attributes.rb +140 -0
  61. data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/class/removal.rb +24 -0
  62. data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/date.rb +10 -0
  63. data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/date/behavior.rb +13 -0
  64. data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/date/calculations.rb +188 -0
  65. data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/date/conversions.rb +98 -0
  66. data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/date_time.rb +10 -0
  67. data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/date_time/calculations.rb +77 -0
  68. data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/date_time/conversions.rb +74 -0
  69. data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/duplicable.rb +37 -0
  70. data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/enumerable.rb +63 -0
  71. data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/exception.rb +33 -0
  72. data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/file.rb +21 -0
  73. data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/float.rb +5 -0
  74. data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/float/rounding.rb +24 -0
  75. data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/hash.rb +13 -0
  76. data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/hash/conversions.rb +242 -0
  77. data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/hash/diff.rb +19 -0
  78. data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/hash/except.rb +24 -0
  79. data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/hash/indifferent_access.rb +102 -0
  80. data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/hash/keys.rb +54 -0
  81. data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/hash/reverse_merge.rb +25 -0
  82. data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/hash/slice.rb +28 -0
  83. data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/integer.rb +7 -0
  84. data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/integer/even_odd.rb +24 -0
  85. data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/integer/inflections.rb +21 -0
  86. data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/kernel.rb +5 -0
  87. data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/kernel/agnostics.rb +11 -0
  88. data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/kernel/daemonizing.rb +15 -0
  89. data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/kernel/debugger.rb +13 -0
  90. data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/kernel/reporting.rb +51 -0
  91. data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/kernel/requires.rb +24 -0
  92. data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/load_error.rb +38 -0
  93. data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/logger.rb +16 -0
  94. data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/module.rb +8 -0
  95. data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/module/aliasing.rb +70 -0
  96. data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/module/attr_accessor_with_default.rb +31 -0
  97. data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/module/attr_internal.rb +31 -0
  98. data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/module/attribute_accessors.rb +48 -0
  99. data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/module/delegation.rb +62 -0
  100. data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/module/inclusion.rb +11 -0
  101. data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/module/introspection.rb +35 -0
  102. data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/module/loading.rb +13 -0
  103. data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/name_error.rb +17 -0
  104. data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/numeric.rb +7 -0
  105. data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/numeric/bytes.rb +44 -0
  106. data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/numeric/time.rb +91 -0
  107. data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/object.rb +4 -0
  108. data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/object/conversions.rb +14 -0
  109. data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/object/extending.rb +58 -0
  110. data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/object/instance_variables.rb +22 -0
  111. data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/object/misc.rb +59 -0
  112. data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/pathname.rb +7 -0
  113. data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/pathname/clean_within.rb +14 -0
  114. data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/proc.rb +12 -0
  115. data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/range.rb +11 -0
  116. data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/range/blockless_step.rb +22 -0
  117. data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/range/conversions.rb +23 -0
  118. data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/range/include_range.rb +22 -0
  119. data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/range/overlaps.rb +12 -0
  120. data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/string.rb +23 -0
  121. data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/string/access.rb +58 -0
  122. data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/string/conversions.rb +28 -0
  123. data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/string/inflections.rb +153 -0
  124. data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/string/iterators.rb +17 -0
  125. data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/string/starts_ends_with.rb +27 -0
  126. data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/string/unicode.rb +42 -0
  127. data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/string/xchar.rb +11 -0
  128. data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/symbol.rb +14 -0
  129. data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/test.rb +1 -0
  130. data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/test/unit/assertions.rb +62 -0
  131. data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/time.rb +19 -0
  132. data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/time/behavior.rb +13 -0
  133. data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/time/calculations.rb +224 -0
  134. data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/time/conversions.rb +94 -0
  135. data/vendor/activesupport-2.0.2-/lib/active_support/dependencies.rb +540 -0
  136. data/vendor/activesupport-2.0.2-/lib/active_support/deprecation.rb +204 -0
  137. data/vendor/activesupport-2.0.2-/lib/active_support/duration.rb +96 -0
  138. data/vendor/activesupport-2.0.2-/lib/active_support/inflections.rb +53 -0
  139. data/vendor/activesupport-2.0.2-/lib/active_support/inflector.rb +282 -0
  140. data/vendor/activesupport-2.0.2-/lib/active_support/json.rb +31 -0
  141. data/vendor/activesupport-2.0.2-/lib/active_support/json/decoding.rb +60 -0
  142. data/vendor/activesupport-2.0.2-/lib/active_support/json/encoders/date.rb +5 -0
  143. data/vendor/activesupport-2.0.2-/lib/active_support/json/encoders/date_time.rb +5 -0
  144. data/vendor/activesupport-2.0.2-/lib/active_support/json/encoders/enumerable.rb +12 -0
  145. data/vendor/activesupport-2.0.2-/lib/active_support/json/encoders/false_class.rb +5 -0
  146. data/vendor/activesupport-2.0.2-/lib/active_support/json/encoders/hash.rb +50 -0
  147. data/vendor/activesupport-2.0.2-/lib/active_support/json/encoders/nil_class.rb +5 -0
  148. data/vendor/activesupport-2.0.2-/lib/active_support/json/encoders/numeric.rb +5 -0
  149. data/vendor/activesupport-2.0.2-/lib/active_support/json/encoders/object.rb +6 -0
  150. data/vendor/activesupport-2.0.2-/lib/active_support/json/encoders/regexp.rb +5 -0
  151. data/vendor/activesupport-2.0.2-/lib/active_support/json/encoders/string.rb +30 -0
  152. data/vendor/activesupport-2.0.2-/lib/active_support/json/encoders/symbol.rb +5 -0
  153. data/vendor/activesupport-2.0.2-/lib/active_support/json/encoders/time.rb +5 -0
  154. data/vendor/activesupport-2.0.2-/lib/active_support/json/encoders/true_class.rb +5 -0
  155. data/vendor/activesupport-2.0.2-/lib/active_support/json/encoding.rb +38 -0
  156. data/vendor/activesupport-2.0.2-/lib/active_support/json/variable.rb +10 -0
  157. data/vendor/activesupport-2.0.2-/lib/active_support/multibyte.rb +9 -0
  158. data/vendor/activesupport-2.0.2-/lib/active_support/multibyte/chars.rb +141 -0
  159. data/vendor/activesupport-2.0.2-/lib/active_support/multibyte/generators/generate_tables.rb +149 -0
  160. data/vendor/activesupport-2.0.2-/lib/active_support/multibyte/handlers/passthru_handler.rb +9 -0
  161. data/vendor/activesupport-2.0.2-/lib/active_support/multibyte/handlers/utf8_handler.rb +564 -0
  162. data/vendor/activesupport-2.0.2-/lib/active_support/multibyte/handlers/utf8_handler_proc.rb +43 -0
  163. data/vendor/activesupport-2.0.2-/lib/active_support/option_merger.rb +25 -0
  164. data/vendor/activesupport-2.0.2-/lib/active_support/ordered_options.rb +49 -0
  165. data/vendor/activesupport-2.0.2-/lib/active_support/test_case.rb +5 -0
  166. data/vendor/activesupport-2.0.2-/lib/active_support/testing.rb +1 -0
  167. data/vendor/activesupport-2.0.2-/lib/active_support/testing/default.rb +12 -0
  168. data/vendor/activesupport-2.0.2-/lib/active_support/values/time_zone.rb +181 -0
  169. data/vendor/activesupport-2.0.2-/lib/active_support/values/unicode_tables.dat +0 -0
  170. data/vendor/activesupport-2.0.2-/lib/active_support/vendor.rb +14 -0
  171. data/vendor/activesupport-2.0.2-/lib/active_support/vendor/builder-2.1.2/blankslate.rb +113 -0
  172. data/vendor/activesupport-2.0.2-/lib/active_support/vendor/builder-2.1.2/builder.rb +13 -0
  173. data/vendor/activesupport-2.0.2-/lib/active_support/vendor/builder-2.1.2/builder/blankslate.rb +20 -0
  174. data/vendor/activesupport-2.0.2-/lib/active_support/vendor/builder-2.1.2/builder/css.rb +250 -0
  175. data/vendor/activesupport-2.0.2-/lib/active_support/vendor/builder-2.1.2/builder/xchar.rb +115 -0
  176. data/vendor/activesupport-2.0.2-/lib/active_support/vendor/builder-2.1.2/builder/xmlbase.rb +139 -0
  177. data/vendor/activesupport-2.0.2-/lib/active_support/vendor/builder-2.1.2/builder/xmlevents.rb +63 -0
  178. data/vendor/activesupport-2.0.2-/lib/active_support/vendor/builder-2.1.2/builder/xmlmarkup.rb +328 -0
  179. data/vendor/activesupport-2.0.2-/lib/active_support/vendor/xml-simple-1.0.11/xmlsimple.rb +1021 -0
  180. data/vendor/activesupport-2.0.2-/lib/active_support/version.rb +9 -0
  181. data/vendor/activesupport-2.0.2-/lib/active_support/whiny_nil.rb +38 -0
  182. data/vendor/activesupport-2.0.2-/lib/activesupport.rb +1 -0
  183. metadata +222 -2
@@ -0,0 +1,31 @@
1
+ require 'active_support/json/encoding'
2
+ require 'active_support/json/decoding'
3
+
4
+ module ActiveSupport
5
+ module JSON
6
+ RESERVED_WORDS = %w(
7
+ abstract delete goto private transient
8
+ boolean do if protected try
9
+ break double implements public typeof
10
+ byte else import return var
11
+ case enum in short void
12
+ catch export instanceof static volatile
13
+ char extends int super while
14
+ class final interface switch with
15
+ const finally long synchronized
16
+ continue float native this
17
+ debugger for new throw
18
+ default function package throws
19
+ ) #:nodoc:
20
+
21
+ class << self
22
+ def valid_identifier?(key) #:nodoc:
23
+ key.to_s =~ /^[[:alpha:]_$][[:alnum:]_$]*$/ && !reserved_word?(key)
24
+ end
25
+
26
+ def reserved_word?(key) #:nodoc:
27
+ RESERVED_WORDS.include?(key.to_s)
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,60 @@
1
+ require 'yaml'
2
+ require 'strscan'
3
+
4
+ module ActiveSupport
5
+ module JSON
6
+ class ParseError < StandardError
7
+ end
8
+
9
+ class << self
10
+ # Converts a JSON string into a Ruby object.
11
+ def decode(json)
12
+ YAML.load(convert_json_to_yaml(json))
13
+ rescue ArgumentError => e
14
+ raise ParseError, "Invalid JSON string"
15
+ end
16
+
17
+ protected
18
+ # matches YAML-formatted dates
19
+ DATE_REGEX = /^\d{4}-\d{2}-\d{2}|\d{4}-\d{1,2}-\d{1,2}[ \t]+\d{1,2}:\d{2}:\d{2}(\.[0-9]*)?(([ \t]*)Z|[-+]\d{2}?(:\d{2})?)?$/
20
+
21
+ # Ensure that ":" and "," are always followed by a space
22
+ def convert_json_to_yaml(json) #:nodoc:
23
+ scanner, quoting, marks, pos, times = StringScanner.new(json), false, [], nil, []
24
+ while scanner.scan_until(/(\\['"]|['":,\\]|\\.)/)
25
+ case char = scanner[1]
26
+ when '"', "'"
27
+ if !quoting
28
+ quoting = char
29
+ pos = scanner.pos
30
+ elsif quoting == char
31
+ if json[pos..scanner.pos-2] =~ DATE_REGEX
32
+ # found a date, track the exact positions of the quotes so we can remove them later.
33
+ # oh, and increment them for each current mark, each one is an extra padded space that bumps
34
+ # the position in the final yaml output
35
+ total_marks = marks.size
36
+ times << pos+total_marks << scanner.pos+total_marks
37
+ end
38
+ quoting = false
39
+ end
40
+ when ":",","
41
+ marks << scanner.pos - 1 unless quoting
42
+ end
43
+ end
44
+
45
+ if marks.empty?
46
+ json.gsub(/\\\//, '/')
47
+ else
48
+ # FIXME: multiple slow enumerations
49
+ output = ([0] + marks.map(&:succ)).
50
+ zip(marks + [json.length]).
51
+ map { |left, right| json[left..right] }.
52
+ join(" ")
53
+ times.each { |i| output[i-1] = ' ' }
54
+ output.gsub!(/\\\//, '/')
55
+ output
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,5 @@
1
+ class Date
2
+ def to_json(options = nil) #:nodoc:
3
+ %("#{strftime("%Y/%m/%d")}")
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ class DateTime
2
+ def to_json(options = nil) #:nodoc:
3
+ %("#{strftime("%Y/%m/%d %H:%M:%S %z")}")
4
+ end
5
+ end
@@ -0,0 +1,12 @@
1
+ module Enumerable
2
+ # Returns a JSON string representing the enumerable. Any +options+
3
+ # given will be passed on to its elements. For example:
4
+ #
5
+ # users = User.find(:all)
6
+ # users.to_json(:only => :name)
7
+ #
8
+ # will pass the <tt>:only => :name</tt> option to each user.
9
+ def to_json(options = {}) #:nodoc:
10
+ "[#{map { |value| ActiveSupport::JSON.encode(value, options) } * ', '}]"
11
+ end
12
+ end
@@ -0,0 +1,5 @@
1
+ class FalseClass
2
+ def to_json(options = nil) #:nodoc:
3
+ 'false'
4
+ end
5
+ end
@@ -0,0 +1,50 @@
1
+ class Hash
2
+ # Returns a JSON string representing the hash.
3
+ #
4
+ # Without any +options+, the returned JSON string will include all
5
+ # the hash keys. For example:
6
+ #
7
+ # { :name => "Konata Izumi", 'age' => 16, 1 => 2 }.to_json
8
+ #
9
+ # {"name": "Konata Izumi", 1: 2, "age": 16}
10
+ #
11
+ # The keys in the JSON string are unordered due to the nature of hashes.
12
+ #
13
+ # The <tt>:only</tt> and <tt>:except</tt> options can be used to limit the
14
+ # attributes included, and will accept 1 or more hash keys to include/exclude.
15
+ #
16
+ # { :name => "Konata Izumi", 'age' => 16, 1 => 2 }.to_json(:only => [:name, 'age'])
17
+ #
18
+ # {"name": "Konata Izumi", "age": 16}
19
+ #
20
+ # { :name => "Konata Izumi", 'age' => 16, 1 => 2 }.to_json(:except => 1)
21
+ #
22
+ # {"name": "Konata Izumi", "age": 16}
23
+ #
24
+ # The +options+ also filter down to any hash values. This is particularly
25
+ # useful for converting hashes containing ActiveRecord objects or any object
26
+ # that responds to options in their <tt>to_json</tt> method. For example:
27
+ #
28
+ # users = User.find(:all)
29
+ # { :users => users, :count => users.size }.to_json(:include => :posts)
30
+ #
31
+ # would pass the <tt>:include => :posts</tt> option to <tt>users</tt>,
32
+ # allowing the posts association in the User model to be converted to JSON
33
+ # as well.
34
+ def to_json(options = {}) #:nodoc:
35
+ hash_keys = self.keys
36
+
37
+ if options[:except]
38
+ hash_keys = hash_keys - Array(options[:except])
39
+ elsif options[:only]
40
+ hash_keys = hash_keys & Array(options[:only])
41
+ end
42
+
43
+ returning result = '{' do
44
+ result << hash_keys.map do |key|
45
+ "#{ActiveSupport::JSON.encode(key)}: #{ActiveSupport::JSON.encode(self[key], options)}"
46
+ end * ', '
47
+ result << '}'
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,5 @@
1
+ class NilClass
2
+ def to_json(options = nil) #:nodoc:
3
+ 'null'
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ class Numeric
2
+ def to_json(options = nil) #:nodoc:
3
+ to_s
4
+ end
5
+ end
@@ -0,0 +1,6 @@
1
+ class Object
2
+ # Dumps object in JSON (JavaScript Object Notation). See www.json.org for more info.
3
+ def to_json(options = {})
4
+ ActiveSupport::JSON.encode(instance_values, options)
5
+ end
6
+ end
@@ -0,0 +1,5 @@
1
+ class Regexp
2
+ def to_json(options = nil) #:nodoc:
3
+ inspect
4
+ end
5
+ end
@@ -0,0 +1,30 @@
1
+ module ActiveSupport
2
+ module JSON
3
+ module Encoding
4
+ ESCAPED_CHARS = {
5
+ "\010" => '\b',
6
+ "\f" => '\f',
7
+ "\n" => '\n',
8
+ "\r" => '\r',
9
+ "\t" => '\t',
10
+ '"' => '\"',
11
+ '\\' => '\\\\',
12
+ '>' => '\u003E',
13
+ '<' => '\u003C',
14
+ '&' => '\u0026'
15
+ }
16
+ end
17
+ end
18
+ end
19
+
20
+ class String
21
+ def to_json(options = nil) #:nodoc:
22
+ '"' + gsub(/[\010\f\n\r\t"\\><&]/) { |s|
23
+ ActiveSupport::JSON::Encoding::ESCAPED_CHARS[s]
24
+ }.gsub(/([\xC0-\xDF][\x80-\xBF]|
25
+ [\xE0-\xEF][\x80-\xBF]{2}|
26
+ [\xF0-\xF7][\x80-\xBF]{3})+/nx) { |s|
27
+ s.unpack("U*").pack("n*").unpack("H*")[0].gsub(/.{4}/, '\\\\u\&')
28
+ } + '"'
29
+ end
30
+ end
@@ -0,0 +1,5 @@
1
+ class Symbol
2
+ def to_json(options = {}) #:nodoc:
3
+ ActiveSupport::JSON.encode(to_s, options)
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ class Time
2
+ def to_json(options = nil) #:nodoc:
3
+ to_datetime.to_json(options)
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ class TrueClass
2
+ def to_json(options = nil) #:nodoc:
3
+ 'true'
4
+ end
5
+ end
@@ -0,0 +1,38 @@
1
+ require 'active_support/json/variable'
2
+
3
+ require 'active_support/json/encoders/object' # Require explicitly for rdoc.
4
+ Dir["#{File.dirname(__FILE__)}/encoders/**/*.rb"].each do |file|
5
+ basename = File.basename(file, '.rb')
6
+ unless basename == 'object'
7
+ require "active_support/json/encoders/#{basename}"
8
+ end
9
+ end
10
+
11
+ module ActiveSupport
12
+ module JSON
13
+ class CircularReferenceError < StandardError
14
+ end
15
+
16
+ class << self
17
+ REFERENCE_STACK_VARIABLE = :json_reference_stack #:nodoc:
18
+
19
+ # Converts a Ruby object into a JSON string.
20
+ def encode(value, options = {})
21
+ raise_on_circular_reference(value) do
22
+ value.send(:to_json, options)
23
+ end
24
+ end
25
+
26
+ protected
27
+ def raise_on_circular_reference(value) #:nodoc:
28
+ stack = Thread.current[REFERENCE_STACK_VARIABLE] ||= []
29
+ raise CircularReferenceError, 'object references itself' if
30
+ stack.include? value
31
+ stack << value
32
+ yield
33
+ ensure
34
+ stack.pop
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,10 @@
1
+ module ActiveSupport
2
+ module JSON
3
+ # A string that returns itself as its JSON-encoded form.
4
+ class Variable < String
5
+ def to_json(options=nil)
6
+ self
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,9 @@
1
+ module ActiveSupport
2
+ module Multibyte #:nodoc:
3
+ DEFAULT_NORMALIZATION_FORM = :kc
4
+ NORMALIZATIONS_FORMS = [:c, :kc, :d, :kd]
5
+ UNICODE_VERSION = '5.0.0'
6
+ end
7
+ end
8
+
9
+ require 'active_support/multibyte/chars'
@@ -0,0 +1,141 @@
1
+ require 'active_support/multibyte/handlers/utf8_handler'
2
+ require 'active_support/multibyte/handlers/passthru_handler'
3
+
4
+ # Encapsulates all the functionality related to the Chars proxy.
5
+ module ActiveSupport::Multibyte #:nodoc:
6
+ # Chars enables you to work transparently with multibyte encodings in the Ruby String class without having extensive
7
+ # knowledge about the encoding. A Chars object accepts a string upon initialization and proxies String methods in an
8
+ # encoding safe manner. All the normal String methods are also implemented on the proxy.
9
+ #
10
+ # String methods are proxied through the Chars object, and can be accessed through the +chars+ method. Methods
11
+ # which would normally return a String object now return a Chars object so methods can be chained.
12
+ #
13
+ # "The Perfect String ".chars.downcase.strip.normalize #=> "the perfect string"
14
+ #
15
+ # Chars objects are perfectly interchangeable with String objects as long as no explicit class checks are made.
16
+ # If certain methods do explicitly check the class, call +to_s+ before you pass chars objects to them.
17
+ #
18
+ # bad.explicit_checking_method "T".chars.downcase.to_s
19
+ #
20
+ # The actual operations on the string are delegated to handlers. Theoretically handlers can be implemented for
21
+ # any encoding, but the default handler handles UTF-8. This handler is set during initialization, if you want to
22
+ # use you own handler, you can set it on the Chars class. Look at the UTF8Handler source for an example how to
23
+ # implement your own handler. If you your own handler to work on anything but UTF-8 you probably also
24
+ # want to override Chars#handler.
25
+ #
26
+ # ActiveSupport::Multibyte::Chars.handler = MyHandler
27
+ #
28
+ # Note that a few methods are defined on Chars instead of the handler because they are defined on Object or Kernel
29
+ # and method_missing can't catch them.
30
+ class Chars
31
+
32
+ attr_reader :string # The contained string
33
+ alias_method :to_s, :string
34
+
35
+ include Comparable
36
+
37
+ # The magic method to make String and Chars comparable
38
+ def to_str
39
+ # Using any other ways of overriding the String itself will lead you all the way from infinite loops to
40
+ # core dumps. Don't go there.
41
+ @string
42
+ end
43
+
44
+ # Make duck-typing with String possible
45
+ def respond_to?(method)
46
+ super || @string.respond_to?(method) || handler.respond_to?(method) ||
47
+ (method.to_s =~ /(.*)!/ && handler.respond_to?($1)) || false
48
+ end
49
+
50
+ # Create a new Chars instance.
51
+ def initialize(str)
52
+ @string = str.respond_to?(:string) ? str.string : str
53
+ end
54
+
55
+ # Returns -1, 0 or +1 depending on whether the Chars object is to be sorted before, equal or after the
56
+ # object on the right side of the operation. It accepts any object that implements +to_s+. See String.<=>
57
+ # for more details.
58
+ def <=>(other); @string <=> other.to_s; end
59
+
60
+ # Works just like String#split, with the exception that the items in the resulting list are Chars
61
+ # instances instead of String. This makes chaining methods easier.
62
+ def split(*args)
63
+ @string.split(*args).map { |i| i.chars }
64
+ end
65
+
66
+ # Gsub works exactly the same as gsub on a normal string.
67
+ def gsub(*a, &b); @string.gsub(*a, &b).chars; end
68
+
69
+ # Like String.=~ only it returns the character offset (in codepoints) instead of the byte offset.
70
+ def =~(other)
71
+ handler.translate_offset(@string, @string =~ other)
72
+ end
73
+
74
+ # Try to forward all undefined methods to the handler, when a method is not defined on the handler, send it to
75
+ # the contained string. Method_missing is also responsible for making the bang! methods destructive.
76
+ def method_missing(m, *a, &b)
77
+ begin
78
+ # Simulate methods with a ! at the end because we can't touch the enclosed string from the handlers.
79
+ if m.to_s =~ /^(.*)\!$/ && handler.respond_to?($1)
80
+ result = handler.send($1, @string, *a, &b)
81
+ if result == @string
82
+ result = nil
83
+ else
84
+ @string.replace result
85
+ end
86
+ elsif handler.respond_to?(m)
87
+ result = handler.send(m, @string, *a, &b)
88
+ else
89
+ result = @string.send(m, *a, &b)
90
+ end
91
+ rescue Handlers::EncodingError
92
+ @string.replace handler.tidy_bytes(@string)
93
+ retry
94
+ end
95
+
96
+ if result.kind_of?(String)
97
+ result.chars
98
+ else
99
+ result
100
+ end
101
+ end
102
+
103
+ # Set the handler class for the Char objects.
104
+ def self.handler=(klass)
105
+ @@handler = klass
106
+ end
107
+
108
+ # Returns the proper handler for the contained string depending on $KCODE and the encoding of the string. This
109
+ # method is used internally to always redirect messages to the proper classes depending on the context.
110
+ def handler
111
+ if utf8_pragma?
112
+ @@handler
113
+ else
114
+ ActiveSupport::Multibyte::Handlers::PassthruHandler
115
+ end
116
+ end
117
+
118
+ private
119
+
120
+ # +utf8_pragma+ checks if it can send this string to the handlers. It makes sure @string isn't nil and $KCODE is
121
+ # set to 'UTF8'.
122
+ if RUBY_VERSION < '1.9'
123
+ def utf8_pragma?
124
+ !@string.nil? && ($KCODE == 'UTF8')
125
+ end
126
+ else
127
+ def utf8_pragma?
128
+ !@string.nil? && (Encoding.default_external == Encoding::UTF_8)
129
+ end
130
+ end
131
+ end
132
+ end
133
+
134
+ # When we can load the utf8proc library, override normalization with the faster methods
135
+ begin
136
+ require 'utf8proc_native'
137
+ require 'active_support/multibyte/handlers/utf8_handler_proc'
138
+ ActiveSupport::Multibyte::Chars.handler = ActiveSupport::Multibyte::Handlers::UTF8HandlerProc
139
+ rescue LoadError
140
+ ActiveSupport::Multibyte::Chars.handler = ActiveSupport::Multibyte::Handlers::UTF8Handler
141
+ end