ardm-core 1.2.1

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 (259) hide show
  1. checksums.yaml +7 -0
  2. data/.autotest +29 -0
  3. data/.document +5 -0
  4. data/.gitignore +35 -0
  5. data/.travis.yml +23 -0
  6. data/.yardopts +1 -0
  7. data/Gemfile +63 -0
  8. data/LICENSE +20 -0
  9. data/README.rdoc +237 -0
  10. data/Rakefile +4 -0
  11. data/VERSION +1 -0
  12. data/ardm-core.gemspec +25 -0
  13. data/lib/ardm-core.rb +1 -0
  14. data/lib/dm-core.rb +285 -0
  15. data/lib/dm-core/adapters.rb +222 -0
  16. data/lib/dm-core/adapters/abstract_adapter.rb +236 -0
  17. data/lib/dm-core/adapters/in_memory_adapter.rb +113 -0
  18. data/lib/dm-core/associations/many_to_many.rb +496 -0
  19. data/lib/dm-core/associations/many_to_one.rb +296 -0
  20. data/lib/dm-core/associations/one_to_many.rb +345 -0
  21. data/lib/dm-core/associations/one_to_one.rb +86 -0
  22. data/lib/dm-core/associations/relationship.rb +663 -0
  23. data/lib/dm-core/backwards.rb +13 -0
  24. data/lib/dm-core/collection.rb +1514 -0
  25. data/lib/dm-core/core_ext/kernel.rb +23 -0
  26. data/lib/dm-core/core_ext/pathname.rb +6 -0
  27. data/lib/dm-core/core_ext/symbol.rb +10 -0
  28. data/lib/dm-core/identity_map.rb +7 -0
  29. data/lib/dm-core/model.rb +869 -0
  30. data/lib/dm-core/model/hook.rb +102 -0
  31. data/lib/dm-core/model/is.rb +32 -0
  32. data/lib/dm-core/model/property.rb +253 -0
  33. data/lib/dm-core/model/relationship.rb +377 -0
  34. data/lib/dm-core/model/scope.rb +89 -0
  35. data/lib/dm-core/property.rb +839 -0
  36. data/lib/dm-core/property/binary.rb +22 -0
  37. data/lib/dm-core/property/boolean.rb +31 -0
  38. data/lib/dm-core/property/class.rb +24 -0
  39. data/lib/dm-core/property/date.rb +45 -0
  40. data/lib/dm-core/property/date_time.rb +44 -0
  41. data/lib/dm-core/property/decimal.rb +50 -0
  42. data/lib/dm-core/property/discriminator.rb +46 -0
  43. data/lib/dm-core/property/float.rb +28 -0
  44. data/lib/dm-core/property/integer.rb +32 -0
  45. data/lib/dm-core/property/lookup.rb +29 -0
  46. data/lib/dm-core/property/numeric.rb +40 -0
  47. data/lib/dm-core/property/object.rb +28 -0
  48. data/lib/dm-core/property/serial.rb +13 -0
  49. data/lib/dm-core/property/string.rb +50 -0
  50. data/lib/dm-core/property/text.rb +12 -0
  51. data/lib/dm-core/property/time.rb +46 -0
  52. data/lib/dm-core/property/typecast/numeric.rb +32 -0
  53. data/lib/dm-core/property/typecast/time.rb +33 -0
  54. data/lib/dm-core/property_set.rb +177 -0
  55. data/lib/dm-core/query.rb +1444 -0
  56. data/lib/dm-core/query/conditions/comparison.rb +910 -0
  57. data/lib/dm-core/query/conditions/operation.rb +720 -0
  58. data/lib/dm-core/query/direction.rb +36 -0
  59. data/lib/dm-core/query/operator.rb +35 -0
  60. data/lib/dm-core/query/path.rb +114 -0
  61. data/lib/dm-core/query/sort.rb +39 -0
  62. data/lib/dm-core/relationship_set.rb +72 -0
  63. data/lib/dm-core/repository.rb +226 -0
  64. data/lib/dm-core/resource.rb +1228 -0
  65. data/lib/dm-core/resource/persistence_state.rb +75 -0
  66. data/lib/dm-core/resource/persistence_state/clean.rb +40 -0
  67. data/lib/dm-core/resource/persistence_state/deleted.rb +30 -0
  68. data/lib/dm-core/resource/persistence_state/dirty.rb +96 -0
  69. data/lib/dm-core/resource/persistence_state/immutable.rb +34 -0
  70. data/lib/dm-core/resource/persistence_state/persisted.rb +29 -0
  71. data/lib/dm-core/resource/persistence_state/transient.rb +78 -0
  72. data/lib/dm-core/spec/lib/adapter_helpers.rb +54 -0
  73. data/lib/dm-core/spec/lib/collection_helpers.rb +20 -0
  74. data/lib/dm-core/spec/lib/counter_adapter.rb +38 -0
  75. data/lib/dm-core/spec/lib/pending_helpers.rb +50 -0
  76. data/lib/dm-core/spec/lib/spec_helper.rb +74 -0
  77. data/lib/dm-core/spec/setup.rb +173 -0
  78. data/lib/dm-core/spec/shared/adapter_spec.rb +326 -0
  79. data/lib/dm-core/spec/shared/public/property_spec.rb +229 -0
  80. data/lib/dm-core/spec/shared/resource_spec.rb +1236 -0
  81. data/lib/dm-core/spec/shared/sel_spec.rb +111 -0
  82. data/lib/dm-core/spec/shared/semipublic/property_spec.rb +134 -0
  83. data/lib/dm-core/spec/shared/semipublic/query/conditions/abstract_comparison_spec.rb +261 -0
  84. data/lib/dm-core/support/assertions.rb +8 -0
  85. data/lib/dm-core/support/chainable.rb +18 -0
  86. data/lib/dm-core/support/deprecate.rb +12 -0
  87. data/lib/dm-core/support/descendant_set.rb +89 -0
  88. data/lib/dm-core/support/equalizer.rb +48 -0
  89. data/lib/dm-core/support/ext/array.rb +22 -0
  90. data/lib/dm-core/support/ext/blank.rb +25 -0
  91. data/lib/dm-core/support/ext/hash.rb +67 -0
  92. data/lib/dm-core/support/ext/module.rb +47 -0
  93. data/lib/dm-core/support/ext/object.rb +57 -0
  94. data/lib/dm-core/support/ext/string.rb +24 -0
  95. data/lib/dm-core/support/ext/try_dup.rb +12 -0
  96. data/lib/dm-core/support/hook.rb +402 -0
  97. data/lib/dm-core/support/inflections.rb +60 -0
  98. data/lib/dm-core/support/inflector/inflections.rb +211 -0
  99. data/lib/dm-core/support/inflector/methods.rb +151 -0
  100. data/lib/dm-core/support/lazy_array.rb +451 -0
  101. data/lib/dm-core/support/local_object_space.rb +12 -0
  102. data/lib/dm-core/support/logger.rb +199 -0
  103. data/lib/dm-core/support/mash.rb +176 -0
  104. data/lib/dm-core/support/naming_conventions.rb +90 -0
  105. data/lib/dm-core/support/ordered_set.rb +380 -0
  106. data/lib/dm-core/support/subject.rb +33 -0
  107. data/lib/dm-core/support/subject_set.rb +250 -0
  108. data/lib/dm-core/version.rb +3 -0
  109. data/script/performance.rb +275 -0
  110. data/script/profile.rb +218 -0
  111. data/spec/lib/rspec_immediate_feedback_formatter.rb +54 -0
  112. data/spec/public/associations/many_to_many/read_multiple_join_spec.rb +68 -0
  113. data/spec/public/associations/many_to_many_spec.rb +197 -0
  114. data/spec/public/associations/many_to_one_spec.rb +83 -0
  115. data/spec/public/associations/many_to_one_with_boolean_cpk_spec.rb +40 -0
  116. data/spec/public/associations/many_to_one_with_custom_fk_spec.rb +49 -0
  117. data/spec/public/associations/one_to_many_spec.rb +81 -0
  118. data/spec/public/associations/one_to_one_spec.rb +176 -0
  119. data/spec/public/associations/one_to_one_with_boolean_cpk_spec.rb +46 -0
  120. data/spec/public/collection_spec.rb +69 -0
  121. data/spec/public/finalize_spec.rb +76 -0
  122. data/spec/public/model/hook_spec.rb +246 -0
  123. data/spec/public/model/property_spec.rb +88 -0
  124. data/spec/public/model/relationship_spec.rb +1040 -0
  125. data/spec/public/model_spec.rb +458 -0
  126. data/spec/public/property/binary_spec.rb +41 -0
  127. data/spec/public/property/boolean_spec.rb +22 -0
  128. data/spec/public/property/class_spec.rb +28 -0
  129. data/spec/public/property/date_spec.rb +22 -0
  130. data/spec/public/property/date_time_spec.rb +22 -0
  131. data/spec/public/property/decimal_spec.rb +23 -0
  132. data/spec/public/property/discriminator_spec.rb +135 -0
  133. data/spec/public/property/float_spec.rb +22 -0
  134. data/spec/public/property/integer_spec.rb +22 -0
  135. data/spec/public/property/object_spec.rb +107 -0
  136. data/spec/public/property/serial_spec.rb +22 -0
  137. data/spec/public/property/string_spec.rb +22 -0
  138. data/spec/public/property/text_spec.rb +63 -0
  139. data/spec/public/property/time_spec.rb +22 -0
  140. data/spec/public/property_spec.rb +341 -0
  141. data/spec/public/resource_spec.rb +284 -0
  142. data/spec/public/sel_spec.rb +53 -0
  143. data/spec/public/setup_spec.rb +145 -0
  144. data/spec/public/shared/association_collection_shared_spec.rb +309 -0
  145. data/spec/public/shared/collection_finder_shared_spec.rb +267 -0
  146. data/spec/public/shared/collection_shared_spec.rb +1669 -0
  147. data/spec/public/shared/finder_shared_spec.rb +1629 -0
  148. data/spec/rcov.opts +6 -0
  149. data/spec/semipublic/adapters/abstract_adapter_spec.rb +30 -0
  150. data/spec/semipublic/adapters/in_memory_adapter_spec.rb +12 -0
  151. data/spec/semipublic/associations/many_to_many_spec.rb +94 -0
  152. data/spec/semipublic/associations/many_to_one_spec.rb +63 -0
  153. data/spec/semipublic/associations/one_to_many_spec.rb +55 -0
  154. data/spec/semipublic/associations/one_to_one_spec.rb +53 -0
  155. data/spec/semipublic/associations/relationship_spec.rb +200 -0
  156. data/spec/semipublic/associations_spec.rb +177 -0
  157. data/spec/semipublic/collection_spec.rb +110 -0
  158. data/spec/semipublic/model_spec.rb +96 -0
  159. data/spec/semipublic/property/binary_spec.rb +13 -0
  160. data/spec/semipublic/property/boolean_spec.rb +47 -0
  161. data/spec/semipublic/property/class_spec.rb +33 -0
  162. data/spec/semipublic/property/date_spec.rb +43 -0
  163. data/spec/semipublic/property/date_time_spec.rb +46 -0
  164. data/spec/semipublic/property/decimal_spec.rb +83 -0
  165. data/spec/semipublic/property/discriminator_spec.rb +19 -0
  166. data/spec/semipublic/property/float_spec.rb +82 -0
  167. data/spec/semipublic/property/integer_spec.rb +82 -0
  168. data/spec/semipublic/property/lookup_spec.rb +29 -0
  169. data/spec/semipublic/property/serial_spec.rb +13 -0
  170. data/spec/semipublic/property/string_spec.rb +13 -0
  171. data/spec/semipublic/property/text_spec.rb +31 -0
  172. data/spec/semipublic/property/time_spec.rb +50 -0
  173. data/spec/semipublic/property_spec.rb +114 -0
  174. data/spec/semipublic/query/conditions/comparison_spec.rb +1501 -0
  175. data/spec/semipublic/query/conditions/operation_spec.rb +1294 -0
  176. data/spec/semipublic/query/path_spec.rb +471 -0
  177. data/spec/semipublic/query_spec.rb +3777 -0
  178. data/spec/semipublic/resource/state/clean_spec.rb +88 -0
  179. data/spec/semipublic/resource/state/deleted_spec.rb +78 -0
  180. data/spec/semipublic/resource/state/dirty_spec.rb +156 -0
  181. data/spec/semipublic/resource/state/immutable_spec.rb +105 -0
  182. data/spec/semipublic/resource/state/transient_spec.rb +162 -0
  183. data/spec/semipublic/resource/state_spec.rb +230 -0
  184. data/spec/semipublic/resource_spec.rb +23 -0
  185. data/spec/semipublic/shared/condition_shared_spec.rb +9 -0
  186. data/spec/semipublic/shared/resource_shared_spec.rb +199 -0
  187. data/spec/semipublic/shared/resource_state_shared_spec.rb +79 -0
  188. data/spec/semipublic/shared/subject_shared_spec.rb +79 -0
  189. data/spec/spec.opts +5 -0
  190. data/spec/spec_helper.rb +37 -0
  191. data/spec/support/core_ext/hash.rb +10 -0
  192. data/spec/support/core_ext/inheritable_attributes.rb +46 -0
  193. data/spec/support/properties/huge_integer.rb +17 -0
  194. data/spec/unit/array_spec.rb +23 -0
  195. data/spec/unit/blank_spec.rb +73 -0
  196. data/spec/unit/data_mapper/ordered_set/append_spec.rb +26 -0
  197. data/spec/unit/data_mapper/ordered_set/clear_spec.rb +24 -0
  198. data/spec/unit/data_mapper/ordered_set/delete_spec.rb +28 -0
  199. data/spec/unit/data_mapper/ordered_set/each_spec.rb +19 -0
  200. data/spec/unit/data_mapper/ordered_set/empty_spec.rb +20 -0
  201. data/spec/unit/data_mapper/ordered_set/entries_spec.rb +22 -0
  202. data/spec/unit/data_mapper/ordered_set/eql_spec.rb +51 -0
  203. data/spec/unit/data_mapper/ordered_set/equal_value_spec.rb +84 -0
  204. data/spec/unit/data_mapper/ordered_set/hash_spec.rb +12 -0
  205. data/spec/unit/data_mapper/ordered_set/include_spec.rb +23 -0
  206. data/spec/unit/data_mapper/ordered_set/index_spec.rb +28 -0
  207. data/spec/unit/data_mapper/ordered_set/initialize_spec.rb +32 -0
  208. data/spec/unit/data_mapper/ordered_set/merge_spec.rb +36 -0
  209. data/spec/unit/data_mapper/ordered_set/shared/append_spec.rb +24 -0
  210. data/spec/unit/data_mapper/ordered_set/shared/clear_spec.rb +9 -0
  211. data/spec/unit/data_mapper/ordered_set/shared/delete_spec.rb +25 -0
  212. data/spec/unit/data_mapper/ordered_set/shared/each_spec.rb +17 -0
  213. data/spec/unit/data_mapper/ordered_set/shared/empty_spec.rb +9 -0
  214. data/spec/unit/data_mapper/ordered_set/shared/entries_spec.rb +9 -0
  215. data/spec/unit/data_mapper/ordered_set/shared/include_spec.rb +9 -0
  216. data/spec/unit/data_mapper/ordered_set/shared/index_spec.rb +13 -0
  217. data/spec/unit/data_mapper/ordered_set/shared/initialize_spec.rb +28 -0
  218. data/spec/unit/data_mapper/ordered_set/shared/merge_spec.rb +28 -0
  219. data/spec/unit/data_mapper/ordered_set/shared/size_spec.rb +13 -0
  220. data/spec/unit/data_mapper/ordered_set/shared/to_ary_spec.rb +11 -0
  221. data/spec/unit/data_mapper/ordered_set/size_spec.rb +27 -0
  222. data/spec/unit/data_mapper/ordered_set/to_ary_spec.rb +23 -0
  223. data/spec/unit/data_mapper/subject_set/append_spec.rb +47 -0
  224. data/spec/unit/data_mapper/subject_set/clear_spec.rb +34 -0
  225. data/spec/unit/data_mapper/subject_set/delete_spec.rb +40 -0
  226. data/spec/unit/data_mapper/subject_set/each_spec.rb +30 -0
  227. data/spec/unit/data_mapper/subject_set/empty_spec.rb +31 -0
  228. data/spec/unit/data_mapper/subject_set/entries_spec.rb +31 -0
  229. data/spec/unit/data_mapper/subject_set/get_spec.rb +34 -0
  230. data/spec/unit/data_mapper/subject_set/include_spec.rb +32 -0
  231. data/spec/unit/data_mapper/subject_set/named_spec.rb +33 -0
  232. data/spec/unit/data_mapper/subject_set/shared/append_spec.rb +18 -0
  233. data/spec/unit/data_mapper/subject_set/shared/clear_spec.rb +9 -0
  234. data/spec/unit/data_mapper/subject_set/shared/delete_spec.rb +9 -0
  235. data/spec/unit/data_mapper/subject_set/shared/each_spec.rb +9 -0
  236. data/spec/unit/data_mapper/subject_set/shared/empty_spec.rb +9 -0
  237. data/spec/unit/data_mapper/subject_set/shared/entries_spec.rb +9 -0
  238. data/spec/unit/data_mapper/subject_set/shared/get_spec.rb +9 -0
  239. data/spec/unit/data_mapper/subject_set/shared/include_spec.rb +9 -0
  240. data/spec/unit/data_mapper/subject_set/shared/named_spec.rb +9 -0
  241. data/spec/unit/data_mapper/subject_set/shared/size_spec.rb +13 -0
  242. data/spec/unit/data_mapper/subject_set/shared/to_ary_spec.rb +9 -0
  243. data/spec/unit/data_mapper/subject_set/shared/values_at_spec.rb +44 -0
  244. data/spec/unit/data_mapper/subject_set/size_spec.rb +42 -0
  245. data/spec/unit/data_mapper/subject_set/to_ary_spec.rb +34 -0
  246. data/spec/unit/data_mapper/subject_set/values_at_spec.rb +57 -0
  247. data/spec/unit/hash_spec.rb +28 -0
  248. data/spec/unit/hook_spec.rb +1235 -0
  249. data/spec/unit/lazy_array_spec.rb +1949 -0
  250. data/spec/unit/mash_spec.rb +312 -0
  251. data/spec/unit/module_spec.rb +71 -0
  252. data/spec/unit/object_spec.rb +38 -0
  253. data/spec/unit/try_dup_spec.rb +46 -0
  254. data/tasks/ci.rake +1 -0
  255. data/tasks/db.rake +11 -0
  256. data/tasks/spec.rake +38 -0
  257. data/tasks/yard.rake +9 -0
  258. data/tasks/yardstick.rake +19 -0
  259. metadata +491 -0
