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,176 @@
1
+ module DataMapper
2
+ # This class has dubious semantics and we only have it so that people can write
3
+ # params[:key] instead of params['key'].
4
+ class Mash < Hash
5
+
6
+ # Initializes a new mash.
7
+ #
8
+ # @param [Hash, Object] constructor
9
+ # The default value for the mash. If +constructor+ is a Hash, a new mash
10
+ # will be created based on the keys of the hash and no default value will
11
+ # be set.
12
+ def initialize(constructor = {})
13
+ if constructor.is_a?(Hash)
14
+ super()
15
+ update(constructor)
16
+ else
17
+ super(constructor)
18
+ end
19
+ end
20
+
21
+ # Gets the default value for the mash.
22
+ #
23
+ # @param [Object] key
24
+ # The default value for the mash. If +key+ is a Symbol and it is a key in
25
+ # the mash, then the default value will be set to the value matching the
26
+ # key.
27
+ def default(key = nil)
28
+ if key.is_a?(Symbol) && include?(key = key.to_s)
29
+ self[key]
30
+ else
31
+ super
32
+ end
33
+ end
34
+
35
+ alias_method :regular_writer, :[]= unless method_defined?(:regular_writer)
36
+ alias_method :regular_update, :update unless method_defined?(:regular_update)
37
+
38
+ # Sets the +value+ associated with the specified +key+.
39
+ #
40
+ # @param [Object] key The key to set.
41
+ # @param [Object] value The value to set the key to.
42
+ def []=(key, value)
43
+ regular_writer(convert_key(key), convert_value(value))
44
+ end
45
+
46
+ # Updates the mash with the key/value pairs from the specified hash.
47
+ #
48
+ # @param [Hash] other_hash
49
+ # A hash to update values in the mash with. The keys and the values will be
50
+ # converted to Mash format.
51
+ #
52
+ # @return [Mash] The updated mash.
53
+ def update(other_hash)
54
+ other_hash.each_pair { |key, value| regular_writer(convert_key(key), convert_value(value)) }
55
+ self
56
+ end
57
+
58
+ alias_method :merge!, :update
59
+
60
+ # Determines whether the mash contains the specified +key+.
61
+ #
62
+ # @param [Object] key The key to check for.
63
+ # @return [Boolean] True if the key exists in the mash.
64
+ def key?(key)
65
+ super(convert_key(key))
66
+ end
67
+
68
+ alias_method :include?, :key?
69
+ alias_method :has_key?, :key?
70
+ alias_method :member?, :key?
71
+
72
+ # @param [Object] key The key to fetch.
73
+ # @param [Array] *extras Default value.
74
+ #
75
+ # @return [Object] The value at key or the default value.
76
+ def fetch(key, *extras)
77
+ super(convert_key(key), *extras)
78
+ end
79
+
80
+ # @param [Array] *indices
81
+ # The keys to retrieve values for.
82
+ #
83
+ # @return [Array] The values at each of the provided keys.
84
+ def values_at(*indices)
85
+ indices.collect {|key| self[convert_key(key)]}
86
+ end
87
+
88
+ # @param [Hash] hash The hash to merge with the mash.
89
+ #
90
+ # @return [Mash] A new mash with the hash values merged in.
91
+ def merge(hash)
92
+ self.dup.update(hash)
93
+ end
94
+
95
+ # @param [Object] key The key to delete from the mash.
96
+ def delete(key)
97
+ super(convert_key(key))
98
+ end
99
+
100
+ # Returns a mash that includes everything but the given +keys+.
101
+ #
102
+ # @param [Array<String, Symbol>] *keys The mash keys to exclude.
103
+ #
104
+ # @return [Mash] A new mash without the selected keys.
105
+ #
106
+ # @example
107
+ # { :one => 1, :two => 2, :three => 3 }.except(:one)
108
+ # #=> { "two" => 2, "three" => 3 }
109
+ def except(*keys)
110
+ self.dup.except!(*keys.map {|k| convert_key(k)})
111
+ end
112
+
113
+ # Removes the specified +keys+ from the mash.
114
+ #
115
+ # @param [Array] *keys The mash keys to exclude.
116
+ #
117
+ # @return [Hash] +hash+
118
+ #
119
+ # @example
120
+ # mash = { :one => 1, :two => 2, :three => 3 }
121
+ # mash.except!(:one, :two)
122
+ # mash # => { :three => 3 }
123
+ def except!(*keys)
124
+ keys.each { |key| delete(key) }
125
+ self
126
+ end
127
+
128
+ # Used to provide the same interface as Hash.
129
+ #
130
+ # @return [Mash] This mash unchanged.
131
+ def stringify_keys!; self end
132
+
133
+ # @return [Hash] The mash as a Hash with symbolized keys.
134
+ def symbolize_keys
135
+ h = Hash.new(default)
136
+ each { |key, val| h[key.to_sym] = val }
137
+ h
138
+ end
139
+
140
+ # @return [Hash] The mash as a Hash with string keys.
141
+ def to_hash
142
+ Hash.new(default).replace(self)
143
+ end
144
+
145
+ protected
146
+ # @param [Object] key The key to convert.
147
+ #
148
+ # @param [Object]
149
+ # The converted key. If the key was a symbol, it will be converted to a
150
+ # string.
151
+ #
152
+ # @api private
153
+ def convert_key(key)
154
+ key.kind_of?(Symbol) ? key.to_s : key
155
+ end
156
+
157
+ # @param [Object] value The value to convert.
158
+ #
159
+ # @return [Object]
160
+ # The converted value. A Hash or an Array of hashes, will be converted to
161
+ # their Mash equivalents.
162
+ #
163
+ # @api private
164
+ def convert_value(value)
165
+ if value.class == Hash
166
+ mash = Mash.new(value)
167
+ mash.default = value.default
168
+ mash
169
+ elsif value.is_a?(Array)
170
+ value.collect { |e| convert_value(e) }
171
+ else
172
+ value
173
+ end
174
+ end
175
+ end
176
+ end
@@ -0,0 +1,109 @@
1
+ module DataMapper
2
+
3
+ # Use these modules to establish naming conventions.
4
+ # The default is UnderscoredAndPluralized.
5
+ # You assign a naming convention like so:
6
+ #
7
+ # repository(:default).adapter.resource_naming_convention = NamingConventions::Resource::Underscored
8
+ #
9
+ # You can also easily assign a custom convention with a Proc:
10
+ #
11
+ # repository(:default).adapter.resource_naming_convention = lambda do |value|
12
+ # 'tbl' + value.camelize(true)
13
+ # end
14
+ #
15
+ # Or by simply defining your own module in NamingConventions that responds to
16
+ # ::call.
17
+ #
18
+ # NOTE: It's important to set the convention before accessing your models
19
+ # since the resource_names are cached after first accessed.
20
+ # DataMapper.setup(name, uri) returns the Adapter for convenience, so you can
21
+ # use code like this:
22
+ #
23
+ # adapter = DataMapper.setup(:default, 'mock://127.0.0.1/mock')
24
+ # adapter.resource_naming_convention = NamingConventions::Resource::Underscored
25
+ module NamingConventions
26
+
27
+ module Resource
28
+
29
+ module UnderscoredAndPluralized
30
+ def self.call(name)
31
+ DataMapper::Inflector.pluralize(DataMapper::Inflector.underscore(name)).gsub('/', '_')
32
+ end
33
+ end # module UnderscoredAndPluralized
34
+
35
+ module UnderscoredAndPluralizedWithoutModule
36
+ def self.call(name)
37
+ DataMapper::Inflector.pluralize(DataMapper::Inflector.underscore(DataMapper::Inflector.demodulize(name)))
38
+ end
39
+ end # module UnderscoredAndPluralizedWithoutModule
40
+
41
+ module UnderscoredAndPluralizedWithoutLeadingModule
42
+ def self.call(name)
43
+ UnderscoredAndPluralized.call(name.to_s.gsub(/^[^:]*::/,''))
44
+ end
45
+ end
46
+
47
+ module Underscored
48
+ def self.call(name)
49
+ DataMapper::Inflector.underscore(name)
50
+ end
51
+ end # module Underscored
52
+
53
+ module Yaml
54
+ def self.call(name)
55
+ "#{DataMapper::Inflector.pluralize(DataMapper::Inflector.underscore(name))}.yaml"
56
+ end
57
+ end # module Yaml
58
+
59
+ end # module Resource
60
+
61
+ module Field
62
+
63
+ module UnderscoredAndPluralized
64
+ def self.call(property)
65
+ DataMapper::Inflector.pluralize(DataMapper::Inflector.underscore(property.name.to_s)).gsub('/', '_')
66
+ end
67
+ end # module UnderscoredAndPluralized
68
+
69
+ module UnderscoredAndPluralizedWithoutModule
70
+ def self.call(property)
71
+ DataMapper::Inflector.pluralize(DataMapper::Inflector.underscore(DataMapper::Inflector.demodulize(property.name.to_s)))
72
+ end
73
+ end # module UnderscoredAndPluralizedWithoutModule
74
+
75
+ module Underscored
76
+ def self.call(property)
77
+ DataMapper::Inflector.underscore(property.name.to_s)
78
+ end
79
+ end # module Underscored
80
+
81
+ module FQN
82
+ def self.call(property)
83
+ model, name = property.model, property.name
84
+
85
+ fk_names = model.relationships.inject([]) { |names, rel|
86
+ if rel.respond_to?(:required?)
87
+ names + rel.source_key.map(&:name)
88
+ else
89
+ names
90
+ end
91
+ }
92
+
93
+ return name.to_s if fk_names.include?(name)
94
+
95
+ storage_name = model.storage_name(property.repository_name)
96
+ "#{DataMapper::Inflector.singularize(storage_name)}_#{name}"
97
+ end
98
+ end # module FQN
99
+
100
+ module Yaml
101
+ def self.call(property)
102
+ "#{DataMapper::Inflector.pluralize(DataMapper::Inflector.underscore(property.name.to_s))}.yaml"
103
+ end
104
+ end # module Yaml
105
+
106
+ end # module Field
107
+
108
+ end # module NamingConventions
109
+ end # module DataMapper
@@ -0,0 +1,381 @@
1
+ module DataMapper
2
+
3
+ # An ordered set of things
4
+ #
5
+ # {OrderedSet} implements set behavior and keeps
6
+ # track of the order in which entries were added.
7
+ #
8
+ # {OrderedSet} allows to inject a class that implements
9
+ # {OrderedSet::Cache::API} at construction time, and will
10
+ # use that cache implementation to enforce set semantics
11
+ # and perform internal caching of insertion order.
12
+ #
13
+ # @see OrderedSet::Cache::API
14
+ # @see OrderedSet::Cache
15
+ # @see SubjectSet::NameCache
16
+ #
17
+ # @api private
18
+ class OrderedSet
19
+
20
+ # The default cache used by {OrderedSet}
21
+ #
22
+ # Uses a {Hash} as internal storage and enforces set semantics
23
+ # by calling #eql? and #hash on the set's entries.
24
+ #
25
+ # @api private
26
+ class Cache
27
+
28
+ # The default implementation of the {API} that {OrderedSet} expects from
29
+ # the cache object that it uses to
30
+ #
31
+ # 1. keep track of insertion order
32
+ # 2. enforce set semantics.
33
+ #
34
+ # Classes including {API} must customize the behavior of the cache in 2 ways:
35
+ #
36
+ # They must determine the value to use as cache key and thus set discriminator,
37
+ # by implementing the {#key_for} method. The {#key_for} method accepts an arbitrary
38
+ # object as param and the method is free to return whatever value from that method.
39
+ # Obviously this will most likely be some attribute or value otherwise derived from
40
+ # the object that got passed in.
41
+ #
42
+ # They must determine which objects are valid set entries by overwriting the
43
+ # {#valid?} method. The {#valid?} method accepts an arbitrary object as param and
44
+ # the overwriting method must return either true or false.
45
+ #
46
+ # The motivation behind this is that set semantics cannot always be enforced
47
+ # by calling {#eql?} and {#hash} on the set's entries. For example, two entries
48
+ # might be considered unique wrt the set if their names are the same, but other
49
+ # internal state differs. This is exactly the case for {DataMapper::Property} and
50
+ # {DataMapper::Associations::Relationship} objects.
51
+ #
52
+ # @see DataMapper::SubjectSet::NameCache
53
+ #
54
+ # @api private
55
+ module API
56
+
57
+ # Initialize a new Cache
58
+ #
59
+ # @api private
60
+ def initialize
61
+ @cache = {}
62
+ end
63
+
64
+ # Tests if the given entry qualifies to be added to the cache
65
+ #
66
+ # @param [Object] entry
67
+ # the entry to be checked
68
+ #
69
+ # @return [Boolean]
70
+ # true if the entry qualifies to be added to the cache
71
+ #
72
+ # @api private
73
+ def valid?(entry)
74
+ raise NotImplementedError, "#{self}#valid? must be implemented"
75
+ end
76
+
77
+ # Given an entry, return the key to be used in the cache
78
+ #
79
+ # @param [Object] entry
80
+ # the entry to get the key for
81
+ #
82
+ # @return [Object, nil]
83
+ # a value derived from the entry that is used as key in the cache
84
+ #
85
+ # @api private
86
+ def key_for(entry)
87
+ raise NotImplementedError, "#{self}#key_for must be implemented"
88
+ end
89
+
90
+ # Check if the entry exists in the cache
91
+ #
92
+ # @param [Object] entry
93
+ # the entry to test for
94
+ #
95
+ # @return [Boolean]
96
+ # true if entry is included in the cache
97
+ #
98
+ # @api private
99
+ def include?(entry)
100
+ @cache.has_key?(key_for(entry))
101
+ end
102
+
103
+ # Return the index for the entry in the cache
104
+ #
105
+ # @param [Object] entry
106
+ # the entry to get the index for
107
+ #
108
+ # @return [Integer, nil]
109
+ # the index for the entry, or nil if it does not exist
110
+ #
111
+ # @api private
112
+ def [](entry)
113
+ @cache[key_for(entry)]
114
+ end
115
+
116
+ # Set the index for the entry in the cache
117
+ #
118
+ # @param [Object] entry
119
+ # the entry to set the index for
120
+ # @param [Integer] index
121
+ # the index to assign to the given entry
122
+ #
123
+ # @return [Integer]
124
+ # the given index for the entry
125
+ #
126
+ # @api private
127
+ def []=(entry, index)
128
+ if valid?(entry)
129
+ @cache[key_for(entry)] = index
130
+ end
131
+ end
132
+
133
+ # Delete an entry from the cache
134
+ #
135
+ # @param [Object] entry
136
+ # the entry to delete from the cache
137
+ #
138
+ # @return [API] self
139
+ #
140
+ # @api private
141
+ def delete(entry)
142
+ deleted_index = @cache.delete(key_for(entry))
143
+ if deleted_index
144
+ @cache.each do |key, index|
145
+ @cache[key] -= 1 if index > deleted_index
146
+ end
147
+ end
148
+ deleted_index
149
+ end
150
+
151
+ # Removes all entries and returns self
152
+ #
153
+ # @return [API] self
154
+ #
155
+ # @api private
156
+ def clear
157
+ @cache.clear
158
+ self
159
+ end
160
+
161
+ end # module API
162
+
163
+ include API
164
+
165
+ # Tests if the given entry qualifies to be added to the cache
166
+ #
167
+ # @param [Object] entry
168
+ # the entry to be checked
169
+ #
170
+ # @return [true] true
171
+ #
172
+ # @api private
173
+ def valid?(entry)
174
+ true
175
+ end
176
+
177
+ # Given an entry, return the key to be used in the cache
178
+ #
179
+ # @param [Object] entry
180
+ # the entry to get the key for
181
+ #
182
+ # @return [Object]
183
+ # the passed in entry
184
+ #
185
+ # @api private
186
+ def key_for(entry)
187
+ entry
188
+ end
189
+
190
+ end # class Cache
191
+
192
+ include Enumerable
193
+ extend Equalizer
194
+
195
+ # This set's entries
196
+ #
197
+ # The order in this Array is not guaranteed
198
+ # to be the order in which the entries were
199
+ # inserted. Use #each to access the entries
200
+ # in insertion order.
201
+ #
202
+ # @return [Array]
203
+ # this set's entries
204
+ #
205
+ # @api private
206
+ attr_reader :entries
207
+
208
+ equalize :entries
209
+
210
+ # Initialize an OrderedSet
211
+ #
212
+ # @param [#each] entries
213
+ # the entries to initialize this set with
214
+ # @param [Class<Cache::API>] cache
215
+ # the cache implementation to use
216
+ #
217
+ # @api private
218
+ def initialize(entries = [], cache = Cache)
219
+ @cache = cache.new
220
+ @entries = []
221
+ merge(entries.to_ary)
222
+ end
223
+
224
+ # Initialize a copy of OrderedSet
225
+ #
226
+ # @api private
227
+ def initialize_copy(*)
228
+ @cache = @cache.dup
229
+ @entries = @entries.dup
230
+ end
231
+
232
+ # Get the entry at the given index
233
+ #
234
+ # @param [Integer] index
235
+ # the index of the desired entry
236
+ #
237
+ # @return [Object, nil]
238
+ # the entry at the given index, or nil if no entry is present
239
+ #
240
+ # @api private
241
+ def [](index)
242
+ entries[index]
243
+ end
244
+
245
+ # Add or update an entry in the set
246
+ #
247
+ # If the entry to add isn't part of the set already,
248
+ # it will be added. If an entry with the same cache
249
+ # key as the entry to add is part of the set already,
250
+ # it will be replaced with the given entry.
251
+ #
252
+ # @param [Object] entry
253
+ # the entry to be added
254
+ #
255
+ # @return [OrderedSet] self
256
+ #
257
+ # @api private
258
+ def <<(entry)
259
+ if index = @cache[entry]
260
+ entries[index] = entry
261
+ else
262
+ @cache[entry] = size
263
+ entries << entry
264
+ end
265
+ self
266
+ end
267
+
268
+ # Merge with another Enumerable object
269
+ #
270
+ # @param [#each] other
271
+ # the Enumerable to merge with this OrderedSet
272
+ #
273
+ # @return [OrderedSet] self
274
+ #
275
+ # @api private
276
+ def merge(other)
277
+ other.each { |entry| self << entry }
278
+ self
279
+ end
280
+
281
+ # Delete an entry from this OrderedSet
282
+ #
283
+ # @param [Object] entry
284
+ # the entry to delete
285
+ #
286
+ # @return [Object, nil]
287
+ # the deleted entry or nil
288
+ #
289
+ # @api private
290
+ def delete(entry)
291
+ if index = @cache.delete(entry)
292
+ entries.delete_at(index)
293
+ end
294
+ end
295
+
296
+ # Removes all entries and returns self
297
+ #
298
+ # @return [OrderedSet] self
299
+ #
300
+ # @api private
301
+ def clear
302
+ @cache.clear
303
+ entries.clear
304
+ self
305
+ end
306
+
307
+ # Iterate over each entry in the set
308
+ #
309
+ # @yield [entry]
310
+ # all entries in the set
311
+ #
312
+ # @yieldparam [Object] entry
313
+ # an entry in the set
314
+ #
315
+ # @return [OrderedSet] self
316
+ #
317
+ # @api private
318
+ def each
319
+ return to_enum unless block_given?
320
+ entries.each { |entry| yield(entry) }
321
+ self
322
+ end
323
+
324
+ # The number of entries
325
+ #
326
+ # @return [Integer]
327
+ # the number of entries
328
+ #
329
+ # @api private
330
+ def size
331
+ entries.size
332
+ end
333
+
334
+ # Check if there are any entries
335
+ #
336
+ # @return [Boolean]
337
+ # true if the set is empty
338
+ #
339
+ # @api private
340
+ def empty?
341
+ entries.empty?
342
+ end
343
+
344
+ # Check if the entry exists in the set
345
+ #
346
+ # @param [Object] entry
347
+ # the entry to test for
348
+ #
349
+ # @return [Boolean]
350
+ # true if entry is included in the set
351
+ #
352
+ # @api private
353
+ def include?(entry)
354
+ entries.include?(entry)
355
+ end
356
+
357
+ # Return the index for the entry in the set
358
+ #
359
+ # @param [Object] entry
360
+ # the entry to check the set for
361
+ #
362
+ # @return [Integer, nil]
363
+ # the index for the entry, or nil if it does not exist
364
+ #
365
+ # @api private
366
+ def index(entry)
367
+ @cache[entry]
368
+ end
369
+
370
+ # Convert the OrderedSet into an Array
371
+ #
372
+ # @return [Array]
373
+ # an array containing all the OrderedSet's entries
374
+ #
375
+ # @api private
376
+ def to_ary
377
+ entries
378
+ end
379
+
380
+ end # class OrderedSet
381
+ end # module DataMapper
@@ -0,0 +1,33 @@
1
+ module DataMapper
2
+ module Subject
3
+ # Returns a default value of the subject for given resource
4
+ #
5
+ # When default value is a callable object, it is called with resource
6
+ # and subject passed as arguments.
7
+ #
8
+ # @param [Resource] resource
9
+ # the model instance for which the default is to be set
10
+ #
11
+ # @return [Object]
12
+ # the default value of this subject for +resource+
13
+ #
14
+ # @api semipublic
15
+ def default_for(resource)
16
+ if @default.respond_to?(:call)
17
+ @default.call(resource, self)
18
+ else
19
+ DataMapper::Ext.try_dup(@default)
20
+ end
21
+ end
22
+
23
+ # Returns true if the subject has a default value
24
+ #
25
+ # @return [Boolean]
26
+ # true if the subject has a default value
27
+ #
28
+ # @api semipublic
29
+ def default?
30
+ @options.key?(:default)
31
+ end
32
+ end
33
+ end