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,37 @@
1
+ class Object
2
+ # Can you safely .dup this object?
3
+ # False for nil, false, true, symbols, and numbers; true otherwise.
4
+ def duplicable?
5
+ true
6
+ end
7
+ end
8
+
9
+ class NilClass #:nodoc:
10
+ def duplicable?
11
+ false
12
+ end
13
+ end
14
+
15
+ class FalseClass #:nodoc:
16
+ def duplicable?
17
+ false
18
+ end
19
+ end
20
+
21
+ class TrueClass #:nodoc:
22
+ def duplicable?
23
+ false
24
+ end
25
+ end
26
+
27
+ class Symbol #:nodoc:
28
+ def duplicable?
29
+ false
30
+ end
31
+ end
32
+
33
+ class Numeric #:nodoc:
34
+ def duplicable?
35
+ false
36
+ end
37
+ end
@@ -0,0 +1,63 @@
1
+ module Enumerable
2
+ # Collect an enumerable into sets, grouped by the result of a block. Useful,
3
+ # for example, for grouping records by date.
4
+ #
5
+ # e.g.
6
+ #
7
+ # latest_transcripts.group_by(&:day).each do |day, transcripts|
8
+ # p "#{day} -> #{transcripts.map(&:class) * ', '}"
9
+ # end
10
+ # "2006-03-01 -> Transcript"
11
+ # "2006-02-28 -> Transcript"
12
+ # "2006-02-27 -> Transcript, Transcript"
13
+ # "2006-02-26 -> Transcript, Transcript"
14
+ # "2006-02-25 -> Transcript"
15
+ # "2006-02-24 -> Transcript, Transcript"
16
+ # "2006-02-23 -> Transcript"
17
+ def group_by
18
+ inject({}) do |groups, element|
19
+ (groups[yield(element)] ||= []) << element
20
+ groups
21
+ end
22
+ end if RUBY_VERSION < '1.9'
23
+
24
+ # Calculates a sum from the elements. Examples:
25
+ #
26
+ # payments.sum { |p| p.price * p.tax_rate }
27
+ # payments.sum(&:price)
28
+ #
29
+ # This is instead of payments.inject { |sum, p| sum + p.price }
30
+ #
31
+ # Also calculates sums without the use of a block:
32
+ # [5, 15, 10].sum # => 30
33
+ #
34
+ # The default identity (sum of an empty list) is zero.
35
+ # However, you can override this default:
36
+ #
37
+ # [].sum(Payment.new(0)) { |i| i.amount } # => Payment.new(0)
38
+ #
39
+ def sum(identity = 0, &block)
40
+ return identity unless size > 0
41
+
42
+ if block_given?
43
+ map(&block).sum
44
+ else
45
+ inject { |sum, element| sum + element }
46
+ end
47
+ end
48
+
49
+ # Convert an enumerable to a hash. Examples:
50
+ #
51
+ # people.index_by(&:login)
52
+ # => { "nextangle" => <Person ...>, "chade-" => <Person ...>, ...}
53
+ # people.index_by { |person| "#{person.first_name} #{person.last_name}" }
54
+ # => { "Chade- Fowlersburg-e" => <Person ...>, "David Heinemeier Hansson" => <Person ...>, ...}
55
+ #
56
+ def index_by
57
+ inject({}) do |accum, elem|
58
+ accum[yield(elem)] = elem
59
+ accum
60
+ end
61
+ end
62
+
63
+ end
@@ -0,0 +1,33 @@
1
+ class Exception # :nodoc:
2
+ def clean_message
3
+ Pathname.clean_within message
4
+ end
5
+
6
+ TraceSubstitutions = []
7
+ FrameworkRegexp = /generated|vendor|dispatch|ruby|script\/\w+/
8
+
9
+ def clean_backtrace
10
+ backtrace.collect do |line|
11
+ Pathname.clean_within(TraceSubstitutions.inject(line) do |result, (regexp, sub)|
12
+ result.gsub regexp, sub
13
+ end)
14
+ end
15
+ end
16
+
17
+ def application_backtrace
18
+ before_application_frame = true
19
+
20
+ trace = clean_backtrace.reject do |line|
21
+ non_app_frame = (line =~ FrameworkRegexp)
22
+ before_application_frame = false unless non_app_frame
23
+ non_app_frame && ! before_application_frame
24
+ end
25
+
26
+ # If we didn't find any application frames, return an empty app trace.
27
+ before_application_frame ? [] : trace
28
+ end
29
+
30
+ def framework_backtrace
31
+ clean_backtrace.grep FrameworkRegexp
32
+ end
33
+ end
@@ -0,0 +1,21 @@
1
+ require 'tempfile'
2
+
3
+ # Write to a file atomically. Useful for situations where you don't
4
+ # want other processes or threads to see half-written files.
5
+ #
6
+ # File.atomic_write("important.file") do |file|
7
+ # file.write("hello")
8
+ # end
9
+ #
10
+ # If your temp directory is not on the same filesystem as the file you're
11
+ # trying to write, you can provide a different temporary directory.
12
+ #
13
+ # File.atomic_write("/data/something.imporant", "/data/tmp") do |f|
14
+ # file.write("hello")
15
+ # end
16
+ def File.atomic_write(file_name, temp_dir = Dir.tmpdir)
17
+ temp_file = Tempfile.new(File.basename(file_name), temp_dir)
18
+ yield temp_file
19
+ temp_file.close
20
+ File.rename(temp_file.path, file_name)
21
+ end
@@ -0,0 +1,5 @@
1
+ require 'active_support/core_ext/float/rounding'
2
+
3
+ class Float #:nodoc:
4
+ include ActiveSupport::CoreExtensions::Float::Rounding
5
+ end
@@ -0,0 +1,24 @@
1
+ module ActiveSupport #:nodoc:
2
+ module CoreExtensions #:nodoc:
3
+ module Float #:nodoc:
4
+ module Rounding
5
+ def self.included(base) #:nodoc:
6
+ base.class_eval do
7
+ alias_method :round_without_precision, :round
8
+ alias_method :round, :round_with_precision
9
+ end
10
+ end
11
+
12
+ # Rounds the float with the specified precision.
13
+ #
14
+ # x = 1.337
15
+ # x.round # => 1
16
+ # x.round(1) # => 1.3
17
+ # x.round(2) # => 1.34
18
+ def round_with_precision(precision = nil)
19
+ precision.nil? ? round_without_precision : (self * (10 ** precision)).round / (10 ** precision).to_f
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,13 @@
1
+ %w(keys indifferent_access reverse_merge conversions diff slice except).each do |ext|
2
+ require "active_support/core_ext/hash/#{ext}"
3
+ end
4
+
5
+ class Hash #:nodoc:
6
+ include ActiveSupport::CoreExtensions::Hash::Keys
7
+ include ActiveSupport::CoreExtensions::Hash::IndifferentAccess
8
+ include ActiveSupport::CoreExtensions::Hash::ReverseMerge
9
+ include ActiveSupport::CoreExtensions::Hash::Conversions
10
+ include ActiveSupport::CoreExtensions::Hash::Diff
11
+ include ActiveSupport::CoreExtensions::Hash::Slice
12
+ include ActiveSupport::CoreExtensions::Hash::Except
13
+ end
@@ -0,0 +1,242 @@
1
+ require 'date'
2
+ require 'cgi'
3
+ require 'base64'
4
+ require 'builder'
5
+ require 'xmlsimple'
6
+
7
+ # Locked down XmlSimple#xml_in_string
8
+ class XmlSimple
9
+ # Same as xml_in but doesn't try to smartly shoot itself in the foot.
10
+ def xml_in_string(string, options = nil)
11
+ handle_options('in', options)
12
+
13
+ @doc = parse(string)
14
+ result = collapse(@doc.root)
15
+
16
+ if @options['keeproot']
17
+ merge({}, @doc.root.name, result)
18
+ else
19
+ result
20
+ end
21
+ end
22
+
23
+ def self.xml_in_string(string, options = nil)
24
+ new.xml_in_string(string, options)
25
+ end
26
+ end
27
+
28
+ module ActiveSupport #:nodoc:
29
+ module CoreExtensions #:nodoc:
30
+ module Hash #:nodoc:
31
+ module Conversions
32
+ XML_TYPE_NAMES = {
33
+ "Symbol" => "symbol",
34
+ "Fixnum" => "integer",
35
+ "Bignum" => "integer",
36
+ "BigDecimal" => "decimal",
37
+ "Float" => "float",
38
+ "Date" => "date",
39
+ "DateTime" => "datetime",
40
+ "Time" => "datetime",
41
+ "TrueClass" => "boolean",
42
+ "FalseClass" => "boolean"
43
+ } unless defined?(XML_TYPE_NAMES)
44
+
45
+ XML_FORMATTING = {
46
+ "symbol" => Proc.new { |symbol| symbol.to_s },
47
+ "date" => Proc.new { |date| date.to_s(:db) },
48
+ "datetime" => Proc.new { |time| time.xmlschema },
49
+ "binary" => Proc.new { |binary| Base64.encode64(binary) },
50
+ "yaml" => Proc.new { |yaml| yaml.to_yaml }
51
+ } unless defined?(XML_FORMATTING)
52
+
53
+ # TODO: use Time.xmlschema instead of Time.parse;
54
+ # use regexp instead of Date.parse
55
+ unless defined?(XML_PARSING)
56
+ XML_PARSING = {
57
+ "symbol" => Proc.new { |symbol| symbol.to_sym },
58
+ "date" => Proc.new { |date| ::Date.parse(date) },
59
+ "datetime" => Proc.new { |time| ::Time.parse(time).utc },
60
+ "integer" => Proc.new { |integer| integer.to_i },
61
+ "float" => Proc.new { |float| float.to_f },
62
+ "decimal" => Proc.new { |number| BigDecimal(number) },
63
+ "boolean" => Proc.new { |boolean| %w(1 true).include?(boolean.strip) },
64
+ "string" => Proc.new { |string| string.to_s },
65
+ "yaml" => Proc.new { |yaml| YAML::load(yaml) rescue yaml },
66
+ "base64Binary" => Proc.new { |bin| Base64.decode64(bin) },
67
+ # FIXME: Get rid of eval and institute a proper decorator here
68
+ "file" => Proc.new do |file, entity|
69
+ f = StringIO.new(Base64.decode64(file))
70
+ eval "def f.original_filename() '#{entity["name"]}' || 'untitled' end"
71
+ eval "def f.content_type() '#{entity["content_type"]}' || 'application/octet-stream' end"
72
+ f
73
+ end
74
+ }
75
+
76
+ XML_PARSING.update(
77
+ "double" => XML_PARSING["float"],
78
+ "dateTime" => XML_PARSING["datetime"]
79
+ )
80
+ end
81
+
82
+ def self.included(klass)
83
+ klass.extend(ClassMethods)
84
+ end
85
+
86
+ # Converts a hash into a string suitable for use as a URL query string. An optional <tt>namespace</tt> can be
87
+ # passed to enclose the param names (see example below).
88
+ #
89
+ # ==== Example:
90
+ # { :name => 'David', :nationality => 'Danish' }.to_query # => "name=David&nationality=Danish"
91
+ #
92
+ # { :name => 'David', :nationality => 'Danish' }.to_query('user') # => "user%5Bname%5D=David&user%5Bnationality%5D=Danish"
93
+ def to_query(namespace = nil)
94
+ collect do |key, value|
95
+ value.to_query(namespace ? "#{namespace}[#{key}]" : key)
96
+ end.sort * '&'
97
+ end
98
+
99
+ def to_xml(options = {})
100
+ options[:indent] ||= 2
101
+ options.reverse_merge!({ :builder => Builder::XmlMarkup.new(:indent => options[:indent]),
102
+ :root => "hash" })
103
+ options[:builder].instruct! unless options.delete(:skip_instruct)
104
+ dasherize = !options.has_key?(:dasherize) || options[:dasherize]
105
+ root = dasherize ? options[:root].to_s.dasherize : options[:root].to_s
106
+
107
+ options[:builder].__send__(:method_missing, root) do
108
+ each do |key, value|
109
+ case value
110
+ when ::Hash
111
+ value.to_xml(options.merge({ :root => key, :skip_instruct => true }))
112
+ when ::Array
113
+ value.to_xml(options.merge({ :root => key, :children => key.to_s.singularize, :skip_instruct => true}))
114
+ when ::Method, ::Proc
115
+ # If the Method or Proc takes two arguments, then
116
+ # pass the suggested child element name. This is
117
+ # used if the Method or Proc will be operating over
118
+ # multiple records and needs to create an containing
119
+ # element that will contain the objects being
120
+ # serialized.
121
+ if 1 == value.arity
122
+ value.call(options.merge({ :root => key, :skip_instruct => true }))
123
+ else
124
+ value.call(options.merge({ :root => key, :skip_instruct => true }), key.to_s.singularize)
125
+ end
126
+ else
127
+ if value.respond_to?(:to_xml)
128
+ value.to_xml(options.merge({ :root => key, :skip_instruct => true }))
129
+ else
130
+ type_name = XML_TYPE_NAMES[value.class.name]
131
+
132
+ key = dasherize ? key.to_s.dasherize : key.to_s
133
+
134
+ attributes = options[:skip_types] || value.nil? || type_name.nil? ? { } : { :type => type_name }
135
+ if value.nil?
136
+ attributes[:nil] = true
137
+ end
138
+
139
+ options[:builder].tag!(key,
140
+ XML_FORMATTING[type_name] ? XML_FORMATTING[type_name].call(value) : value,
141
+ attributes
142
+ )
143
+ end
144
+ end
145
+ end
146
+
147
+ yield options[:builder] if block_given?
148
+ end
149
+
150
+ end
151
+
152
+ module ClassMethods
153
+ def from_xml(xml)
154
+ # TODO: Refactor this into something much cleaner that doesn't rely on XmlSimple
155
+ typecast_xml_value(undasherize_keys(XmlSimple.xml_in_string(xml,
156
+ 'forcearray' => false,
157
+ 'forcecontent' => true,
158
+ 'keeproot' => true,
159
+ 'contentkey' => '__content__')
160
+ ))
161
+ end
162
+
163
+ private
164
+ def typecast_xml_value(value)
165
+ case value.class.to_s
166
+ when 'Hash'
167
+ if value['type'] == 'array'
168
+ child_key, entries = value.detect { |k,v| k != 'type' } # child_key is throwaway
169
+ if entries.nil? || (c = value['__content__'] && c.blank?)
170
+ []
171
+ else
172
+ case entries.class.to_s # something weird with classes not matching here. maybe singleton methods breaking is_a?
173
+ when "Array"
174
+ entries.collect { |v| typecast_xml_value(v) }
175
+ when "Hash"
176
+ [typecast_xml_value(entries)]
177
+ else
178
+ raise "can't typecast #{entries.inspect}"
179
+ end
180
+ end
181
+ elsif value.has_key?("__content__")
182
+ content = value["__content__"]
183
+ if parser = XML_PARSING[value["type"]]
184
+ if parser.arity == 2
185
+ XML_PARSING[value["type"]].call(content, value)
186
+ else
187
+ XML_PARSING[value["type"]].call(content)
188
+ end
189
+ else
190
+ content
191
+ end
192
+ elsif value['type'] == 'string' && value['nil'] != 'true'
193
+ ""
194
+ # blank or nil parsed values are represented by nil
195
+ elsif value.blank? || value['nil'] == 'true'
196
+ nil
197
+ # If the type is the only element which makes it then
198
+ # this still makes the value nil
199
+ elsif value['type'] && value.size == 1
200
+ nil
201
+ else
202
+ xml_value = value.inject({}) do |h,(k,v)|
203
+ h[k] = typecast_xml_value(v)
204
+ h
205
+ end
206
+
207
+ # Turn { :files => { :file => #<StringIO> } into { :files => #<StringIO> } so it is compatible with
208
+ # how multipart uploaded files from HTML appear
209
+ xml_value["file"].is_a?(StringIO) ? xml_value["file"] : xml_value
210
+ end
211
+ when 'Array'
212
+ value.map! { |i| typecast_xml_value(i) }
213
+ case value.length
214
+ when 0 then nil
215
+ when 1 then value.first
216
+ else value
217
+ end
218
+ when 'String'
219
+ value
220
+ else
221
+ raise "can't typecast #{value.class.name} - #{value.inspect}"
222
+ end
223
+ end
224
+
225
+ def undasherize_keys(params)
226
+ case params.class.to_s
227
+ when "Hash"
228
+ params.inject({}) do |h,(k,v)|
229
+ h[k.to_s.tr("-", "_")] = undasherize_keys(v)
230
+ h
231
+ end
232
+ when "Array"
233
+ params.map { |v| undasherize_keys(v) }
234
+ else
235
+ params
236
+ end
237
+ end
238
+ end
239
+ end
240
+ end
241
+ end
242
+ end