sbf-dm-core 1.3.0.beta

Sign up to get free protection for your applications and to get access to all the features.
Files changed (259) hide show
  1. checksums.yaml +7 -0
  2. data/.autotest +29 -0
  3. data/.document +5 -0
  4. data/.gitignore +44 -0
  5. data/.rspec +1 -0
  6. data/.rubocop.yml +468 -0
  7. data/.travis.yml +57 -0
  8. data/.yardopts +1 -0
  9. data/Gemfile +70 -0
  10. data/LICENSE +20 -0
  11. data/README.md +269 -0
  12. data/Rakefile +4 -0
  13. data/dm-core.gemspec +21 -0
  14. data/lib/dm-core/adapters/abstract_adapter.rb +233 -0
  15. data/lib/dm-core/adapters/in_memory_adapter.rb +110 -0
  16. data/lib/dm-core/adapters.rb +249 -0
  17. data/lib/dm-core/associations/many_to_many.rb +477 -0
  18. data/lib/dm-core/associations/many_to_one.rb +282 -0
  19. data/lib/dm-core/associations/one_to_many.rb +332 -0
  20. data/lib/dm-core/associations/one_to_one.rb +84 -0
  21. data/lib/dm-core/associations/relationship.rb +650 -0
  22. data/lib/dm-core/backwards.rb +11 -0
  23. data/lib/dm-core/collection.rb +1486 -0
  24. data/lib/dm-core/core_ext/kernel.rb +21 -0
  25. data/lib/dm-core/core_ext/pathname.rb +4 -0
  26. data/lib/dm-core/core_ext/symbol.rb +10 -0
  27. data/lib/dm-core/identity_map.rb +6 -0
  28. data/lib/dm-core/model/hook.rb +99 -0
  29. data/lib/dm-core/model/is.rb +30 -0
  30. data/lib/dm-core/model/property.rb +244 -0
  31. data/lib/dm-core/model/relationship.rb +366 -0
  32. data/lib/dm-core/model/scope.rb +87 -0
  33. data/lib/dm-core/model.rb +876 -0
  34. data/lib/dm-core/property/binary.rb +19 -0
  35. data/lib/dm-core/property/boolean.rb +35 -0
  36. data/lib/dm-core/property/class.rb +23 -0
  37. data/lib/dm-core/property/date.rb +45 -0
  38. data/lib/dm-core/property/date_time.rb +44 -0
  39. data/lib/dm-core/property/decimal.rb +47 -0
  40. data/lib/dm-core/property/discriminator.rb +40 -0
  41. data/lib/dm-core/property/float.rb +27 -0
  42. data/lib/dm-core/property/integer.rb +32 -0
  43. data/lib/dm-core/property/invalid_value_error.rb +17 -0
  44. data/lib/dm-core/property/lookup.rb +26 -0
  45. data/lib/dm-core/property/numeric.rb +35 -0
  46. data/lib/dm-core/property/object.rb +33 -0
  47. data/lib/dm-core/property/serial.rb +13 -0
  48. data/lib/dm-core/property/string.rb +47 -0
  49. data/lib/dm-core/property/text.rb +12 -0
  50. data/lib/dm-core/property/time.rb +46 -0
  51. data/lib/dm-core/property/typecast/numeric.rb +32 -0
  52. data/lib/dm-core/property/typecast/time.rb +33 -0
  53. data/lib/dm-core/property.rb +856 -0
  54. data/lib/dm-core/property_set.rb +177 -0
  55. data/lib/dm-core/query/conditions/comparison.rb +886 -0
  56. data/lib/dm-core/query/conditions/operation.rb +710 -0
  57. data/lib/dm-core/query/direction.rb +33 -0
  58. data/lib/dm-core/query/operator.rb +34 -0
  59. data/lib/dm-core/query/path.rb +113 -0
  60. data/lib/dm-core/query/sort.rb +38 -0
  61. data/lib/dm-core/query.rb +1352 -0
  62. data/lib/dm-core/relationship_set.rb +69 -0
  63. data/lib/dm-core/repository.rb +226 -0
  64. data/lib/dm-core/resource/persistence_state/clean.rb +36 -0
  65. data/lib/dm-core/resource/persistence_state/deleted.rb +26 -0
  66. data/lib/dm-core/resource/persistence_state/dirty.rb +91 -0
  67. data/lib/dm-core/resource/persistence_state/immutable.rb +32 -0
  68. data/lib/dm-core/resource/persistence_state/persisted.rb +25 -0
  69. data/lib/dm-core/resource/persistence_state/transient.rb +87 -0
  70. data/lib/dm-core/resource/persistence_state.rb +70 -0
  71. data/lib/dm-core/resource.rb +1220 -0
  72. data/lib/dm-core/spec/lib/adapter_helpers.rb +63 -0
  73. data/lib/dm-core/spec/lib/collection_helpers.rb +21 -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 +164 -0
  78. data/lib/dm-core/spec/shared/adapter_spec.rb +366 -0
  79. data/lib/dm-core/spec/shared/public/property_spec.rb +229 -0
  80. data/lib/dm-core/spec/shared/resource_spec.rb +1221 -0
  81. data/lib/dm-core/spec/shared/sel_spec.rb +111 -0
  82. data/lib/dm-core/spec/shared/semipublic/property_spec.rb +184 -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 +388 -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 +13 -0
  102. data/lib/dm-core/support/logger.rb +201 -0
  103. data/lib/dm-core/support/mash.rb +176 -0
  104. data/lib/dm-core/support/naming_conventions.rb +109 -0
  105. data/lib/dm-core/support/ordered_set.rb +381 -0
  106. data/lib/dm-core/support/subject.rb +33 -0
  107. data/lib/dm-core/support/subject_set.rb +251 -0
  108. data/lib/dm-core/version.rb +3 -0
  109. data/lib/dm-core.rb +274 -0
  110. data/script/performance.rb +275 -0
  111. data/script/profile.rb +218 -0
  112. data/spec/lib/rspec_immediate_feedback_formatter.rb +54 -0
  113. data/spec/public/associations/many_to_many/read_multiple_join_spec.rb +69 -0
  114. data/spec/public/associations/many_to_many_spec.rb +197 -0
  115. data/spec/public/associations/many_to_one_spec.rb +83 -0
  116. data/spec/public/associations/many_to_one_with_boolean_cpk_spec.rb +40 -0
  117. data/spec/public/associations/many_to_one_with_custom_fk_spec.rb +49 -0
  118. data/spec/public/associations/one_to_many_spec.rb +81 -0
  119. data/spec/public/associations/one_to_one_spec.rb +176 -0
  120. data/spec/public/associations/one_to_one_with_boolean_cpk_spec.rb +46 -0
  121. data/spec/public/collection_spec.rb +69 -0
  122. data/spec/public/finalize_spec.rb +77 -0
  123. data/spec/public/model/hook_spec.rb +245 -0
  124. data/spec/public/model/property_spec.rb +91 -0
  125. data/spec/public/model/relationship_spec.rb +1040 -0
  126. data/spec/public/model_spec.rb +456 -0
  127. data/spec/public/property/binary_spec.rb +43 -0
  128. data/spec/public/property/boolean_spec.rb +21 -0
  129. data/spec/public/property/class_spec.rb +27 -0
  130. data/spec/public/property/date_spec.rb +21 -0
  131. data/spec/public/property/date_time_spec.rb +21 -0
  132. data/spec/public/property/decimal_spec.rb +23 -0
  133. data/spec/public/property/discriminator_spec.rb +134 -0
  134. data/spec/public/property/float_spec.rb +22 -0
  135. data/spec/public/property/integer_spec.rb +22 -0
  136. data/spec/public/property/object_spec.rb +117 -0
  137. data/spec/public/property/serial_spec.rb +22 -0
  138. data/spec/public/property/string_spec.rb +21 -0
  139. data/spec/public/property/text_spec.rb +62 -0
  140. data/spec/public/property/time_spec.rb +21 -0
  141. data/spec/public/property_spec.rb +333 -0
  142. data/spec/public/resource/state_spec.rb +72 -0
  143. data/spec/public/resource_spec.rb +289 -0
  144. data/spec/public/sel_spec.rb +53 -0
  145. data/spec/public/setup_spec.rb +145 -0
  146. data/spec/public/shared/association_collection_shared_spec.rb +309 -0
  147. data/spec/public/shared/collection_finder_shared_spec.rb +267 -0
  148. data/spec/public/shared/collection_shared_spec.rb +1637 -0
  149. data/spec/public/shared/finder_shared_spec.rb +1647 -0
  150. data/spec/semipublic/adapters/abstract_adapter_spec.rb +30 -0
  151. data/spec/semipublic/adapters/in_memory_adapter_spec.rb +13 -0
  152. data/spec/semipublic/associations/many_to_many_spec.rb +94 -0
  153. data/spec/semipublic/associations/many_to_one_spec.rb +63 -0
  154. data/spec/semipublic/associations/one_to_many_spec.rb +55 -0
  155. data/spec/semipublic/associations/one_to_one_spec.rb +53 -0
  156. data/spec/semipublic/associations/relationship_spec.rb +200 -0
  157. data/spec/semipublic/associations_spec.rb +177 -0
  158. data/spec/semipublic/collection_spec.rb +110 -0
  159. data/spec/semipublic/model_spec.rb +96 -0
  160. data/spec/semipublic/property/binary_spec.rb +13 -0
  161. data/spec/semipublic/property/boolean_spec.rb +47 -0
  162. data/spec/semipublic/property/class_spec.rb +33 -0
  163. data/spec/semipublic/property/date_spec.rb +43 -0
  164. data/spec/semipublic/property/date_time_spec.rb +46 -0
  165. data/spec/semipublic/property/decimal_spec.rb +83 -0
  166. data/spec/semipublic/property/discriminator_spec.rb +19 -0
  167. data/spec/semipublic/property/float_spec.rb +82 -0
  168. data/spec/semipublic/property/integer_spec.rb +82 -0
  169. data/spec/semipublic/property/lookup_spec.rb +29 -0
  170. data/spec/semipublic/property/serial_spec.rb +13 -0
  171. data/spec/semipublic/property/string_spec.rb +13 -0
  172. data/spec/semipublic/property/text_spec.rb +31 -0
  173. data/spec/semipublic/property/time_spec.rb +50 -0
  174. data/spec/semipublic/property_spec.rb +114 -0
  175. data/spec/semipublic/query/conditions/comparison_spec.rb +1502 -0
  176. data/spec/semipublic/query/conditions/operation_spec.rb +1296 -0
  177. data/spec/semipublic/query/path_spec.rb +471 -0
  178. data/spec/semipublic/query_spec.rb +3665 -0
  179. data/spec/semipublic/resource/state/clean_spec.rb +89 -0
  180. data/spec/semipublic/resource/state/deleted_spec.rb +79 -0
  181. data/spec/semipublic/resource/state/dirty_spec.rb +163 -0
  182. data/spec/semipublic/resource/state/immutable_spec.rb +107 -0
  183. data/spec/semipublic/resource/state/transient_spec.rb +163 -0
  184. data/spec/semipublic/resource/state_spec.rb +230 -0
  185. data/spec/semipublic/resource_spec.rb +23 -0
  186. data/spec/semipublic/shared/condition_shared_spec.rb +9 -0
  187. data/spec/semipublic/shared/resource_shared_spec.rb +198 -0
  188. data/spec/semipublic/shared/resource_state_shared_spec.rb +91 -0
  189. data/spec/semipublic/shared/subject_shared_spec.rb +79 -0
  190. data/spec/spec_helper.rb +34 -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 +27 -0
  248. data/spec/unit/hook_spec.rb +1216 -0
  249. data/spec/unit/inflections_spec.rb +14 -0
  250. data/spec/unit/lazy_array_spec.rb +1949 -0
  251. data/spec/unit/mash_spec.rb +289 -0
  252. data/spec/unit/module_spec.rb +70 -0
  253. data/spec/unit/object_spec.rb +38 -0
  254. data/spec/unit/try_dup_spec.rb +46 -0
  255. data/tasks/ci.rake +1 -0
  256. data/tasks/spec.rake +18 -0
  257. data/tasks/yard.rake +9 -0
  258. data/tasks/yardstick.rake +19 -0
  259. metadata +323 -0
