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,19 @@
1
+ module ActiveSupport #:nodoc:
2
+ module CoreExtensions #:nodoc:
3
+ module Hash #:nodoc:
4
+ module Diff
5
+ # Returns a hash that represents the difference between two hashes.
6
+ #
7
+ # Examples:
8
+ #
9
+ # {1 => 2}.diff(1 => 2) # => {}
10
+ # {1 => 2}.diff(1 => 3) # => {1 => 2}
11
+ # {}.diff(1 => 2) # => {1 => 2}
12
+ # {1 => 2, 3 => 4}.diff(1 => 2) # => {3 => 4}
13
+ def diff(h2)
14
+ self.dup.delete_if { |k, v| h2[k] == v }.merge(h2.dup.delete_if { |k, v| self.has_key?(k) })
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,24 @@
1
+ require 'set'
2
+
3
+ module ActiveSupport #:nodoc:
4
+ module CoreExtensions #:nodoc:
5
+ module Hash #:nodoc:
6
+ # Return a hash that includes everything but the given keys. This is useful for
7
+ # limiting a set of parameters to everything but a few known toggles:
8
+ #
9
+ # @person.update_attributes(params[:person].except(:admin))
10
+ module Except
11
+ # Returns a new hash without the given keys.
12
+ def except(*keys)
13
+ rejected = Set.new(respond_to?(:convert_key) ? keys.map { |key| convert_key(key) } : keys)
14
+ reject { |key,| rejected.include?(key) }
15
+ end
16
+
17
+ # Replaces the hash without only the given keys.
18
+ def except!(*keys)
19
+ replace(except(*keys))
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,102 @@
1
+ # This class has dubious semantics and we only have it so that
2
+ # people can write params[:key] instead of params['key']
3
+
4
+ class HashWithIndifferentAccess < Hash
5
+ def initialize(constructor = {})
6
+ if constructor.is_a?(Hash)
7
+ super()
8
+ update(constructor)
9
+ else
10
+ super(constructor)
11
+ end
12
+ end
13
+
14
+ def default(key = nil)
15
+ if key.is_a?(Symbol) && include?(key = key.to_s)
16
+ self[key]
17
+ else
18
+ super
19
+ end
20
+ end
21
+
22
+ alias_method :regular_writer, :[]= unless method_defined?(:regular_writer)
23
+ alias_method :regular_update, :update unless method_defined?(:regular_update)
24
+
25
+ def []=(key, value)
26
+ regular_writer(convert_key(key), convert_value(value))
27
+ end
28
+
29
+ def update(other_hash)
30
+ other_hash.each_pair { |key, value| regular_writer(convert_key(key), convert_value(value)) }
31
+ self
32
+ end
33
+
34
+ alias_method :merge!, :update
35
+
36
+ def key?(key)
37
+ super(convert_key(key))
38
+ end
39
+
40
+ alias_method :include?, :key?
41
+ alias_method :has_key?, :key?
42
+ alias_method :member?, :key?
43
+
44
+ def fetch(key, *extras)
45
+ super(convert_key(key), *extras)
46
+ end
47
+
48
+ def values_at(*indices)
49
+ indices.collect {|key| self[convert_key(key)]}
50
+ end
51
+
52
+ def dup
53
+ HashWithIndifferentAccess.new(self)
54
+ end
55
+
56
+ def merge(hash)
57
+ self.dup.update(hash)
58
+ end
59
+
60
+ def delete(key)
61
+ super(convert_key(key))
62
+ end
63
+
64
+ def stringify_keys!; self end
65
+ def symbolize_keys!; self end
66
+ def to_options!; self end
67
+
68
+ # Convert to a Hash with String keys.
69
+ def to_hash
70
+ Hash.new(default).merge(self)
71
+ end
72
+
73
+ protected
74
+ def convert_key(key)
75
+ key.kind_of?(Symbol) ? key.to_s : key
76
+ end
77
+
78
+ def convert_value(value)
79
+ case value
80
+ when Hash
81
+ value.with_indifferent_access
82
+ when Array
83
+ value.collect { |e| e.is_a?(Hash) ? e.with_indifferent_access : e }
84
+ else
85
+ value
86
+ end
87
+ end
88
+ end
89
+
90
+ module ActiveSupport #:nodoc:
91
+ module CoreExtensions #:nodoc:
92
+ module Hash #:nodoc:
93
+ module IndifferentAccess #:nodoc:
94
+ def with_indifferent_access
95
+ hash = HashWithIndifferentAccess.new(self)
96
+ hash.default = self.default
97
+ hash
98
+ end
99
+ end
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,54 @@
1
+ module ActiveSupport #:nodoc:
2
+ module CoreExtensions #:nodoc:
3
+ module Hash #:nodoc:
4
+ module Keys
5
+ # Return a new hash with all keys converted to strings.
6
+ def stringify_keys
7
+ inject({}) do |options, (key, value)|
8
+ options[key.to_s] = value
9
+ options
10
+ end
11
+ end
12
+
13
+ # Destructively convert all keys to strings.
14
+ def stringify_keys!
15
+ keys.each do |key|
16
+ unless key.class.to_s == "String" # weird hack to make the tests run when string_ext_test.rb is also running
17
+ self[key.to_s] = self[key]
18
+ delete(key)
19
+ end
20
+ end
21
+ self
22
+ end
23
+
24
+ # Return a new hash with all keys converted to symbols.
25
+ def symbolize_keys
26
+ inject({}) do |options, (key, value)|
27
+ options[key.to_sym || key] = value
28
+ options
29
+ end
30
+ end
31
+
32
+ # Destructively convert all keys to symbols.
33
+ def symbolize_keys!
34
+ self.replace(self.symbolize_keys)
35
+ end
36
+
37
+ alias_method :to_options, :symbolize_keys
38
+ alias_method :to_options!, :symbolize_keys!
39
+
40
+ # Validate all keys in a hash match *valid keys, raising ArgumentError on a mismatch.
41
+ # Note that keys are NOT treated indifferently, meaning if you use strings for keys but assert symbol
42
+ # as keys, this will fail.
43
+ # examples:
44
+ # { :name => "Rob", :years => "28" }.assert_valid_keys(:name, :age) # => raises "ArgumentError: Unknown key(s): years"
45
+ # { :name => "Rob", :age => "28" }.assert_valid_keys("name", "age") # => raises "ArgumentError: Unknown key(s): years, name"
46
+ # { :name => "Rob", :age => "28" }.assert_valid_keys(:name, :age) # => passes, raises nothing
47
+ def assert_valid_keys(*valid_keys)
48
+ unknown_keys = keys - [valid_keys].flatten
49
+ raise(ArgumentError, "Unknown key(s): #{unknown_keys.join(", ")}") unless unknown_keys.empty?
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,25 @@
1
+ module ActiveSupport #:nodoc:
2
+ module CoreExtensions #:nodoc:
3
+ module Hash #:nodoc:
4
+ # Allows for reverse merging where its the keys in the calling hash that wins over those in the <tt>other_hash</tt>.
5
+ # This is particularly useful for initializing an incoming option hash with default values:
6
+ #
7
+ # def setup(options = {})
8
+ # options.reverse_merge! :size => 25, :velocity => 10
9
+ # end
10
+ #
11
+ # The default :size and :velocity is only set if the +options+ passed in doesn't already have those keys set.
12
+ module ReverseMerge
13
+ def reverse_merge(other_hash)
14
+ other_hash.merge(self)
15
+ end
16
+
17
+ def reverse_merge!(other_hash)
18
+ replace(reverse_merge(other_hash))
19
+ end
20
+
21
+ alias_method :reverse_update, :reverse_merge!
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,28 @@
1
+ require 'set'
2
+
3
+ module ActiveSupport #:nodoc:
4
+ module CoreExtensions #:nodoc:
5
+ module Hash #:nodoc:
6
+ # Slice a hash to include only the given keys. This is useful for
7
+ # limiting an options hash to valid keys before passing to a method:
8
+ #
9
+ # def search(criteria = {})
10
+ # assert_valid_keys(:mass, :velocity, :time)
11
+ # end
12
+ #
13
+ # search(options.slice(:mass, :velocity, :time))
14
+ module Slice
15
+ # Returns a new hash with only the given keys.
16
+ def slice(*keys)
17
+ allowed = Set.new(respond_to?(:convert_key) ? keys.map { |key| convert_key(key) } : keys)
18
+ reject { |key,| !allowed.include?(key) }
19
+ end
20
+
21
+ # Replaces the hash with only the given keys.
22
+ def slice!(*keys)
23
+ replace(slice(*keys))
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,7 @@
1
+ require 'active_support/core_ext/integer/even_odd'
2
+ require 'active_support/core_ext/integer/inflections'
3
+
4
+ class Integer #:nodoc:
5
+ include ActiveSupport::CoreExtensions::Integer::EvenOdd
6
+ include ActiveSupport::CoreExtensions::Integer::Inflections
7
+ end
@@ -0,0 +1,24 @@
1
+ module ActiveSupport #:nodoc:
2
+ module CoreExtensions #:nodoc:
3
+ module Integer #:nodoc:
4
+ # For checking if a fixnum is even or odd.
5
+ # * 1.even? # => false
6
+ # * 1.odd? # => true
7
+ # * 2.even? # => true
8
+ # * 2.odd? # => false
9
+ module EvenOdd
10
+ def multiple_of?(number)
11
+ self % number == 0
12
+ end
13
+
14
+ def even?
15
+ multiple_of? 2
16
+ end if RUBY_VERSION < '1.9'
17
+
18
+ def odd?
19
+ !even?
20
+ end if RUBY_VERSION < '1.9'
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,21 @@
1
+ require 'active_support/inflector'
2
+
3
+ module ActiveSupport #:nodoc:
4
+ module CoreExtensions #:nodoc:
5
+ module Integer #:nodoc:
6
+ module Inflections
7
+ # Ordinalize turns a number into an ordinal string used to denote the
8
+ # position in an ordered sequence such as 1st, 2nd, 3rd, 4th.
9
+ #
10
+ # Examples
11
+ # 1.ordinalize # => "1st"
12
+ # 2.ordinalize # => "2nd"
13
+ # 1002.ordinalize # => "1002nd"
14
+ # 1003.ordinalize # => "1003rd"
15
+ def ordinalize
16
+ Inflector.ordinalize(self)
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,5 @@
1
+ require 'active_support/core_ext/kernel/daemonizing'
2
+ require 'active_support/core_ext/kernel/reporting'
3
+ require 'active_support/core_ext/kernel/agnostics'
4
+ require 'active_support/core_ext/kernel/requires'
5
+ require 'active_support/core_ext/kernel/debugger'
@@ -0,0 +1,11 @@
1
+ class Object
2
+ # Makes backticks behave (somewhat more) similarly on all platforms.
3
+ # On win32 `nonexistent_command` raises Errno::ENOENT; on Unix, the
4
+ # spawned shell prints a message to stderr and sets $?. We emulate
5
+ # Unix on the former but not the latter.
6
+ def `(command) #:nodoc:
7
+ super
8
+ rescue Errno::ENOENT => e
9
+ STDERR.puts "#$0: #{e}"
10
+ end
11
+ end
@@ -0,0 +1,15 @@
1
+ module Kernel
2
+ # Turns the current script into a daemon process that detaches from the console.
3
+ # It can be shut down with a TERM signal.
4
+ def daemonize
5
+ exit if fork # Parent exits, child continues.
6
+ Process.setsid # Become session leader.
7
+ exit if fork # Zap session leader. See [1].
8
+ Dir.chdir "/" # Release old working directory.
9
+ File.umask 0000 # Ensure sensible umask. Adjust as needed.
10
+ STDIN.reopen "/dev/null" # Free file descriptors and
11
+ STDOUT.reopen "/dev/null", "a" # point them somewhere sensible.
12
+ STDERR.reopen STDOUT # STDOUT/ERR should better go to a logfile.
13
+ trap("TERM") { exit }
14
+ end
15
+ end
@@ -0,0 +1,13 @@
1
+ module Kernel
2
+ unless respond_to?(:debugger)
3
+ # Starts a debugging session if ruby-debug has been loaded (call script/server --debugger to do load it).
4
+ def debugger
5
+ RAILS_DEFAULT_LOGGER.info "\n***** Debugger requested, but was not available: Start server with --debugger to enable *****\n"
6
+ end
7
+ end
8
+
9
+ def breakpoint
10
+ RAILS_DEFAULT_LOGGER.info "\n***** The 'breakpoint' command has been renamed 'debugger' -- please change *****\n"
11
+ debugger
12
+ end
13
+ end
@@ -0,0 +1,51 @@
1
+ module Kernel
2
+ # Sets $VERBOSE to nil for the duration of the block and back to its original value afterwards.
3
+ #
4
+ # silence_warnings do
5
+ # value = noisy_call # no warning voiced
6
+ # end
7
+ #
8
+ # noisy_call # warning voiced
9
+ def silence_warnings
10
+ old_verbose, $VERBOSE = $VERBOSE, nil
11
+ yield
12
+ ensure
13
+ $VERBOSE = old_verbose
14
+ end
15
+
16
+ # Sets $VERBOSE to true for the duration of the block and back to its original value afterwards.
17
+ def enable_warnings
18
+ old_verbose, $VERBOSE = $VERBOSE, true
19
+ yield
20
+ ensure
21
+ $VERBOSE = old_verbose
22
+ end
23
+
24
+ # For compatibility
25
+ def silence_stderr #:nodoc:
26
+ silence_stream(STDERR) { yield }
27
+ end
28
+
29
+ # Silences any stream for the duration of the block.
30
+ #
31
+ # silence_stream(STDOUT) do
32
+ # puts 'This will never be seen'
33
+ # end
34
+ #
35
+ # puts 'But this will'
36
+ def silence_stream(stream)
37
+ old_stream = stream.dup
38
+ stream.reopen(RUBY_PLATFORM =~ /mswin/ ? 'NUL:' : '/dev/null')
39
+ stream.sync = true
40
+ yield
41
+ ensure
42
+ stream.reopen(old_stream)
43
+ end
44
+
45
+ def suppress(*exception_classes)
46
+ begin yield
47
+ rescue Exception => e
48
+ raise unless exception_classes.any? { |cls| e.kind_of?(cls) }
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,24 @@
1
+ module Kernel
2
+ # Require a library with fallback to RubyGems. Warnings during library
3
+ # loading are silenced to increase signal/noise for application warnings.
4
+ def require_library_or_gem(library_name)
5
+ silence_warnings do
6
+ begin
7
+ require library_name
8
+ rescue LoadError => cannot_require
9
+ # 1. Requiring the module is unsuccessful, maybe it's a gem and nobody required rubygems yet. Try.
10
+ begin
11
+ require 'rubygems'
12
+ rescue LoadError => rubygems_not_installed
13
+ raise cannot_require
14
+ end
15
+ # 2. Rubygems is installed and loaded. Try to load the library again
16
+ begin
17
+ require library_name
18
+ rescue LoadError => gem_not_installed
19
+ raise cannot_require
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end