data/lib/ardm-core.rb ADDED
@@ -0,0 +1 @@
1
+ require 'dm-core'
data/lib/dm-core.rb ADDED
@@ -0,0 +1,285 @@
1
+ require 'addressable/uri'
2
+ require 'bigdecimal'
3
+ require 'bigdecimal/util'
4
+ require 'date'
5
+ require 'pathname'
6
+ require 'set'
7
+ require 'time'
8
+ require 'yaml'
9
+
10
+ module DataMapper
11
+ module Undefined; end
12
+ end
13
+
14
+ require 'dm-core/support/ext/blank'
15
+ require 'dm-core/support/ext/hash'
16
+ require 'dm-core/support/ext/object'
17
+ require 'dm-core/support/ext/string'
18
+
19
+ begin
20
+ require 'fastthread'
21
+ rescue LoadError
22
+ # fastthread not installed
23
+ end
24
+
25
+ require 'dm-core/core_ext/pathname'
26
+ require 'dm-core/support/ext/module'
27
+ require 'dm-core/support/ext/array'
28
+ require 'dm-core/support/ext/try_dup'
29
+
30
+ require 'dm-core/support/mash'
31
+ require 'dm-core/support/inflector/inflections'
32
+ require 'dm-core/support/inflector/methods'
33
+ require 'dm-core/support/inflections'
34
+ require 'dm-core/support/chainable'
35
+ require 'dm-core/support/deprecate'
36
+ require 'dm-core/support/descendant_set'
37
+ require 'dm-core/support/equalizer'
38
+ require 'dm-core/support/assertions'
39
+ require 'dm-core/support/lazy_array'
40
+ require 'dm-core/support/local_object_space'
41
+ require 'dm-core/support/hook'
42
+ require 'dm-core/support/subject'
43
+ require 'dm-core/support/ordered_set'
44
+ require 'dm-core/support/subject_set'
45
+
46
+ require 'dm-core/query'
47
+ require 'dm-core/query/conditions/operation'
48
+ require 'dm-core/query/conditions/comparison'
49
+ require 'dm-core/query/operator'
50
+ require 'dm-core/query/direction'
51
+ require 'dm-core/query/path'
52
+ require 'dm-core/query/sort'
53
+
54
+ require 'dm-core/resource'
55
+ require 'dm-core/resource/persistence_state'
56
+ require 'dm-core/resource/persistence_state/transient'
57
+ require 'dm-core/resource/persistence_state/immutable'
58
+ require 'dm-core/resource/persistence_state/persisted'
59
+ require 'dm-core/resource/persistence_state/clean'
60
+ require 'dm-core/resource/persistence_state/deleted'
61
+ require 'dm-core/resource/persistence_state/dirty'
62
+
63
+ require 'dm-core/property'
64
+ require 'dm-core/property/typecast/numeric'
65
+ require 'dm-core/property/typecast/time'
66
+ require 'dm-core/property/object'
67
+ require 'dm-core/property/string'
68
+ require 'dm-core/property/binary'
69
+ require 'dm-core/property/text'
70
+ require 'dm-core/property/numeric'
71
+ require 'dm-core/property/float'
72
+ require 'dm-core/property/decimal'
73
+ require 'dm-core/property/boolean'
74
+ require 'dm-core/property/integer'
75
+ require 'dm-core/property/serial'
76
+ require 'dm-core/property/date'
77
+ require 'dm-core/property/date_time'
78
+ require 'dm-core/property/time'
79
+ require 'dm-core/property/class'
80
+ require 'dm-core/property/discriminator'
81
+ require 'dm-core/property/lookup'
82
+ require 'dm-core/property_set'
83
+
84
+ require 'dm-core/model'
85
+ require 'dm-core/model/hook'
86
+ require 'dm-core/model/is'
87
+ require 'dm-core/model/scope'
88
+ require 'dm-core/model/relationship'
89
+ require 'dm-core/model/property'
90
+
91
+ require 'dm-core/collection'
92
+ require 'dm-core/relationship_set'
93
+ require 'dm-core/associations/relationship'
94
+ require 'dm-core/associations/one_to_many'
95
+ require 'dm-core/associations/one_to_one'
96
+ require 'dm-core/associations/many_to_one'
97
+ require 'dm-core/associations/many_to_many'
98
+
99
+ require 'dm-core/identity_map'
100
+ require 'dm-core/repository'
101
+ require 'dm-core/adapters'
102
+ require 'dm-core/adapters/abstract_adapter'
103
+
104
+ require 'dm-core/support/logger'
105
+ require 'dm-core/support/naming_conventions'
106
+ require 'dm-core/version'
107
+
108
+ require 'dm-core/core_ext/kernel' # TODO: do not load automatically
109
+ require 'dm-core/core_ext/symbol' # TODO: do not load automatically
110
+
111
+ require 'dm-core/backwards' # TODO: do not load automatically
112
+
113
+ # A logger should always be present. Lets be consistent with DO
114
+ DataMapper::Logger.new(StringIO.new, :fatal)
115
+
116
+ unless defined?(Infinity)
117
+ Infinity = 1.0/0
118
+ end
119
+
120
+ # == Setup and Configuration
121
+ # DataMapper uses URIs or a connection hash to connect to your data-store.
122
+ # URI connections takes the form of:
123
+ # DataMapper.setup(:default, 'protocol://username:password@localhost:port/path/to/repo')
124
+ #
125
+ # Breaking this down, the first argument is the name you wish to give this
126
+ # connection. If you do not specify one, it will be assigned :default. If you
127
+ # would like to connect to more than one data-store, simply issue this command
128
+ # again, but with a different name specified.
129
+ #
130
+ # In order to issue ORM commands without specifying the repository context, you
131
+ # must define the :default database. Otherwise, you'll need to wrap your ORM
132
+ # calls in <tt>repository(:name) { }</tt>.
133
+ #
134
+ # Second, the URI breaks down into the access protocol, the username, the
135
+ # server, the password, and whatever path information is needed to properly
136
+ # address the data-store on the server.
137
+ #
138
+ # Here's some examples
139
+ # DataMapper.setup(:default, 'sqlite3://path/to/your/project/db/development.db')
140
+ # DataMapper.setup(:default, 'mysql://localhost/dm_core_test')
141
+ # # no auth-info
142
+ # DataMapper.setup(:default, 'postgres://root:supahsekret@127.0.0.1/dm_core_test')
143
+ # # with auth-info
144
+ #
145
+ #
146
+ # Alternatively, you can supply a hash as the second parameter, which would
147
+ # take the form:
148
+ #
149
+ # DataMapper.setup(:default, {
150
+ # :adapter => 'adapter_name_here',
151
+ # :database => 'path/to/repo',
152
+ # :username => 'username',
153
+ # :password => 'password',
154
+ # :host => 'hostname'
155
+ # })
156
+ #
157
+ # === Logging
158
+ # To turn on error logging to STDOUT, issue:
159
+ #
160
+ # DataMapper::Logger.new($stdout, :debug)
161
+ #
162
+ # You can pass a file location ("/path/to/log/file.log") in place of $stdout.
163
+ # see DataMapper::Logger for more information.
164
+ #
165
+ module DataMapper
166
+ extend DataMapper::Assertions
167
+
168
+ class RepositoryNotSetupError < StandardError; end
169
+
170
+ class IncompleteModelError < StandardError; end
171
+
172
+ class PluginNotFoundError < StandardError; end
173
+
174
+ class UnknownRelationshipError < StandardError; end
175
+
176
+ class ObjectNotFoundError < RuntimeError; end
177
+
178
+ class PersistenceError < RuntimeError; end
179
+
180
+ class UpdateConflictError < PersistenceError; end
181
+
182
+ class SaveFailureError < PersistenceError
183
+ attr_reader :resource
184
+
185
+ def initialize(message, resource)
186
+ super(message)
187
+ @resource = resource
188
+ end
189
+ end
190
+
191
+ class ImmutableError < RuntimeError; end
192
+
193
+ class ImmutableDeletedError < ImmutableError; end
194
+
195
+ # Raised on attempt to operate on collection of child objects
196
+ # when parent object is not yet saved.
197
+ # For instance, if your article object is not saved,
198
+ # but you try to fetch or scope down comments (1:n case), or
199
+ # publications (n:m case), operation cannot be completed
200
+ # because parent object's keys are not yet persisted,
201
+ # and thus there is no FK value to use in the query.
202
+ class UnsavedParentError < PersistenceError; end
203
+
204
+ # @api private
205
+ def self.root
206
+ @root ||= Pathname(__FILE__).dirname.parent.expand_path.freeze
207
+ end
208
+
209
+ # Setups up a connection to a data-store
210
+ #
211
+ # @param [Symbol] name
212
+ # a name for the context, defaults to :default
213
+ # @param [Hash(Symbol => String), Addressable::URI, String] uri_or_options
214
+ # connection information
215
+ #
216
+ # @return [DataMapper::Adapters::AbstractAdapter]
217
+ # the resulting setup adapter
218
+ #
219
+ # @raise [ArgumentError] "+name+ must be a Symbol, but was..."
220
+ # indicates that an invalid argument was passed for name[Symbol]
221
+ # @raise [ArgumentError] "+uri_or_options+ must be a Hash, URI or String, but was..."
222
+ # indicates that connection information could not be gleaned from
223
+ # the given uri_or_options[Hash, Addressable::URI, String]
224
+ #
225
+ # @api public
226
+ def self.setup(*args)
227
+ adapter = args.first
228
+
229
+ unless adapter.kind_of?(Adapters::AbstractAdapter)
230
+ adapter = Adapters.new(*args)
231
+ end
232
+
233
+ Repository.adapters[adapter.name] = adapter
234
+ end
235
+
236
+ # Block Syntax
237
+ # Pushes the named repository onto the context-stack,
238
+ # yields a new session, and pops the context-stack.
239
+ #
240
+ # Non-Block Syntax
241
+ # Returns the current session, or if there is none,
242
+ # a new Session.
243
+ #
244
+ # @param [Symbol] args the name of a repository to act within or return, :default is default
245
+ #
246
+ # @yield [Proc] (optional) block to execute within the context of the named repository
247
+ #
248
+ # @api public
249
+ def self.repository(name = nil)
250
+ context = Repository.context
251
+
252
+ current_repository = if name
253
+ name = name.to_sym
254
+ context.detect { |repository| repository.name == name }
255
+ else
256
+ name = Repository.default_name
257
+ context.last
258
+ end
259
+
260
+ current_repository ||= Repository.new(name)
261
+
262
+ if block_given?
263
+ current_repository.scope { |*block_args| yield(*block_args) }
264
+ else
265
+ current_repository
266
+ end
267
+ end
268
+
269
+ # Perform necessary steps to finalize DataMapper for the current repository
270
+ #
271
+ # This method should be called after loading all models and plugins.
272
+ #
273
+ # It ensures foreign key properties and anonymous join models are created.
274
+ # These are otherwise lazily declared, which can lead to unexpected errors.
275
+ # It also performs basic validity checking of the DataMapper models.
276
+ #
277
+ # @return [DataMapper] The DataMapper module
278
+ #
279
+ # @api public
280
+ def self.finalize
281
+ Model.descendants.each { |model| model.finalize }
282
+ self
283
+ end
284
+
285
+ end # module DataMapper
@@ -0,0 +1,222 @@
1
+ module DataMapper
2
+ module Adapters
3
+ extend Chainable
4
+ extend DataMapper::Assertions
5
+
6
+ # Set up an adapter for a storage engine
7
+ #
8
+ # @see DataMapper.setup
9
+ #
10
+ # @api private
11
+ def self.new(repository_name, options)
12
+ options = normalize_options(options)
13
+ adapter_class(options.fetch(:adapter)).new(repository_name, options)
14
+ end
15
+
16
+ # The path used to require the in memory adapter
17
+ #
18
+ # Set this if you want to register your own adapter
19
+ # to be used when you specify an 'in_memory' connection
20
+ # during
21
+ #
22
+ # @see DataMapper.setup
23
+ #
24
+ # @param [String] path
25
+ # the path used to require the desired in memory adapter
26
+ #
27
+ # @api semipublic
28
+ def self.in_memory_adapter_path=(path)
29
+ @in_memory_adapter_path = path
30
+ end
31
+
32
+ # The path used to require the in memory adapter
33
+ #
34
+ # @see DataMapper.setup
35
+ #
36
+ # @return [String]
37
+ # the path used to require the desired in memory adapter
38
+ #
39
+ # @api semipublic
40
+ def self.in_memory_adapter_path
41
+ @in_memory_adapter_path ||= 'dm-core/adapters/in_memory_adapter'
42
+ end
43
+
44
+ class << self
45
+ private
46
+
47
+ # Normalize the arguments passed to new()
48
+ #
49
+ # Turns options hash or connection URI into the options hash used
50
+ # by the adapter.
51
+ #
52
+ # @param [Hash, Addressable::URI, String] options
53
+ # the options to be normalized
54
+ #
55
+ # @return [Mash]
56
+ # the options normalized as a Mash
57
+ #
58
+ # @api private
59
+ def normalize_options(options)
60
+ case options
61
+ when Hash then normalize_options_hash(options)
62
+ when Addressable::URI then normalize_options_uri(options)
63
+ when String then normalize_options_string(options)
64
+ else
65
+ assert_kind_of 'options', options, Hash, Addressable::URI, String
66
+ end
67
+ end
68
+
69
+ # Normalize Hash options into a Mash
70
+ #
71
+ # @param [Hash] hash
72
+ # the hash to be normalized
73
+ #
74
+ # @return [Mash]
75
+ # the options normalized as a Mash
76
+ #
77
+ # @api private
78
+ def normalize_options_hash(hash)
79
+ DataMapper::Ext::Hash.to_mash(hash)
80
+ end
81
+
82
+ # Normalize Addressable::URI options into a Mash
83
+ #
84
+ # @param [Addressable::URI] uri
85
+ # the uri to be normalized
86
+ #
87
+ # @return [Mash]
88
+ # the options normalized as a Mash
89
+ #
90
+ # @api private
91
+ def normalize_options_uri(uri)
92
+ options = normalize_options_hash(uri.to_hash)
93
+
94
+ # Extract the name/value pairs from the query portion of the
95
+ # connection uri, and set them as options directly.
96
+ if options.fetch(:query)
97
+ options.update(uri.query_values)
98
+ end
99
+
100
+ options[:adapter] = options.fetch(:scheme)
101
+
102
+ options
103
+ end
104
+
105
+ # Normalize String options into a Mash
106
+ #
107
+ # @param [String] string
108
+ # the string to be normalized
109
+ #
110
+ # @return [Mash]
111
+ # the options normalized as a Mash
112
+ #
113
+ # @api private
114
+ def normalize_options_string(string)
115
+ normalize_options_uri(Addressable::URI.parse(string))
116
+ end
117
+
118
+ # Return the adapter class constant
119
+ #
120
+ # @example
121
+ # DataMapper::Adapters.send(:adapter_class, 'mysql') # => DataMapper::Adapters::MysqlAdapter
122
+ #
123
+ # @param [Symbol] name
124
+ # the name of the adapter
125
+ #
126
+ # @return [Class]
127
+ # the AbstractAdapter subclass
128
+ #
129
+ # @api private
130
+ def adapter_class(name)
131
+ adapter_name = normalize_adapter_name(name)
132
+ class_name = (DataMapper::Inflector.camelize(adapter_name) << 'Adapter').to_sym
133
+ load_adapter(adapter_name) unless const_defined?(class_name)
134
+ const_get(class_name)
135
+ end
136
+
137
+ # Return the name of the adapter
138
+ #
139
+ # @example
140
+ # DataMapper::Adapters.adapter_name('MysqlAdapter') # => 'mysql'
141
+ #
142
+ # @param [String] const_name
143
+ # the adapter constant name
144
+ #
145
+ # @return [String]
146
+ # the name of the adapter
147
+ #
148
+ # @api semipublic
149
+ def adapter_name(const_name)
150
+ const_name.to_s.chomp('Adapter').downcase
151
+ end
152
+
153
+ # Require the adapter library
154
+ #
155
+ # @param [String, Symbol] name
156
+ # the name of the adapter
157
+ #
158
+ # @return [Boolean]
159
+ # true if the adapter is loaded
160
+ #
161
+ # @api private
162
+ def load_adapter(name)
163
+ require "dm-#{name}-adapter"
164
+ rescue LoadError => original_error
165
+ begin
166
+ require in_memory_adapter?(name) ? in_memory_adapter_path : legacy_path(name)
167
+ rescue LoadError
168
+ raise original_error
169
+ end
170
+ end
171
+
172
+ # Returns wether or not the given adapter name is considered an in memory adapter
173
+ #
174
+ # @param [String, Symbol] name
175
+ # the name of the adapter
176
+ #
177
+ # @return [Boolean]
178
+ # true if the adapter is considered to be an in memory adapter
179
+ #
180
+ # @api private
181
+ def in_memory_adapter?(name)
182
+ name.to_s == 'in_memory'
183
+ end
184
+
185
+ # Returns the fallback filename that would be used to require the named adapter
186
+ #
187
+ # The fallback format is "#{name}_adapter" and will be phased out in favor of
188
+ # the properly 'namespaced' "dm-#{name}-adapter" format.
189
+ #
190
+ # @param [String, Symbol] name
191
+ # the name of the adapter to require
192
+ #
193
+ # @return [String]
194
+ # the filename that gets required for the adapter identified by name
195
+ #
196
+ # @api private
197
+ def legacy_path(name)
198
+ "#{name}_adapter"
199
+ end
200
+
201
+ # Adjust the adapter name to match the name used in the gem providing the adapter
202
+ #
203
+ # @param [String, Symbol] name
204
+ # the name of the adapter
205
+ #
206
+ # @return [String]
207
+ # the normalized adapter name
208
+ #
209
+ # @api private
210
+ def normalize_adapter_name(name)
211
+ (original = name.to_s) == 'sqlite3' ? 'sqlite' : original
212
+ end
213
+
214
+ end
215
+
216
+ extendable do
217
+ # @api private
218
+ def const_added(const_name)
219
+ end
220
+ end
221
+ end # module Adapters
222
+ end # module DataMapper