@@ -0,0 +1,451 @@
1
+ class LazyArray # borrowed partially from StrokeDB
2
+ include Enumerable
3
+
4
+ attr_reader :head, :tail
5
+
6
+ def first(*args)
7
+ if lazy_possible?(@head, *args)
8
+ @head.first(*args)
9
+ else
10
+ lazy_load
11
+ @array.first(*args)
12
+ end
13
+ end
14
+
15
+ def last(*args)
16
+ if lazy_possible?(@tail, *args)
17
+ @tail.last(*args)
18
+ else
19
+ lazy_load
20
+ @array.last(*args)
21
+ end
22
+ end
23
+
24
+ def at(index)
25
+ if index >= 0 && lazy_possible?(@head, index + 1)
26
+ @head.at(index)
27
+ elsif index < 0 && lazy_possible?(@tail, index.abs)
28
+ @tail.at(index)
29
+ else
30
+ lazy_load
31
+ @array.at(index)
32
+ end
33
+ end
34
+
35
+ def fetch(*args, &block)
36
+ index = args.first
37
+
38
+ if index >= 0 && lazy_possible?(@head, index + 1)
39
+ @head.fetch(*args, &block)
40
+ elsif index < 0 && lazy_possible?(@tail, index.abs)
41
+ @tail.fetch(*args, &block)
42
+ else
43
+ lazy_load
44
+ @array.fetch(*args, &block)
45
+ end
46
+ end
47
+
48
+ def values_at(*args)
49
+ accumulator = []
50
+
51
+ lazy_possible = args.all? do |arg|
52
+ index, length = extract_slice_arguments(arg)
53
+
54
+ if index >= 0 && lazy_possible?(@head, index + length)
55
+ accumulator.concat(head.values_at(*arg))
56
+ elsif index < 0 && lazy_possible?(@tail, index.abs)
57
+ accumulator.concat(tail.values_at(*arg))
58
+ end
59
+ end
60
+
61
+ if lazy_possible
62
+ accumulator
63
+ else
64
+ lazy_load
65
+ @array.values_at(*args)
66
+ end
67
+ end
68
+
69
+ def index(entry)
70
+ (lazy_possible?(@head) && @head.index(entry)) || begin
71
+ lazy_load
72
+ @array.index(entry)
73
+ end
74
+ end
75
+
76
+ def include?(entry)
77
+ (lazy_possible?(@tail) && @tail.include?(entry)) ||
78
+ (lazy_possible?(@head) && @head.include?(entry)) || begin
79
+ lazy_load
80
+ @array.include?(entry)
81
+ end
82
+ end
83
+
84
+ def empty?
85
+ (@tail.nil? || @tail.empty?) &&
86
+ (@head.nil? || @head.empty?) && begin
87
+ lazy_load
88
+ @array.empty?
89
+ end
90
+ end
91
+
92
+ def any?(&block)
93
+ (lazy_possible?(@tail) && @tail.any?(&block)) ||
94
+ (lazy_possible?(@head) && @head.any?(&block)) || begin
95
+ lazy_load
96
+ @array.any?(&block)
97
+ end
98
+ end
99
+
100
+ def [](*args)
101
+ index, length = extract_slice_arguments(*args)
102
+
103
+ if length == 1 && args.size == 1 && args.first.kind_of?(Integer)
104
+ return at(index)
105
+ end
106
+
107
+ if index >= 0 && lazy_possible?(@head, index + length)
108
+ @head[*args]
109
+ elsif index < 0 && lazy_possible?(@tail, index.abs - 1 + length)
110
+ @tail[*args]
111
+ else
112
+ lazy_load
113
+ @array[*args]
114
+ end
115
+ end
116
+
117
+ alias_method :slice, :[]
118
+
119
+ def slice!(*args)
120
+ index, length = extract_slice_arguments(*args)
121
+
122
+ if index >= 0 && lazy_possible?(@head, index + length)
123
+ @head.slice!(*args)
124
+ elsif index < 0 && lazy_possible?(@tail, index.abs - 1 + length)
125
+ @tail.slice!(*args)
126
+ else
127
+ lazy_load
128
+ @array.slice!(*args)
129
+ end
130
+ end
131
+
132
+ def []=(*args)
133
+ index, length = extract_slice_arguments(*args[0..-2])
134
+
135
+ if index >= 0 && lazy_possible?(@head, index + length)
136
+ @head.[]=(*args)
137
+ elsif index < 0 && lazy_possible?(@tail, index.abs - 1 + length)
138
+ @tail.[]=(*args)
139
+ else
140
+ lazy_load
141
+ @array.[]=(*args)
142
+ end
143
+ end
144
+
145
+ alias_method :splice, :[]=
146
+
147
+ def reverse
148
+ dup.reverse!
149
+ end
150
+
151
+ def reverse!
152
+ # reverse without kicking if possible
153
+ if loaded?
154
+ @array = @array.reverse
155
+ else
156
+ @head, @tail = @tail.reverse, @head.reverse
157
+
158
+ proc = @load_with_proc
159
+
160
+ @load_with_proc = lambda do |v|
161
+ proc.call(v)
162
+ v.instance_variable_get(:@array).reverse!
163
+ end
164
+ end
165
+
166
+ self
167
+ end
168
+
169
+ def <<(entry)
170
+ if loaded?
171
+ lazy_load
172
+ @array << entry
173
+ else
174
+ @tail << entry
175
+ end
176
+ self
177
+ end
178
+
179
+ def concat(other)
180
+ if loaded?
181
+ lazy_load
182
+ @array.concat(other)
183
+ else
184
+ @tail.concat(other)
185
+ end
186
+ self
187
+ end
188
+
189
+ def push(*entries)
190
+ if loaded?
191
+ lazy_load
192
+ @array.push(*entries)
193
+ else
194
+ @tail.push(*entries)
195
+ end
196
+ self
197
+ end
198
+
199
+ def unshift(*entries)
200
+ if loaded?
201
+ lazy_load
202
+ @array.unshift(*entries)
203
+ else
204
+ @head.unshift(*entries)
205
+ end
206
+ self
207
+ end
208
+
209
+ def insert(index, *entries)
210
+ if index >= 0 && lazy_possible?(@head, index)
211
+ @head.insert(index, *entries)
212
+ elsif index < 0 && lazy_possible?(@tail, index.abs - 1)
213
+ @tail.insert(index, *entries)
214
+ else
215
+ lazy_load
216
+ @array.insert(index, *entries)
217
+ end
218
+ self
219
+ end
220
+
221
+ def pop(*args)
222
+ if lazy_possible?(@tail, *args)
223
+ @tail.pop(*args)
224
+ else
225
+ lazy_load
226
+ @array.pop(*args)
227
+ end
228
+ end
229
+
230
+ def shift(*args)
231
+ if lazy_possible?(@head, *args)
232
+ @head.shift(*args)
233
+ else
234
+ lazy_load
235
+ @array.shift(*args)
236
+ end
237
+ end
238
+
239
+ def delete_at(index)
240
+ if index >= 0 && lazy_possible?(@head, index + 1)
241
+ @head.delete_at(index)
242
+ elsif index < 0 && lazy_possible?(@tail, index.abs)
243
+ @tail.delete_at(index)
244
+ else
245
+ lazy_load
246
+ @array.delete_at(index)
247
+ end
248
+ end
249
+
250
+ def delete_if(&block)
251
+ if loaded?
252
+ lazy_load
253
+ @array.delete_if(&block)
254
+ else
255
+ @reapers << block
256
+ @head.delete_if(&block)
257
+ @tail.delete_if(&block)
258
+ end
259
+ self
260
+ end
261
+
262
+ def replace(other)
263
+ mark_loaded
264
+ @array.replace(other)
265
+ self
266
+ end
267
+
268
+ def clear
269
+ mark_loaded
270
+ @array.clear
271
+ self
272
+ end
273
+
274
+ def to_a
275
+ lazy_load
276
+ @array.to_a
277
+ end
278
+
279
+ alias_method :to_ary, :to_a
280
+
281
+ def load_with(&block)
282
+ @load_with_proc = block
283
+ self
284
+ end
285
+
286
+ def loaded?
287
+ @loaded == true
288
+ end
289
+
290
+ def kind_of?(klass)
291
+ super || @array.kind_of?(klass)
292
+ end
293
+
294
+ alias_method :is_a?, :kind_of?
295
+
296
+ def respond_to?(method, include_private = false)
297
+ super || @array.respond_to?(method)
298
+ end
299
+
300
+ def freeze
301
+ if loaded?
302
+ @array.freeze
303
+ else
304
+ @head.freeze
305
+ @tail.freeze
306
+ end
307
+ @frozen = true
308
+ self
309
+ end
310
+
311
+ def frozen?
312
+ @frozen == true
313
+ end
314
+
315
+ def ==(other)
316
+ if equal?(other)
317
+ return true
318
+ end
319
+
320
+ unless other.respond_to?(:to_ary)
321
+ return false
322
+ end
323
+
324
+ # if necessary, convert to something that can be compared
325
+ other = other.to_ary unless other.respond_to?(:[])
326
+
327
+ cmp?(other, :==)
328
+ end
329
+
330
+ def eql?(other)
331
+ if equal?(other)
332
+ return true
333
+ end
334
+
335
+ unless other.class.equal?(self.class)
336
+ return false
337
+ end
338
+
339
+ cmp?(other, :eql?)
340
+ end
341
+
342
+ def lazy_possible?(list, need_length = 1)
343
+ !loaded? && need_length <= list.size
344
+ end
345
+
346
+ private
347
+
348
+ def initialize
349
+ @frozen = false
350
+ @loaded = false
351
+ @load_with_proc = lambda { |v| v }
352
+ @head = []
353
+ @tail = []
354
+ @array = []
355
+ @reapers = []
356
+ end
357
+
358
+ def initialize_copy(original)
359
+ @head = DataMapper::Ext.try_dup(@head)
360
+ @tail = DataMapper::Ext.try_dup(@tail)
361
+ @array = DataMapper::Ext.try_dup(@array)
362
+ end
363
+
364
+ def lazy_load
365
+ return if loaded?
366
+ mark_loaded
367
+ @load_with_proc[self]
368
+ @array.unshift(*@head)
369
+ @array.concat(@tail)
370
+ @head = @tail = nil
371
+ @reapers.each { |r| @array.delete_if(&r) } if @reapers
372
+ @array.freeze if frozen?
373
+ end
374
+
375
+ def mark_loaded
376
+ @loaded = true
377
+ end
378
+
379
+ ##
380
+ # Extract arguments for #slice an #slice! and return index and length
381
+ #
382
+ # @param [Integer, Array(Integer), Range] *args the index,
383
+ # index and length, or range indicating first and last position
384
+ #
385
+ # @return [Integer] the index
386
+ # @return [Integer,NilClass] the length, if any
387
+ #
388
+ # @api private
389
+ def extract_slice_arguments(*args)
390
+ first_arg, second_arg = args
391
+
392
+ if args.size == 2 && first_arg.kind_of?(Integer) && second_arg.kind_of?(Integer)
393
+ return first_arg, second_arg
394
+ elsif args.size == 1
395
+ if first_arg.kind_of?(Integer)
396
+ return first_arg, 1
397
+ elsif first_arg.kind_of?(Range)
398
+ index = first_arg.first
399
+ length = first_arg.last - index
400
+ length += 1 unless first_arg.exclude_end?
401
+ return index, length
402
+ end
403
+ end
404
+
405
+ raise ArgumentError, "arguments may be 1 or 2 Integers, or 1 Range object, was: #{args.inspect}", caller(1)
406
+ end
407
+
408
+ def each
409
+ lazy_load
410
+ if block_given?
411
+ @array.each { |entry| yield entry }
412
+ self
413
+ else
414
+ @array.each
415
+ end
416
+ end
417
+
418
+ # delegate any not-explicitly-handled methods to @array, if possible.
419
+ # this is handy for handling methods mixed-into Array like group_by
420
+ def method_missing(method, *args, &block)
421
+ if @array.respond_to?(method)
422
+ lazy_load
423
+ results = @array.send(method, *args, &block)
424
+ results.equal?(@array) ? self : results
425
+ else
426
+ super
427
+ end
428
+ end
429
+
430
+ def cmp?(other, operator)
431
+ unless loaded?
432
+ # compare the head against the beginning of other. start at index
433
+ # 0 and incrementally compare each entry. if other is a LazyArray
434
+ # this has a lesser likelyhood of triggering a lazy load
435
+ 0.upto(@head.size - 1) do |i|
436
+ return false unless @head[i].__send__(operator, other[i])
437
+ end
438
+
439
+ # compare the tail against the end of other. start at index
440
+ # -1 and decrementally compare each entry. if other is a LazyArray
441
+ # this has a lesser likelyhood of triggering a lazy load
442
+ -1.downto(@tail.size * -1) do |i|
443
+ return false unless @tail[i].__send__(operator, other[i])
444
+ end
445
+
446
+ lazy_load
447
+ end
448
+
449
+ @array.send(operator, other.to_ary)
450
+ end
451
+ end
@@ -0,0 +1,13 @@
1
+ module DataMapper
2
+ module LocalObjectSpace
3
+ def self.extended(klass)
4
+ (class << klass; self; end).send :attr_accessor, :hook_scopes
5
+ klass.hook_scopes = []
6
+ super
7
+ end
8
+
9
+ def object_by_id(object_id)
10
+ self.hook_scopes.detect { |object| object.object_id == object_id }
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,201 @@
1
+ # ==== Public DataMapper Logger API
2
+ #
3
+ # To replace an existing logger with a new one:
4
+ # DataMapper::Logger.set_log(log{String, IO},level{Symbol, String})
5
+ #
6
+ # Available logging levels are
7
+ # DataMapper::Logger::{ Fatal, Error, Warn, Info, Debug }
8
+ #
9
+ # Logging via:
10
+ # DataMapper.logger.fatal(message<String>,&block)
11
+ # DataMapper.logger.error(message<String>,&block)
12
+ # DataMapper.logger.warn(message<String>,&block)
13
+ # DataMapper.logger.info(message<String>,&block)
14
+ # DataMapper.logger.debug(message<String>,&block)
15
+ #
16
+ # Logging with autoflush:
17
+ # DataMapper.logger.fatal!(message<String>,&block)
18
+ # DataMapper.logger.error!(message<String>,&block)
19
+ # DataMapper.logger.warn!(message<String>,&block)
20
+ # DataMapper.logger.info!(message<String>,&block)
21
+ # DataMapper.logger.debug!(message<String>,&block)
22
+ #
23
+ # Flush the buffer to
24
+ # DataMapper.logger.flush
25
+ #
26
+ # Remove the current log object
27
+ # DataMapper.logger.close
28
+ #
29
+ # ==== Private DataMapper Logger API
30
+ #
31
+ # To initialize the logger you create a new object, proxies to set_log.
32
+ # DataMapper::Logger.new(log{String, IO},level{Symbol, String})
33
+ module DataMapper
34
+
35
+ class << self
36
+ attr_accessor :logger
37
+ end
38
+
39
+ class Logger
40
+
41
+ attr_accessor :level
42
+ attr_accessor :delimiter
43
+ attr_accessor :auto_flush
44
+ attr_reader :buffer
45
+ attr_reader :log
46
+ attr_reader :init_args
47
+
48
+ # ==== Notes
49
+ # Ruby (standard) logger levels:
50
+ # :fatal:: An unhandleable error that results in a program crash
51
+ # :error:: A handleable error condition
52
+ # :warn:: A warning
53
+ # :info:: generic (useful) information about system operation
54
+ # :debug:: low-level information for developers
55
+ Levels =
56
+ {
57
+ :fatal => 7,
58
+ :error => 6,
59
+ :warn => 4,
60
+ :info => 3,
61
+ :debug => 0
62
+ }
63
+
64
+ private
65
+
66
+ # Readies a log for writing.
67
+ #
68
+ # ==== Parameters
69
+ # log<IO, String>:: Either an IO object or a name of a logfile.
70
+ def initialize_log(log)
71
+ close if @log # be sure that we don't leave open files laying around.
72
+
73
+ if log.respond_to?(:write)
74
+ @log = log
75
+ elsif File.exist?(log)
76
+ @log = open(log, (File::WRONLY | File::APPEND))
77
+ @log.sync = true
78
+ else
79
+ FileUtils.mkdir_p(File.dirname(log)) unless File.directory?(File.dirname(log))
80
+ @log = open(log, (File::WRONLY | File::APPEND | File::CREAT))
81
+ @log.sync = true
82
+ @log.write("#{Time.now.httpdate} #{delimiter} info #{delimiter} Logfile created\n")
83
+ end
84
+ end
85
+
86
+ public
87
+
88
+ # To initialize the logger you create a new object, proxies to set_log.
89
+ #
90
+ # ==== Parameters
91
+ # *args:: Arguments to create the log from. See set_logs for specifics.
92
+ def initialize(*args)
93
+ @init_args = args
94
+ set_log(*args)
95
+ self.auto_flush = true
96
+ DataMapper.logger = self
97
+ end
98
+
99
+ # Replaces an existing logger with a new one.
100
+ #
101
+ # ==== Parameters
102
+ # log<IO, String>:: Either an IO object or a name of a logfile.
103
+ # log_level<~to_sym>::
104
+ # The log level from, e.g. :fatal or :info. Defaults to :error in the
105
+ # production environment and :debug otherwise.
106
+ # delimiter<String>::
107
+ # Delimiter to use between message sections. Defaults to " ~ ".
108
+ # auto_flush<Boolean>::
109
+ # Whether the log should automatically flush after new messages are
110
+ # added. Defaults to false.
111
+ def set_log(log, log_level = nil, delimiter = " ~ ", auto_flush = false)
112
+ if log_level && Levels[log_level.to_sym]
113
+ @level = Levels[log_level.to_sym]
114
+ else
115
+ @level = Levels[:debug]
116
+ end
117
+ @buffer = []
118
+ @delimiter = delimiter
119
+ @auto_flush = auto_flush
120
+
121
+ initialize_log(log)
122
+ end
123
+
124
+ # Flush the entire buffer to the log object.
125
+ def flush
126
+ return unless @buffer.size > 0
127
+ to_flush = @buffer
128
+ @buffer = []
129
+ @log.write(to_flush.join)
130
+ end
131
+
132
+ # Close and remove the current log object.
133
+ def close
134
+ flush
135
+ @log.close if @log.respond_to?(:close) && !@log.tty?
136
+ @log = nil
137
+ end
138
+
139
+ # Appends a message to the log. The methods yield to an optional block and
140
+ # the output of this block will be appended to the message.
141
+ #
142
+ # ==== Parameters
143
+ # string<String>:: The message to be logged. Defaults to nil.
144
+ #
145
+ # ==== Returns
146
+ # String:: The resulting message added to the log file.
147
+ def <<(string = nil)
148
+ message = ""
149
+ message << delimiter
150
+ message << string if string
151
+ message << "\n" unless message[-1] == ?\n
152
+ @buffer << message
153
+ flush if @auto_flush
154
+
155
+ message
156
+ end
157
+ alias_method :push, :<<
158
+
159
+ # Generate the logging methods for DataMapper.logger for each log level.
160
+ Levels.each_pair do |name, number|
161
+ class_eval <<-LEVELMETHODS, __FILE__, __LINE__
162
+
163
+ # Appends a message to the log if the log level is at least as high as
164
+ # the log level of the logger.
165
+ #
166
+ # ==== Parameters
167
+ # string<String>:: The message to be logged. Defaults to nil.
168
+ #
169
+ # ==== Returns
170
+ # self:: The logger object for chaining.
171
+ def #{name}(message = nil)
172
+ self << message if #{number} >= level
173
+ self
174
+ end
175
+
176
+ # Appends a message to the log if the log level is at least as high as
177
+ # the log level of the logger. The bang! version of the method also auto
178
+ # flushes the log buffer to disk.
179
+ #
180
+ # ==== Parameters
181
+ # string<String>:: The message to be logged. Defaults to nil.
182
+ #
183
+ # ==== Returns
184
+ # self:: The logger object for chaining.
185
+ def #{name}!(message = nil)
186
+ self << message if #{number} >= level
187
+ flush if #{number} >= level
188
+ self
189
+ end
190
+
191
+ # ==== Returns
192
+ # Boolean:: True if this level will be logged by this logger.
193
+ def #{name}?
194
+ #{number} >= level
195
+ end
196
+ LEVELMETHODS
197
+ end
198
+
199
+ end
200
+
201
+ end