ghost_dm-core 1.3.0.beta

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 (254) hide show
  1. data/.autotest +29 -0
  2. data/.document +5 -0
  3. data/.gitignore +35 -0
  4. data/.yardopts +1 -0
  5. data/Gemfile +65 -0
  6. data/LICENSE +20 -0
  7. data/README.md +269 -0
  8. data/Rakefile +4 -0
  9. data/dm-core.gemspec +24 -0
  10. data/lib/dm-core.rb +292 -0
  11. data/lib/dm-core/adapters.rb +222 -0
  12. data/lib/dm-core/adapters/abstract_adapter.rb +237 -0
  13. data/lib/dm-core/adapters/in_memory_adapter.rb +113 -0
  14. data/lib/dm-core/associations/many_to_many.rb +499 -0
  15. data/lib/dm-core/associations/many_to_one.rb +290 -0
  16. data/lib/dm-core/associations/one_to_many.rb +348 -0
  17. data/lib/dm-core/associations/one_to_one.rb +86 -0
  18. data/lib/dm-core/associations/relationship.rb +663 -0
  19. data/lib/dm-core/backwards.rb +13 -0
  20. data/lib/dm-core/collection.rb +1515 -0
  21. data/lib/dm-core/core_ext/kernel.rb +23 -0
  22. data/lib/dm-core/core_ext/pathname.rb +6 -0
  23. data/lib/dm-core/core_ext/symbol.rb +10 -0
  24. data/lib/dm-core/identity_map.rb +7 -0
  25. data/lib/dm-core/model.rb +874 -0
  26. data/lib/dm-core/model/hook.rb +103 -0
  27. data/lib/dm-core/model/is.rb +32 -0
  28. data/lib/dm-core/model/property.rb +249 -0
  29. data/lib/dm-core/model/relationship.rb +378 -0
  30. data/lib/dm-core/model/scope.rb +89 -0
  31. data/lib/dm-core/property.rb +866 -0
  32. data/lib/dm-core/property/binary.rb +21 -0
  33. data/lib/dm-core/property/boolean.rb +20 -0
  34. data/lib/dm-core/property/class.rb +17 -0
  35. data/lib/dm-core/property/date.rb +10 -0
  36. data/lib/dm-core/property/date_time.rb +10 -0
  37. data/lib/dm-core/property/decimal.rb +36 -0
  38. data/lib/dm-core/property/discriminator.rb +44 -0
  39. data/lib/dm-core/property/float.rb +16 -0
  40. data/lib/dm-core/property/integer.rb +22 -0
  41. data/lib/dm-core/property/invalid_value_error.rb +22 -0
  42. data/lib/dm-core/property/lookup.rb +27 -0
  43. data/lib/dm-core/property/numeric.rb +38 -0
  44. data/lib/dm-core/property/object.rb +34 -0
  45. data/lib/dm-core/property/serial.rb +14 -0
  46. data/lib/dm-core/property/string.rb +38 -0
  47. data/lib/dm-core/property/text.rb +9 -0
  48. data/lib/dm-core/property/time.rb +10 -0
  49. data/lib/dm-core/property_set.rb +177 -0
  50. data/lib/dm-core/query.rb +1366 -0
  51. data/lib/dm-core/query/conditions/comparison.rb +911 -0
  52. data/lib/dm-core/query/conditions/operation.rb +721 -0
  53. data/lib/dm-core/query/direction.rb +36 -0
  54. data/lib/dm-core/query/operator.rb +35 -0
  55. data/lib/dm-core/query/path.rb +114 -0
  56. data/lib/dm-core/query/sort.rb +39 -0
  57. data/lib/dm-core/relationship_set.rb +72 -0
  58. data/lib/dm-core/repository.rb +226 -0
  59. data/lib/dm-core/resource.rb +1214 -0
  60. data/lib/dm-core/resource/persistence_state.rb +75 -0
  61. data/lib/dm-core/resource/persistence_state/clean.rb +40 -0
  62. data/lib/dm-core/resource/persistence_state/deleted.rb +30 -0
  63. data/lib/dm-core/resource/persistence_state/dirty.rb +96 -0
  64. data/lib/dm-core/resource/persistence_state/immutable.rb +34 -0
  65. data/lib/dm-core/resource/persistence_state/persisted.rb +29 -0
  66. data/lib/dm-core/resource/persistence_state/transient.rb +80 -0
  67. data/lib/dm-core/spec/lib/adapter_helpers.rb +64 -0
  68. data/lib/dm-core/spec/lib/collection_helpers.rb +21 -0
  69. data/lib/dm-core/spec/lib/counter_adapter.rb +38 -0
  70. data/lib/dm-core/spec/lib/pending_helpers.rb +50 -0
  71. data/lib/dm-core/spec/lib/spec_helper.rb +74 -0
  72. data/lib/dm-core/spec/setup.rb +174 -0
  73. data/lib/dm-core/spec/shared/adapter_spec.rb +341 -0
  74. data/lib/dm-core/spec/shared/public/property_spec.rb +229 -0
  75. data/lib/dm-core/spec/shared/resource_spec.rb +1232 -0
  76. data/lib/dm-core/spec/shared/sel_spec.rb +111 -0
  77. data/lib/dm-core/spec/shared/semipublic/property_spec.rb +176 -0
  78. data/lib/dm-core/spec/shared/semipublic/query/conditions/abstract_comparison_spec.rb +261 -0
  79. data/lib/dm-core/support/assertions.rb +8 -0
  80. data/lib/dm-core/support/chainable.rb +18 -0
  81. data/lib/dm-core/support/deprecate.rb +12 -0
  82. data/lib/dm-core/support/descendant_set.rb +89 -0
  83. data/lib/dm-core/support/equalizer.rb +48 -0
  84. data/lib/dm-core/support/ext/array.rb +22 -0
  85. data/lib/dm-core/support/ext/blank.rb +25 -0
  86. data/lib/dm-core/support/ext/hash.rb +67 -0
  87. data/lib/dm-core/support/ext/module.rb +47 -0
  88. data/lib/dm-core/support/ext/object.rb +57 -0
  89. data/lib/dm-core/support/ext/string.rb +24 -0
  90. data/lib/dm-core/support/ext/try_dup.rb +12 -0
  91. data/lib/dm-core/support/hook.rb +405 -0
  92. data/lib/dm-core/support/inflections.rb +60 -0
  93. data/lib/dm-core/support/inflector/inflections.rb +211 -0
  94. data/lib/dm-core/support/inflector/methods.rb +151 -0
  95. data/lib/dm-core/support/lazy_array.rb +451 -0
  96. data/lib/dm-core/support/local_object_space.rb +13 -0
  97. data/lib/dm-core/support/logger.rb +201 -0
  98. data/lib/dm-core/support/mash.rb +176 -0
  99. data/lib/dm-core/support/naming_conventions.rb +90 -0
  100. data/lib/dm-core/support/ordered_set.rb +380 -0
  101. data/lib/dm-core/support/subject.rb +33 -0
  102. data/lib/dm-core/support/subject_set.rb +250 -0
  103. data/lib/dm-core/version.rb +3 -0
  104. data/script/performance.rb +275 -0
  105. data/script/profile.rb +218 -0
  106. data/spec/lib/rspec_immediate_feedback_formatter.rb +54 -0
  107. data/spec/public/associations/many_to_many/read_multiple_join_spec.rb +68 -0
  108. data/spec/public/associations/many_to_many_spec.rb +197 -0
  109. data/spec/public/associations/many_to_one_spec.rb +83 -0
  110. data/spec/public/associations/many_to_one_with_boolean_cpk_spec.rb +40 -0
  111. data/spec/public/associations/many_to_one_with_custom_fk_spec.rb +49 -0
  112. data/spec/public/associations/one_to_many_spec.rb +81 -0
  113. data/spec/public/associations/one_to_one_spec.rb +176 -0
  114. data/spec/public/associations/one_to_one_with_boolean_cpk_spec.rb +46 -0
  115. data/spec/public/collection_spec.rb +69 -0
  116. data/spec/public/finalize_spec.rb +76 -0
  117. data/spec/public/model/hook_spec.rb +246 -0
  118. data/spec/public/model/property_spec.rb +88 -0
  119. data/spec/public/model/relationship_spec.rb +1040 -0
  120. data/spec/public/model_spec.rb +462 -0
  121. data/spec/public/property/binary_spec.rb +41 -0
  122. data/spec/public/property/boolean_spec.rb +22 -0
  123. data/spec/public/property/class_spec.rb +28 -0
  124. data/spec/public/property/date_spec.rb +22 -0
  125. data/spec/public/property/date_time_spec.rb +22 -0
  126. data/spec/public/property/decimal_spec.rb +23 -0
  127. data/spec/public/property/discriminator_spec.rb +135 -0
  128. data/spec/public/property/float_spec.rb +22 -0
  129. data/spec/public/property/integer_spec.rb +22 -0
  130. data/spec/public/property/object_spec.rb +107 -0
  131. data/spec/public/property/serial_spec.rb +22 -0
  132. data/spec/public/property/string_spec.rb +22 -0
  133. data/spec/public/property/text_spec.rb +63 -0
  134. data/spec/public/property/time_spec.rb +22 -0
  135. data/spec/public/property_spec.rb +341 -0
  136. data/spec/public/resource_spec.rb +288 -0
  137. data/spec/public/sel_spec.rb +53 -0
  138. data/spec/public/setup_spec.rb +145 -0
  139. data/spec/public/shared/association_collection_shared_spec.rb +309 -0
  140. data/spec/public/shared/collection_finder_shared_spec.rb +267 -0
  141. data/spec/public/shared/collection_shared_spec.rb +1667 -0
  142. data/spec/public/shared/finder_shared_spec.rb +1629 -0
  143. data/spec/rcov.opts +6 -0
  144. data/spec/semipublic/adapters/abstract_adapter_spec.rb +30 -0
  145. data/spec/semipublic/adapters/in_memory_adapter_spec.rb +13 -0
  146. data/spec/semipublic/associations/many_to_many_spec.rb +94 -0
  147. data/spec/semipublic/associations/many_to_one_spec.rb +63 -0
  148. data/spec/semipublic/associations/one_to_many_spec.rb +55 -0
  149. data/spec/semipublic/associations/one_to_one_spec.rb +53 -0
  150. data/spec/semipublic/associations/relationship_spec.rb +200 -0
  151. data/spec/semipublic/associations_spec.rb +177 -0
  152. data/spec/semipublic/collection_spec.rb +110 -0
  153. data/spec/semipublic/model_spec.rb +96 -0
  154. data/spec/semipublic/property/binary_spec.rb +13 -0
  155. data/spec/semipublic/property/boolean_spec.rb +47 -0
  156. data/spec/semipublic/property/class_spec.rb +33 -0
  157. data/spec/semipublic/property/date_spec.rb +43 -0
  158. data/spec/semipublic/property/date_time_spec.rb +46 -0
  159. data/spec/semipublic/property/decimal_spec.rb +83 -0
  160. data/spec/semipublic/property/discriminator_spec.rb +19 -0
  161. data/spec/semipublic/property/float_spec.rb +82 -0
  162. data/spec/semipublic/property/integer_spec.rb +82 -0
  163. data/spec/semipublic/property/lookup_spec.rb +29 -0
  164. data/spec/semipublic/property/serial_spec.rb +13 -0
  165. data/spec/semipublic/property/string_spec.rb +13 -0
  166. data/spec/semipublic/property/text_spec.rb +31 -0
  167. data/spec/semipublic/property/time_spec.rb +50 -0
  168. data/spec/semipublic/property_spec.rb +114 -0
  169. data/spec/semipublic/query/conditions/comparison_spec.rb +1501 -0
  170. data/spec/semipublic/query/conditions/operation_spec.rb +1294 -0
  171. data/spec/semipublic/query/path_spec.rb +471 -0
  172. data/spec/semipublic/query_spec.rb +3682 -0
  173. data/spec/semipublic/resource/state/clean_spec.rb +88 -0
  174. data/spec/semipublic/resource/state/deleted_spec.rb +78 -0
  175. data/spec/semipublic/resource/state/dirty_spec.rb +162 -0
  176. data/spec/semipublic/resource/state/immutable_spec.rb +105 -0
  177. data/spec/semipublic/resource/state/transient_spec.rb +162 -0
  178. data/spec/semipublic/resource/state_spec.rb +230 -0
  179. data/spec/semipublic/resource_spec.rb +23 -0
  180. data/spec/semipublic/shared/condition_shared_spec.rb +9 -0
  181. data/spec/semipublic/shared/resource_shared_spec.rb +199 -0
  182. data/spec/semipublic/shared/resource_state_shared_spec.rb +79 -0
  183. data/spec/semipublic/shared/subject_shared_spec.rb +79 -0
  184. data/spec/spec.opts +5 -0
  185. data/spec/spec_helper.rb +38 -0
  186. data/spec/support/core_ext/hash.rb +10 -0
  187. data/spec/support/core_ext/inheritable_attributes.rb +46 -0
  188. data/spec/support/properties/huge_integer.rb +17 -0
  189. data/spec/unit/array_spec.rb +23 -0
  190. data/spec/unit/blank_spec.rb +73 -0
  191. data/spec/unit/data_mapper/ordered_set/append_spec.rb +26 -0
  192. data/spec/unit/data_mapper/ordered_set/clear_spec.rb +24 -0
  193. data/spec/unit/data_mapper/ordered_set/delete_spec.rb +28 -0
  194. data/spec/unit/data_mapper/ordered_set/each_spec.rb +19 -0
  195. data/spec/unit/data_mapper/ordered_set/empty_spec.rb +20 -0
  196. data/spec/unit/data_mapper/ordered_set/entries_spec.rb +22 -0
  197. data/spec/unit/data_mapper/ordered_set/eql_spec.rb +51 -0
  198. data/spec/unit/data_mapper/ordered_set/equal_value_spec.rb +84 -0
  199. data/spec/unit/data_mapper/ordered_set/hash_spec.rb +12 -0
  200. data/spec/unit/data_mapper/ordered_set/include_spec.rb +23 -0
  201. data/spec/unit/data_mapper/ordered_set/index_spec.rb +28 -0
  202. data/spec/unit/data_mapper/ordered_set/initialize_spec.rb +32 -0
  203. data/spec/unit/data_mapper/ordered_set/merge_spec.rb +36 -0
  204. data/spec/unit/data_mapper/ordered_set/shared/append_spec.rb +24 -0
  205. data/spec/unit/data_mapper/ordered_set/shared/clear_spec.rb +9 -0
  206. data/spec/unit/data_mapper/ordered_set/shared/delete_spec.rb +25 -0
  207. data/spec/unit/data_mapper/ordered_set/shared/each_spec.rb +17 -0
  208. data/spec/unit/data_mapper/ordered_set/shared/empty_spec.rb +9 -0
  209. data/spec/unit/data_mapper/ordered_set/shared/entries_spec.rb +9 -0
  210. data/spec/unit/data_mapper/ordered_set/shared/include_spec.rb +9 -0
  211. data/spec/unit/data_mapper/ordered_set/shared/index_spec.rb +13 -0
  212. data/spec/unit/data_mapper/ordered_set/shared/initialize_spec.rb +28 -0
  213. data/spec/unit/data_mapper/ordered_set/shared/merge_spec.rb +28 -0
  214. data/spec/unit/data_mapper/ordered_set/shared/size_spec.rb +13 -0
  215. data/spec/unit/data_mapper/ordered_set/shared/to_ary_spec.rb +11 -0
  216. data/spec/unit/data_mapper/ordered_set/size_spec.rb +27 -0
  217. data/spec/unit/data_mapper/ordered_set/to_ary_spec.rb +23 -0
  218. data/spec/unit/data_mapper/subject_set/append_spec.rb +47 -0
  219. data/spec/unit/data_mapper/subject_set/clear_spec.rb +34 -0
  220. data/spec/unit/data_mapper/subject_set/delete_spec.rb +40 -0
  221. data/spec/unit/data_mapper/subject_set/each_spec.rb +30 -0
  222. data/spec/unit/data_mapper/subject_set/empty_spec.rb +31 -0
  223. data/spec/unit/data_mapper/subject_set/entries_spec.rb +31 -0
  224. data/spec/unit/data_mapper/subject_set/get_spec.rb +34 -0
  225. data/spec/unit/data_mapper/subject_set/include_spec.rb +32 -0
  226. data/spec/unit/data_mapper/subject_set/named_spec.rb +33 -0
  227. data/spec/unit/data_mapper/subject_set/shared/append_spec.rb +18 -0
  228. data/spec/unit/data_mapper/subject_set/shared/clear_spec.rb +9 -0
  229. data/spec/unit/data_mapper/subject_set/shared/delete_spec.rb +9 -0
  230. data/spec/unit/data_mapper/subject_set/shared/each_spec.rb +9 -0
  231. data/spec/unit/data_mapper/subject_set/shared/empty_spec.rb +9 -0
  232. data/spec/unit/data_mapper/subject_set/shared/entries_spec.rb +9 -0
  233. data/spec/unit/data_mapper/subject_set/shared/get_spec.rb +9 -0
  234. data/spec/unit/data_mapper/subject_set/shared/include_spec.rb +9 -0
  235. data/spec/unit/data_mapper/subject_set/shared/named_spec.rb +9 -0
  236. data/spec/unit/data_mapper/subject_set/shared/size_spec.rb +13 -0
  237. data/spec/unit/data_mapper/subject_set/shared/to_ary_spec.rb +9 -0
  238. data/spec/unit/data_mapper/subject_set/shared/values_at_spec.rb +44 -0
  239. data/spec/unit/data_mapper/subject_set/size_spec.rb +42 -0
  240. data/spec/unit/data_mapper/subject_set/to_ary_spec.rb +34 -0
  241. data/spec/unit/data_mapper/subject_set/values_at_spec.rb +57 -0
  242. data/spec/unit/hash_spec.rb +28 -0
  243. data/spec/unit/hook_spec.rb +1235 -0
  244. data/spec/unit/inflections_spec.rb +16 -0
  245. data/spec/unit/lazy_array_spec.rb +1949 -0
  246. data/spec/unit/mash_spec.rb +312 -0
  247. data/spec/unit/module_spec.rb +71 -0
  248. data/spec/unit/object_spec.rb +38 -0
  249. data/spec/unit/try_dup_spec.rb +46 -0
  250. data/tasks/ci.rake +1 -0
  251. data/tasks/spec.rake +38 -0
  252. data/tasks/yard.rake +9 -0
  253. data/tasks/yardstick.rake +19 -0
  254. metadata +365 -0
@@ -0,0 +1,8 @@
1
+ module DataMapper
2
+ module Assertions
3
+ def assert_kind_of(name, value, *klasses)
4
+ klasses.each { |k| return if value.kind_of?(k) }
5
+ raise ArgumentError, "+#{name}+ should be #{klasses.map { |k| k.name } * ' or '}, but was #{value.class.name}", caller(2)
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,18 @@
1
+ module DataMapper
2
+ module Chainable
3
+
4
+ # @api private
5
+ def chainable(&block)
6
+ mod = Module.new(&block)
7
+ include mod
8
+ mod
9
+ end
10
+
11
+ # @api private
12
+ def extendable(&block)
13
+ mod = Module.new(&block)
14
+ extend mod
15
+ mod
16
+ end
17
+ end # module Chainable
18
+ end # module DataMapper
@@ -0,0 +1,12 @@
1
+ module DataMapper
2
+ module Deprecate
3
+ def deprecate(old_method, new_method)
4
+ class_eval <<-RUBY, __FILE__, __LINE__ + 1
5
+ def #{old_method}(*args, &block)
6
+ warn "\#{self.class}##{old_method} is deprecated, use \#{self.class}##{new_method} instead (\#{caller.first})"
7
+ send(#{new_method.inspect}, *args, &block)
8
+ end
9
+ RUBY
10
+ end
11
+ end # module Deprecate
12
+ end # module DataMapper
@@ -0,0 +1,89 @@
1
+ module DataMapper
2
+ class DescendantSet
3
+ include Enumerable
4
+
5
+ # Initialize a DescendantSet instance
6
+ #
7
+ # @param [#to_ary] descendants
8
+ # initialize with the descendants
9
+ #
10
+ # @api private
11
+ def initialize(descendants = [])
12
+ @descendants = SubjectSet.new(descendants)
13
+ end
14
+
15
+ # Copy a DescendantSet instance
16
+ #
17
+ # @param [DescendantSet] original
18
+ # the original descendants
19
+ #
20
+ # @api private
21
+ def initialize_copy(original)
22
+ @descendants = @descendants.dup
23
+ end
24
+
25
+ # Add a descendant
26
+ #
27
+ # @param [Module] descendant
28
+ #
29
+ # @return [DescendantSet]
30
+ # self
31
+ #
32
+ # @api private
33
+ def <<(descendant)
34
+ @descendants << descendant
35
+ self
36
+ end
37
+
38
+ # Remove a descendant
39
+ #
40
+ # Also removes from all descendants
41
+ #
42
+ # @param [Module] descendant
43
+ #
44
+ # @return [DescendantSet]
45
+ # self
46
+ #
47
+ # @api private
48
+ def delete(descendant)
49
+ @descendants.delete(descendant)
50
+ each { |d| d.descendants.delete(descendant) }
51
+ end
52
+
53
+ # Iterate over each descendant
54
+ #
55
+ # @yield [descendant]
56
+ # @yieldparam [Module] descendant
57
+ #
58
+ # @return [DescendantSet]
59
+ # self
60
+ #
61
+ # @api private
62
+ def each
63
+ @descendants.each do |descendant|
64
+ yield descendant
65
+ descendant.descendants.each { |dd| yield dd }
66
+ end
67
+ self
68
+ end
69
+
70
+ # Test if there are any descendants
71
+ #
72
+ # @return [Boolean]
73
+ #
74
+ # @api private
75
+ def empty?
76
+ @descendants.empty?
77
+ end
78
+
79
+ # Removes all entries and returns self
80
+ #
81
+ # @return [DescendantSet] self
82
+ #
83
+ # @api private
84
+ def clear
85
+ @descendants.clear
86
+ end
87
+
88
+ end # class DescendantSet
89
+ end # module DataMapper
@@ -0,0 +1,48 @@
1
+ module DataMapper
2
+ module Equalizer
3
+ def equalize(*methods)
4
+ define_eql_method(methods)
5
+ define_equivalent_method(methods)
6
+ define_hash_method(methods)
7
+ end
8
+
9
+ private
10
+
11
+ def define_eql_method(methods)
12
+ class_eval <<-RUBY, __FILE__, __LINE__ + 1
13
+ def eql?(other)
14
+ return true if equal?(other)
15
+ instance_of?(other.class) &&
16
+ #{methods.map { |method| "#{method}.eql?(other.#{method})" }.join(' && ')}
17
+ end
18
+ RUBY
19
+ end
20
+
21
+ def define_equivalent_method(methods)
22
+ respond_to = []
23
+ equivalent = []
24
+
25
+ methods.each do |method|
26
+ respond_to << "other.respond_to?(#{method.inspect})"
27
+ equivalent << "#{method} == other.#{method}"
28
+ end
29
+
30
+ class_eval <<-RUBY, __FILE__, __LINE__ + 1
31
+ def ==(other)
32
+ return true if equal?(other)
33
+ return false unless kind_of?(other.class) || other.kind_of?(self.class)
34
+ #{respond_to.join(' && ')} &&
35
+ #{equivalent.join(' && ')}
36
+ end
37
+ RUBY
38
+ end
39
+
40
+ def define_hash_method(methods)
41
+ class_eval <<-RUBY, __FILE__, __LINE__ + 1
42
+ def hash
43
+ self.class.hash ^ #{methods.map { |method| "#{method}.hash" }.join(' ^ ')}
44
+ end
45
+ RUBY
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,22 @@
1
+ module DataMapper; module Ext
2
+ module Array
3
+ # Transforms an Array of key/value pairs into a {Mash}.
4
+ #
5
+ # This is a better idiom than using Mash[*array.flatten] in Ruby 1.8.6
6
+ # because it is not possible to limit the flattening to a single
7
+ # level.
8
+ #
9
+ # @param [Array] array
10
+ # The array of key/value pairs to transform.
11
+ #
12
+ # @return [Mash]
13
+ # A {Mash} where each entry in the Array is turned into a key/value.
14
+ #
15
+ # @api semipublic
16
+ def self.to_mash(array)
17
+ m = Mash.new
18
+ array.each { |k,v| m[k] = v }
19
+ m
20
+ end
21
+ end # class Array
22
+ end; end
@@ -0,0 +1,25 @@
1
+ module DataMapper
2
+ module Ext
3
+ # Determines whether the specified +value+ is blank.
4
+ #
5
+ # An object is blank if it's false, empty, or a whitespace string.
6
+ # For example, "", " ", +nil+, [], and {} are blank.
7
+ #
8
+ # @api semipublic
9
+ def self.blank?(value)
10
+ return value.blank? if value.respond_to?(:blank?)
11
+ case value
12
+ when ::NilClass, ::FalseClass
13
+ true
14
+ when ::TrueClass, ::Numeric
15
+ false
16
+ when ::Array, ::Hash
17
+ value.empty?
18
+ when ::String
19
+ value !~ /\S/
20
+ else
21
+ value.nil? || (value.respond_to?(:empty?) && value.empty?)
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,67 @@
1
+ module DataMapper; module Ext
2
+ module Hash
3
+ # Creates a hash with *only* the specified key/value pairs from +hash+.
4
+ #
5
+ # @param [Hash] hash The hash from which to pick the key/value pairs.
6
+ # @param [Array] *keys The hash keys to include.
7
+ #
8
+ # @return [Hash] A new hash with only the selected keys.
9
+ #
10
+ # @example
11
+ # hash = { :one => 1, :two => 2, :three => 3 }
12
+ # Ext::Hash.only(hash, :one, :two) # => { :one => 1, :two => 2 }
13
+ #
14
+ # @api semipublic
15
+ def self.only(hash, *keys)
16
+ h = {}
17
+ keys.each {|k| h[k] = hash[k] if hash.has_key?(k) }
18
+ h
19
+ end
20
+
21
+ # Returns a hash that includes everything but the given +keys+.
22
+ #
23
+ # @param [Hash] hash The hash from which to pick the key/value pairs.
24
+ # @param [Array] *keys The hash keys to exclude.
25
+ #
26
+ # @return [Hash] A new hash without the specified keys.
27
+ #
28
+ # @example
29
+ # hash = { :one => 1, :two => 2, :three => 3 }
30
+ # Ext::Hash.except(hash, :one, :two) # => { :three => 3 }
31
+ #
32
+ # @api semipublic
33
+ def self.except(hash, *keys)
34
+ self.except!(hash.dup, *keys)
35
+ end
36
+
37
+ # Removes the specified +keys+ from the given +hash+.
38
+ #
39
+ # @param [Hash] hash The hash to modify.
40
+ # @param [Array] *keys The hash keys to exclude.
41
+ #
42
+ # @return [Hash] +hash+
43
+ #
44
+ # @example
45
+ # hash = { :one => 1, :two => 2, :three => 3 }
46
+ # Ext::Hash.except!(hash, :one, :two)
47
+ # hash # => { :three => 3 }
48
+ #
49
+ # @api semipublic
50
+ def self.except!(hash, *keys)
51
+ keys.each { |key| hash.delete(key) }
52
+ hash
53
+ end
54
+
55
+ # Converts the specified +hash+ to a {Mash}.
56
+ #
57
+ # @param [Hash] hash The hash to convert.
58
+ # @return [Mash] The {Mash} for the specified +hash+.
59
+ #
60
+ # @api semipublic
61
+ def self.to_mash(hash)
62
+ h = Mash.new(hash)
63
+ h.default = hash.default
64
+ h
65
+ end
66
+ end
67
+ end; end
@@ -0,0 +1,47 @@
1
+ module DataMapper; module Ext
2
+ module Module
3
+
4
+ # @api semipublic
5
+ def self.find_const(mod, const_name)
6
+ if const_name[0..1] == '::'
7
+ DataMapper::Ext::Object.full_const_get(const_name[2..-1])
8
+ else
9
+ nested_const_lookup(mod, const_name)
10
+ end
11
+ end
12
+
13
+ private
14
+
15
+ # Doesn't do any caching since constants can change with remove_const
16
+ def self.nested_const_lookup(mod, const_name)
17
+ unless mod.equal?(::Object)
18
+ constants = []
19
+
20
+ mod.name.split('::').each do |part|
21
+ const = constants.last || ::Object
22
+ constants << const.const_get(part)
23
+ end
24
+
25
+ parts = const_name.split('::')
26
+
27
+ # from most to least specific constant, use each as a base and try
28
+ # to find a constant with the name const_name within them
29
+ constants.reverse_each do |const|
30
+ # return the nested constant if available
31
+ return const if parts.all? do |part|
32
+ const = if RUBY_VERSION >= '1.9.0'
33
+ const.const_defined?(part, false) ? const.const_get(part, false) : nil
34
+ else
35
+ const.const_defined?(part) ? const.const_get(part) : nil
36
+ end
37
+ end
38
+ end
39
+ end
40
+
41
+ # no relative constant found, fallback to an absolute lookup and
42
+ # use const_missing if not found
43
+ DataMapper::Ext::Object.full_const_get(const_name)
44
+ end
45
+
46
+ end
47
+ end; end
@@ -0,0 +1,57 @@
1
+ module DataMapper; module Ext
2
+ module Object
3
+ # Returns the value of the specified constant.
4
+ #
5
+ # @overload full_const_get(obj, name)
6
+ # Returns the value of the specified constant in +obj+.
7
+ # @param [Object] obj The root object used as origin.
8
+ # @param [String] name The name of the constant to get, e.g. "Merb::Router".
9
+ #
10
+ # @overload full_const_get(name)
11
+ # Returns the value of the fully-qualified constant.
12
+ # @param [String] name The name of the constant to get, e.g. "Merb::Router".
13
+ #
14
+ # @return [Object] The constant corresponding to +name+.
15
+ #
16
+ # @api semipublic
17
+ def self.full_const_get(obj, name = nil)
18
+ obj, name = ::Object, obj if name.nil?
19
+
20
+ list = name.split("::")
21
+ list.shift if DataMapper::Ext.blank?(list.first)
22
+ list.each do |x|
23
+ # This is required because const_get tries to look for constants in the
24
+ # ancestor chain, but we only want constants that are HERE
25
+ obj = obj.const_defined?(x) ? obj.const_get(x) : obj.const_missing(x)
26
+ end
27
+ obj
28
+ end
29
+
30
+ # Sets the specified constant to the given +value+.
31
+ #
32
+ # @overload full_const_set(obj, name)
33
+ # Sets the specified constant in +obj+ to the given +value+.
34
+ # @param [Object] obj The root object used as origin.
35
+ # @param [String] name The name of the constant to set, e.g. "Merb::Router".
36
+ # @param [Object] value The value to assign to the constant.
37
+ #
38
+ # @overload full_const_set(name)
39
+ # Sets the fully-qualified constant to the given +value+.
40
+ # @param [String] name The name of the constant to set, e.g. "Merb::Router".
41
+ # @param [Object] value The value to assign to the constant.
42
+ #
43
+ # @return [Object] The constant corresponding to +name+.
44
+ #
45
+ # @api semipublic
46
+ def self.full_const_set(obj, name, value = nil)
47
+ obj, name, value = ::Object, obj, name if value.nil?
48
+
49
+ list = name.split("::")
50
+ toplevel = DataMapper::Ext.blank?(list.first)
51
+ list.shift if toplevel
52
+ last = list.pop
53
+ obj = list.empty? ? ::Object : DataMapper::Ext::Object.full_const_get(list.join("::"))
54
+ obj.const_set(last, value) if obj && !obj.const_defined?(last)
55
+ end
56
+ end
57
+ end; end
@@ -0,0 +1,24 @@
1
+ module DataMapper; module Ext
2
+ module String
3
+ # Replace sequences of whitespace (including newlines) with either
4
+ # a single space or remove them entirely (according to param _spaced_).
5
+ #
6
+ # compress_lines(<<QUERY)
7
+ # SELECT name
8
+ # FROM users
9
+ # QUERY => "SELECT name FROM users"
10
+ #
11
+ # @param [String] string
12
+ # The input string.
13
+ #
14
+ # @param [TrueClass, FalseClass] spaced (default=true)
15
+ # Determines whether returned string has whitespace collapsed or removed.
16
+ #
17
+ # @return [String] The input string with whitespace (including newlines) replaced.
18
+ #
19
+ # @api semipublic
20
+ def self.compress_lines(string, spaced = true)
21
+ string.split($/).map { |line| line.strip }.join(spaced ? ' ' : '')
22
+ end
23
+ end
24
+ end; end
@@ -0,0 +1,12 @@
1
+ module DataMapper
2
+ module Ext
3
+ def self.try_dup(value)
4
+ case value
5
+ when ::TrueClass, ::FalseClass, ::NilClass, ::Module, ::Numeric, ::Symbol
6
+ value
7
+ else
8
+ value.dup
9
+ end
10
+ end
11
+ end
12
+ end