ghost_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 (254) hide show
  1. data/.autotest +29 -0
  2. data/.document +5 -0
  3. data/.gitignore +35 -0
  4. data/.yardopts +1 -0
  5. data/Gemfile +65 -0
  6. data/LICENSE +20 -0
  7. data/README.md +269 -0
  8. data/Rakefile +4 -0
  9. data/dm-core.gemspec +24 -0
  10. data/lib/dm-core.rb +292 -0
  11. data/lib/dm-core/adapters.rb +222 -0
  12. data/lib/dm-core/adapters/abstract_adapter.rb +237 -0
  13. data/lib/dm-core/adapters/in_memory_adapter.rb +113 -0
  14. data/lib/dm-core/associations/many_to_many.rb +499 -0
  15. data/lib/dm-core/associations/many_to_one.rb +290 -0
  16. data/lib/dm-core/associations/one_to_many.rb +348 -0
  17. data/lib/dm-core/associations/one_to_one.rb +86 -0
  18. data/lib/dm-core/associations/relationship.rb +663 -0
  19. data/lib/dm-core/backwards.rb +13 -0
  20. data/lib/dm-core/collection.rb +1515 -0
  21. data/lib/dm-core/core_ext/kernel.rb +23 -0
  22. data/lib/dm-core/core_ext/pathname.rb +6 -0
  23. data/lib/dm-core/core_ext/symbol.rb +10 -0
  24. data/lib/dm-core/identity_map.rb +7 -0
  25. data/lib/dm-core/model.rb +874 -0
  26. data/lib/dm-core/model/hook.rb +103 -0
  27. data/lib/dm-core/model/is.rb +32 -0
  28. data/lib/dm-core/model/property.rb +249 -0
  29. data/lib/dm-core/model/relationship.rb +378 -0
  30. data/lib/dm-core/model/scope.rb +89 -0
  31. data/lib/dm-core/property.rb +866 -0
  32. data/lib/dm-core/property/binary.rb +21 -0
  33. data/lib/dm-core/property/boolean.rb +20 -0
  34. data/lib/dm-core/property/class.rb +17 -0
  35. data/lib/dm-core/property/date.rb +10 -0
  36. data/lib/dm-core/property/date_time.rb +10 -0
  37. data/lib/dm-core/property/decimal.rb +36 -0
  38. data/lib/dm-core/property/discriminator.rb +44 -0
  39. data/lib/dm-core/property/float.rb +16 -0
  40. data/lib/dm-core/property/integer.rb +22 -0
  41. data/lib/dm-core/property/invalid_value_error.rb +22 -0
  42. data/lib/dm-core/property/lookup.rb +27 -0
  43. data/lib/dm-core/property/numeric.rb +38 -0
  44. data/lib/dm-core/property/object.rb +34 -0
  45. data/lib/dm-core/property/serial.rb +14 -0
  46. data/lib/dm-core/property/string.rb +38 -0
  47. data/lib/dm-core/property/text.rb +9 -0
  48. data/lib/dm-core/property/time.rb +10 -0
  49. data/lib/dm-core/property_set.rb +177 -0
  50. data/lib/dm-core/query.rb +1366 -0
  51. data/lib/dm-core/query/conditions/comparison.rb +911 -0
  52. data/lib/dm-core/query/conditions/operation.rb +721 -0
  53. data/lib/dm-core/query/direction.rb +36 -0
  54. data/lib/dm-core/query/operator.rb +35 -0
  55. data/lib/dm-core/query/path.rb +114 -0
  56. data/lib/dm-core/query/sort.rb +39 -0
  57. data/lib/dm-core/relationship_set.rb +72 -0
  58. data/lib/dm-core/repository.rb +226 -0
  59. data/lib/dm-core/resource.rb +1214 -0
  60. data/lib/dm-core/resource/persistence_state.rb +75 -0
  61. data/lib/dm-core/resource/persistence_state/clean.rb +40 -0
  62. data/lib/dm-core/resource/persistence_state/deleted.rb +30 -0
  63. data/lib/dm-core/resource/persistence_state/dirty.rb +96 -0
  64. data/lib/dm-core/resource/persistence_state/immutable.rb +34 -0
  65. data/lib/dm-core/resource/persistence_state/persisted.rb +29 -0
  66. data/lib/dm-core/resource/persistence_state/transient.rb +80 -0
  67. data/lib/dm-core/spec/lib/adapter_helpers.rb +64 -0
  68. data/lib/dm-core/spec/lib/collection_helpers.rb +21 -0
  69. data/lib/dm-core/spec/lib/counter_adapter.rb +38 -0
  70. data/lib/dm-core/spec/lib/pending_helpers.rb +50 -0
  71. data/lib/dm-core/spec/lib/spec_helper.rb +74 -0
  72. data/lib/dm-core/spec/setup.rb +174 -0
  73. data/lib/dm-core/spec/shared/adapter_spec.rb +341 -0
  74. data/lib/dm-core/spec/shared/public/property_spec.rb +229 -0
  75. data/lib/dm-core/spec/shared/resource_spec.rb +1232 -0
  76. data/lib/dm-core/spec/shared/sel_spec.rb +111 -0
  77. data/lib/dm-core/spec/shared/semipublic/property_spec.rb +176 -0
  78. data/lib/dm-core/spec/shared/semipublic/query/conditions/abstract_comparison_spec.rb +261 -0
  79. data/lib/dm-core/support/assertions.rb +8 -0
  80. data/lib/dm-core/support/chainable.rb +18 -0
  81. data/lib/dm-core/support/deprecate.rb +12 -0
  82. data/lib/dm-core/support/descendant_set.rb +89 -0
  83. data/lib/dm-core/support/equalizer.rb +48 -0
  84. data/lib/dm-core/support/ext/array.rb +22 -0
  85. data/lib/dm-core/support/ext/blank.rb +25 -0
  86. data/lib/dm-core/support/ext/hash.rb +67 -0
  87. data/lib/dm-core/support/ext/module.rb +47 -0
  88. data/lib/dm-core/support/ext/object.rb +57 -0
  89. data/lib/dm-core/support/ext/string.rb +24 -0
  90. data/lib/dm-core/support/ext/try_dup.rb +12 -0
  91. data/lib/dm-core/support/hook.rb +405 -0
  92. data/lib/dm-core/support/inflections.rb +60 -0
  93. data/lib/dm-core/support/inflector/inflections.rb +211 -0
  94. data/lib/dm-core/support/inflector/methods.rb +151 -0
  95. data/lib/dm-core/support/lazy_array.rb +451 -0
  96. data/lib/dm-core/support/local_object_space.rb +13 -0
  97. data/lib/dm-core/support/logger.rb +201 -0
  98. data/lib/dm-core/support/mash.rb +176 -0
  99. data/lib/dm-core/support/naming_conventions.rb +90 -0
  100. data/lib/dm-core/support/ordered_set.rb +380 -0
  101. data/lib/dm-core/support/subject.rb +33 -0
  102. data/lib/dm-core/support/subject_set.rb +250 -0
  103. data/lib/dm-core/version.rb +3 -0
  104. data/script/performance.rb +275 -0
  105. data/script/profile.rb +218 -0
  106. data/spec/lib/rspec_immediate_feedback_formatter.rb +54 -0
  107. data/spec/public/associations/many_to_many/read_multiple_join_spec.rb +68 -0
  108. data/spec/public/associations/many_to_many_spec.rb +197 -0
  109. data/spec/public/associations/many_to_one_spec.rb +83 -0
  110. data/spec/public/associations/many_to_one_with_boolean_cpk_spec.rb +40 -0
  111. data/spec/public/associations/many_to_one_with_custom_fk_spec.rb +49 -0
  112. data/spec/public/associations/one_to_many_spec.rb +81 -0
  113. data/spec/public/associations/one_to_one_spec.rb +176 -0
  114. data/spec/public/associations/one_to_one_with_boolean_cpk_spec.rb +46 -0
  115. data/spec/public/collection_spec.rb +69 -0
  116. data/spec/public/finalize_spec.rb +76 -0
  117. data/spec/public/model/hook_spec.rb +246 -0
  118. data/spec/public/model/property_spec.rb +88 -0
  119. data/spec/public/model/relationship_spec.rb +1040 -0
  120. data/spec/public/model_spec.rb +462 -0
  121. data/spec/public/property/binary_spec.rb +41 -0
  122. data/spec/public/property/boolean_spec.rb +22 -0
  123. data/spec/public/property/class_spec.rb +28 -0
  124. data/spec/public/property/date_spec.rb +22 -0
  125. data/spec/public/property/date_time_spec.rb +22 -0
  126. data/spec/public/property/decimal_spec.rb +23 -0
  127. data/spec/public/property/discriminator_spec.rb +135 -0
  128. data/spec/public/property/float_spec.rb +22 -0
  129. data/spec/public/property/integer_spec.rb +22 -0
  130. data/spec/public/property/object_spec.rb +107 -0
  131. data/spec/public/property/serial_spec.rb +22 -0
  132. data/spec/public/property/string_spec.rb +22 -0
  133. data/spec/public/property/text_spec.rb +63 -0
  134. data/spec/public/property/time_spec.rb +22 -0
  135. data/spec/public/property_spec.rb +341 -0
  136. data/spec/public/resource_spec.rb +288 -0
  137. data/spec/public/sel_spec.rb +53 -0
  138. data/spec/public/setup_spec.rb +145 -0
  139. data/spec/public/shared/association_collection_shared_spec.rb +309 -0
  140. data/spec/public/shared/collection_finder_shared_spec.rb +267 -0
  141. data/spec/public/shared/collection_shared_spec.rb +1667 -0
  142. data/spec/public/shared/finder_shared_spec.rb +1629 -0
  143. data/spec/rcov.opts +6 -0
  144. data/spec/semipublic/adapters/abstract_adapter_spec.rb +30 -0
  145. data/spec/semipublic/adapters/in_memory_adapter_spec.rb +13 -0
  146. data/spec/semipublic/associations/many_to_many_spec.rb +94 -0
  147. data/spec/semipublic/associations/many_to_one_spec.rb +63 -0
  148. data/spec/semipublic/associations/one_to_many_spec.rb +55 -0
  149. data/spec/semipublic/associations/one_to_one_spec.rb +53 -0
  150. data/spec/semipublic/associations/relationship_spec.rb +200 -0
  151. data/spec/semipublic/associations_spec.rb +177 -0
  152. data/spec/semipublic/collection_spec.rb +110 -0
  153. data/spec/semipublic/model_spec.rb +96 -0
  154. data/spec/semipublic/property/binary_spec.rb +13 -0
  155. data/spec/semipublic/property/boolean_spec.rb +47 -0
  156. data/spec/semipublic/property/class_spec.rb +33 -0
  157. data/spec/semipublic/property/date_spec.rb +43 -0
  158. data/spec/semipublic/property/date_time_spec.rb +46 -0
  159. data/spec/semipublic/property/decimal_spec.rb +83 -0
  160. data/spec/semipublic/property/discriminator_spec.rb +19 -0
  161. data/spec/semipublic/property/float_spec.rb +82 -0
  162. data/spec/semipublic/property/integer_spec.rb +82 -0
  163. data/spec/semipublic/property/lookup_spec.rb +29 -0
  164. data/spec/semipublic/property/serial_spec.rb +13 -0
  165. data/spec/semipublic/property/string_spec.rb +13 -0
  166. data/spec/semipublic/property/text_spec.rb +31 -0
  167. data/spec/semipublic/property/time_spec.rb +50 -0
  168. data/spec/semipublic/property_spec.rb +114 -0
  169. data/spec/semipublic/query/conditions/comparison_spec.rb +1501 -0
  170. data/spec/semipublic/query/conditions/operation_spec.rb +1294 -0
  171. data/spec/semipublic/query/path_spec.rb +471 -0
  172. data/spec/semipublic/query_spec.rb +3682 -0
  173. data/spec/semipublic/resource/state/clean_spec.rb +88 -0
  174. data/spec/semipublic/resource/state/deleted_spec.rb +78 -0
  175. data/spec/semipublic/resource/state/dirty_spec.rb +162 -0
  176. data/spec/semipublic/resource/state/immutable_spec.rb +105 -0
  177. data/spec/semipublic/resource/state/transient_spec.rb +162 -0
  178. data/spec/semipublic/resource/state_spec.rb +230 -0
  179. data/spec/semipublic/resource_spec.rb +23 -0
  180. data/spec/semipublic/shared/condition_shared_spec.rb +9 -0
  181. data/spec/semipublic/shared/resource_shared_spec.rb +199 -0
  182. data/spec/semipublic/shared/resource_state_shared_spec.rb +79 -0
  183. data/spec/semipublic/shared/subject_shared_spec.rb +79 -0
  184. data/spec/spec.opts +5 -0
  185. data/spec/spec_helper.rb +38 -0
  186. data/spec/support/core_ext/hash.rb +10 -0
  187. data/spec/support/core_ext/inheritable_attributes.rb +46 -0
  188. data/spec/support/properties/huge_integer.rb +17 -0
  189. data/spec/unit/array_spec.rb +23 -0
  190. data/spec/unit/blank_spec.rb +73 -0
  191. data/spec/unit/data_mapper/ordered_set/append_spec.rb +26 -0
  192. data/spec/unit/data_mapper/ordered_set/clear_spec.rb +24 -0
  193. data/spec/unit/data_mapper/ordered_set/delete_spec.rb +28 -0
  194. data/spec/unit/data_mapper/ordered_set/each_spec.rb +19 -0
  195. data/spec/unit/data_mapper/ordered_set/empty_spec.rb +20 -0
  196. data/spec/unit/data_mapper/ordered_set/entries_spec.rb +22 -0
  197. data/spec/unit/data_mapper/ordered_set/eql_spec.rb +51 -0
  198. data/spec/unit/data_mapper/ordered_set/equal_value_spec.rb +84 -0
  199. data/spec/unit/data_mapper/ordered_set/hash_spec.rb +12 -0
  200. data/spec/unit/data_mapper/ordered_set/include_spec.rb +23 -0
  201. data/spec/unit/data_mapper/ordered_set/index_spec.rb +28 -0
  202. data/spec/unit/data_mapper/ordered_set/initialize_spec.rb +32 -0
  203. data/spec/unit/data_mapper/ordered_set/merge_spec.rb +36 -0
  204. data/spec/unit/data_mapper/ordered_set/shared/append_spec.rb +24 -0
  205. data/spec/unit/data_mapper/ordered_set/shared/clear_spec.rb +9 -0
  206. data/spec/unit/data_mapper/ordered_set/shared/delete_spec.rb +25 -0
  207. data/spec/unit/data_mapper/ordered_set/shared/each_spec.rb +17 -0
  208. data/spec/unit/data_mapper/ordered_set/shared/empty_spec.rb +9 -0
  209. data/spec/unit/data_mapper/ordered_set/shared/entries_spec.rb +9 -0
  210. data/spec/unit/data_mapper/ordered_set/shared/include_spec.rb +9 -0
  211. data/spec/unit/data_mapper/ordered_set/shared/index_spec.rb +13 -0
  212. data/spec/unit/data_mapper/ordered_set/shared/initialize_spec.rb +28 -0
  213. data/spec/unit/data_mapper/ordered_set/shared/merge_spec.rb +28 -0
  214. data/spec/unit/data_mapper/ordered_set/shared/size_spec.rb +13 -0
  215. data/spec/unit/data_mapper/ordered_set/shared/to_ary_spec.rb +11 -0
  216. data/spec/unit/data_mapper/ordered_set/size_spec.rb +27 -0
  217. data/spec/unit/data_mapper/ordered_set/to_ary_spec.rb +23 -0
  218. data/spec/unit/data_mapper/subject_set/append_spec.rb +47 -0
  219. data/spec/unit/data_mapper/subject_set/clear_spec.rb +34 -0
  220. data/spec/unit/data_mapper/subject_set/delete_spec.rb +40 -0
  221. data/spec/unit/data_mapper/subject_set/each_spec.rb +30 -0
  222. data/spec/unit/data_mapper/subject_set/empty_spec.rb +31 -0
  223. data/spec/unit/data_mapper/subject_set/entries_spec.rb +31 -0
  224. data/spec/unit/data_mapper/subject_set/get_spec.rb +34 -0
  225. data/spec/unit/data_mapper/subject_set/include_spec.rb +32 -0
  226. data/spec/unit/data_mapper/subject_set/named_spec.rb +33 -0
  227. data/spec/unit/data_mapper/subject_set/shared/append_spec.rb +18 -0
  228. data/spec/unit/data_mapper/subject_set/shared/clear_spec.rb +9 -0
  229. data/spec/unit/data_mapper/subject_set/shared/delete_spec.rb +9 -0
  230. data/spec/unit/data_mapper/subject_set/shared/each_spec.rb +9 -0
  231. data/spec/unit/data_mapper/subject_set/shared/empty_spec.rb +9 -0
  232. data/spec/unit/data_mapper/subject_set/shared/entries_spec.rb +9 -0
  233. data/spec/unit/data_mapper/subject_set/shared/get_spec.rb +9 -0
  234. data/spec/unit/data_mapper/subject_set/shared/include_spec.rb +9 -0
  235. data/spec/unit/data_mapper/subject_set/shared/named_spec.rb +9 -0
  236. data/spec/unit/data_mapper/subject_set/shared/size_spec.rb +13 -0
  237. data/spec/unit/data_mapper/subject_set/shared/to_ary_spec.rb +9 -0
  238. data/spec/unit/data_mapper/subject_set/shared/values_at_spec.rb +44 -0
  239. data/spec/unit/data_mapper/subject_set/size_spec.rb +42 -0
  240. data/spec/unit/data_mapper/subject_set/to_ary_spec.rb +34 -0
  241. data/spec/unit/data_mapper/subject_set/values_at_spec.rb +57 -0
  242. data/spec/unit/hash_spec.rb +28 -0
  243. data/spec/unit/hook_spec.rb +1235 -0
  244. data/spec/unit/inflections_spec.rb +16 -0
  245. data/spec/unit/lazy_array_spec.rb +1949 -0
  246. data/spec/unit/mash_spec.rb +312 -0
  247. data/spec/unit/module_spec.rb +71 -0
  248. data/spec/unit/object_spec.rb +38 -0
  249. data/spec/unit/try_dup_spec.rb +46 -0
  250. data/tasks/ci.rake +1 -0
  251. data/tasks/spec.rake +38 -0
  252. data/tasks/yard.rake +9 -0
  253. data/tasks/yardstick.rake +19 -0
  254. metadata +365 -0
@@ -0,0 +1,405 @@
1
+ module DataMapper
2
+ #
3
+ # TODO: Write more documentation!
4
+ #
5
+ # Overview
6
+ # ========
7
+ #
8
+ # The Hook module is a very simple set of AOP helpers. Basically, it
9
+ # allows the developer to specify a method or block that should run
10
+ # before or after another method.
11
+ #
12
+ # Usage
13
+ # =====
14
+ #
15
+ # Halting The Hook Stack
16
+ #
17
+ # Inheritance
18
+ #
19
+ # Other Goodies
20
+ #
21
+ # Please bring up any issues regarding Hooks with carllerche on IRC
22
+ #
23
+ module Hook
24
+
25
+ def self.included(base)
26
+ base.extend(ClassMethods)
27
+ base.const_set("CLASS_HOOKS", {}) unless base.const_defined?("CLASS_HOOKS")
28
+ base.const_set("INSTANCE_HOOKS", {}) unless base.const_defined?("INSTANCE_HOOKS")
29
+ base.class_eval do
30
+ class << self
31
+ def method_added(name)
32
+ process_method_added(name, :instance)
33
+ super
34
+ end
35
+
36
+ def singleton_method_added(name)
37
+ process_method_added(name, :class)
38
+ super
39
+ end
40
+ end
41
+ end
42
+ super
43
+ end
44
+
45
+ module ClassMethods
46
+ extend DataMapper::LocalObjectSpace
47
+ include DataMapper::Assertions
48
+ # Inject code that executes before the target class method.
49
+ #
50
+ # @param target_method<Symbol> the name of the class method to inject before
51
+ # @param method_sym<Symbol> the name of the method to run before the
52
+ # target_method
53
+ # @param block<Block> the code to run before the target_method
54
+ #
55
+ # @note
56
+ # Either method_sym or block is required.
57
+ # -
58
+ # @api public
59
+ def before_class_method(target_method, method_sym = nil, &block)
60
+ install_hook :before, target_method, method_sym, :class, &block
61
+ end
62
+
63
+ #
64
+ # Inject code that executes after the target class method.
65
+ #
66
+ # @param target_method<Symbol> the name of the class method to inject after
67
+ # @param method_sym<Symbol> the name of the method to run after the target_method
68
+ # @param block<Block> the code to run after the target_method
69
+ #
70
+ # @note
71
+ # Either method_sym or block is required.
72
+ # -
73
+ # @api public
74
+ def after_class_method(target_method, method_sym = nil, &block)
75
+ install_hook :after, target_method, method_sym, :class, &block
76
+ end
77
+
78
+ #
79
+ # Inject code that executes before the target instance method.
80
+ #
81
+ # @param target_method<Symbol> the name of the instance method to inject before
82
+ # @param method_sym<Symbol> the name of the method to run before the
83
+ # target_method
84
+ # @param block<Block> the code to run before the target_method
85
+ #
86
+ # @note
87
+ # Either method_sym or block is required.
88
+ # -
89
+ # @api public
90
+ def before(target_method, method_sym = nil, &block)
91
+ install_hook :before, target_method, method_sym, :instance, &block
92
+ end
93
+
94
+ #
95
+ # Inject code that executes after the target instance method.
96
+ #
97
+ # @param target_method<Symbol> the name of the instance method to inject after
98
+ # @param method_sym<Symbol> the name of the method to run after the
99
+ # target_method
100
+ # @param block<Block> the code to run after the target_method
101
+ #
102
+ # @note
103
+ # Either method_sym or block is required.
104
+ # -
105
+ # @api public
106
+ def after(target_method, method_sym = nil, &block)
107
+ install_hook :after, target_method, method_sym, :instance, &block
108
+ end
109
+
110
+ # Register a class method as hookable. Registering a method means that
111
+ # before hooks will be run immediately before the method is invoked and
112
+ # after hooks will be called immediately after the method is invoked.
113
+ #
114
+ # @param hookable_method<Symbol> The name of the class method that should
115
+ # be hookable
116
+ # -
117
+ # @api public
118
+ def register_class_hooks(*hooks)
119
+ hooks.each { |hook| register_hook(hook, :class) }
120
+ end
121
+
122
+ # Register aninstance method as hookable. Registering a method means that
123
+ # before hooks will be run immediately before the method is invoked and
124
+ # after hooks will be called immediately after the method is invoked.
125
+ #
126
+ # @param hookable_method<Symbol> The name of the instance method that should
127
+ # be hookable
128
+ # -
129
+ # @api public
130
+ def register_instance_hooks(*hooks)
131
+ hooks.each { |hook| register_hook(hook, :instance) }
132
+ end
133
+
134
+ # Not yet implemented
135
+ def reset_hook!(target_method, scope)
136
+ raise NotImplementedError
137
+ end
138
+
139
+ # --- Alright kids... the rest is internal stuff ---
140
+
141
+ # Returns the correct HOOKS Hash depending on whether we are
142
+ # working with class methods or instance methods
143
+ def hooks_with_scope(scope)
144
+ case scope
145
+ when :class then class_hooks
146
+ when :instance then instance_hooks
147
+ else raise ArgumentError, 'You need to pass :class or :instance as scope'
148
+ end
149
+ end
150
+
151
+ def class_hooks
152
+ self.const_get("CLASS_HOOKS")
153
+ end
154
+
155
+ def instance_hooks
156
+ self.const_get("INSTANCE_HOOKS")
157
+ end
158
+
159
+ # Registers a method as hookable. Registering hooks involves the following
160
+ # process
161
+ #
162
+ # * Create a blank entry in the HOOK Hash for the method.
163
+ # * Define the methods that execute the before and after hook stack.
164
+ # These methods will be no-ops at first, but everytime a new hook is
165
+ # defined, the methods will be redefined to incorporate the new hook.
166
+ # * Redefine the method that is to be hookable so that the hook stacks
167
+ # are invoked approprietly.
168
+ def register_hook(target_method, scope)
169
+ if scope == :instance && !method_defined?(target_method)
170
+ raise ArgumentError, "#{target_method} instance method does not exist"
171
+ elsif scope == :class && !respond_to?(target_method)
172
+ raise ArgumentError, "#{target_method} class method does not exist"
173
+ end
174
+
175
+ hooks = hooks_with_scope(scope)
176
+
177
+ if hooks[target_method].nil?
178
+ hooks[target_method] = {
179
+ # We need to keep track of which class in the Inheritance chain the
180
+ # method was declared hookable in. Every time a child declares a new
181
+ # hook for the method, the hook stack invocations need to be redefined
182
+ # in the original Class. See #define_hook_stack_execution_methods
183
+ :before => [], :after => [], :in => self
184
+ }
185
+
186
+ define_hook_stack_execution_methods(target_method, scope)
187
+ define_advised_method(target_method, scope)
188
+ end
189
+ end
190
+
191
+ # Is the method registered as a hookable in the given scope.
192
+ def registered_as_hook?(target_method, scope)
193
+ ! hooks_with_scope(scope)[target_method].nil?
194
+ end
195
+
196
+ # Generates names for the various utility methods. We need to do this because
197
+ # the various utility methods should not end in = so, while we're at it, we
198
+ # might as well get rid of all punctuation.
199
+ def hook_method_name(target_method, prefix, suffix)
200
+ target_method = target_method.to_s
201
+
202
+ case target_method[-1,1]
203
+ when '?' then "#{prefix}_#{target_method[0..-2]}_ques_#{suffix}"
204
+ when '!' then "#{prefix}_#{target_method[0..-2]}_bang_#{suffix}"
205
+ when '=' then "#{prefix}_#{target_method[0..-2]}_eq_#{suffix}"
206
+ # I add a _nan_ suffix here so that we don't ever encounter
207
+ # any naming conflicts.
208
+ else "#{prefix}_#{target_method[0..-1]}_nan_#{suffix}"
209
+ end
210
+ end
211
+
212
+ # This will need to be refactored
213
+ def process_method_added(method_name, scope)
214
+ hooks_with_scope(scope).each do |target_method, hooks|
215
+ if hooks[:before].any? { |hook| hook[:name] == method_name }
216
+ define_hook_stack_execution_methods(target_method, scope)
217
+ end
218
+
219
+ if hooks[:after].any? { |hook| hook[:name] == method_name }
220
+ define_hook_stack_execution_methods(target_method, scope)
221
+ end
222
+ end
223
+ end
224
+
225
+ # Defines two methods. One method executes the before hook stack. The other executes
226
+ # the after hook stack. This method will be called many times during the Class definition
227
+ # process. It should be called for each hook that is defined. It will also be called
228
+ # when a hook is redefined (to make sure that the arity hasn't changed).
229
+ def define_hook_stack_execution_methods(target_method, scope)
230
+ unless registered_as_hook?(target_method, scope)
231
+ raise ArgumentError, "#{target_method} has not be registered as a hookable #{scope} method"
232
+ end
233
+
234
+ hooks = hooks_with_scope(scope)
235
+
236
+ before_hooks = hooks[target_method][:before]
237
+ before_hooks = before_hooks.map{ |info| inline_call(info, scope) }.join("\n")
238
+
239
+ after_hooks = hooks[target_method][:after]
240
+ after_hooks = after_hooks.map{ |info| inline_call(info, scope) }.join("\n")
241
+
242
+ before_hook_name = hook_method_name(target_method, 'execute_before', 'hook_stack')
243
+ after_hook_name = hook_method_name(target_method, 'execute_after', 'hook_stack')
244
+
245
+ hooks[target_method][:in].class_eval <<-RUBY, __FILE__, __LINE__ + 1
246
+ #{scope == :class ? 'class << self' : ''}
247
+
248
+ private
249
+
250
+ remove_method :#{before_hook_name} if instance_methods(false).any? { |m| m.to_sym == :#{before_hook_name} }
251
+ def #{before_hook_name}(*args)
252
+ #{before_hooks}
253
+ end
254
+
255
+ remove_method :#{after_hook_name} if instance_methods(false).any? { |m| m.to_sym == :#{after_hook_name} }
256
+ def #{after_hook_name}(*args)
257
+ #{after_hooks}
258
+ end
259
+
260
+ #{scope == :class ? 'end' : ''}
261
+ RUBY
262
+ end
263
+
264
+ # Returns ruby code that will invoke the hook. It checks the arity of the hook method
265
+ # and passes arguments accordingly.
266
+ def inline_call(method_info, scope)
267
+ DataMapper::Hook::ClassMethods.hook_scopes << method_info[:from]
268
+ name = method_info[:name]
269
+ if scope == :instance
270
+ args = method_defined?(name) && instance_method(name).arity != 0 ? '*args' : ''
271
+ %(#{name}(#{args}) if self.class <= DataMapper::Hook::ClassMethods.object_by_id(#{method_info[:from].object_id}))
272
+ else
273
+ args = respond_to?(name) && method(name).arity != 0 ? '*args' : ''
274
+ %(#{name}(#{args}) if self <= DataMapper::Hook::ClassMethods.object_by_id(#{method_info[:from].object_id}))
275
+ end
276
+ end
277
+
278
+ def define_advised_method(target_method, scope)
279
+ args = args_for(method_with_scope(target_method, scope))
280
+
281
+ renamed_target = hook_method_name(target_method, 'hookable_', 'before_advised')
282
+
283
+ source = <<-EOD
284
+ def #{target_method}(#{args})
285
+ retval = nil
286
+ catch(:halt) do
287
+ #{hook_method_name(target_method, 'execute_before', 'hook_stack')}(#{args})
288
+ retval = #{renamed_target}(#{args})
289
+ #{hook_method_name(target_method, 'execute_after', 'hook_stack')}(retval, #{args})
290
+ retval
291
+ end
292
+ end
293
+ EOD
294
+
295
+ if scope == :instance && !instance_methods(false).any? { |m| m.to_sym == target_method }
296
+ send(:alias_method, renamed_target, target_method)
297
+
298
+ proxy_module = Module.new
299
+ proxy_module.class_eval(source, __FILE__, __LINE__)
300
+ self.send(:include, proxy_module)
301
+ else
302
+ source = %{alias_method :#{renamed_target}, :#{target_method}\n#{source}}
303
+ source = %{class << self\n#{source}\nend} if scope == :class
304
+ class_eval(source, __FILE__, __LINE__)
305
+ end
306
+ end
307
+
308
+ # --- Add a hook ---
309
+
310
+ def install_hook(type, target_method, method_sym, scope, &block)
311
+ assert_kind_of 'target_method', target_method, Symbol
312
+ assert_kind_of 'method_sym', method_sym, Symbol unless method_sym.nil?
313
+ assert_kind_of 'scope', scope, Symbol
314
+
315
+ if !block_given? and method_sym.nil?
316
+ raise ArgumentError, "You need to pass 2 arguments to \"#{type}\"."
317
+ end
318
+
319
+ if method_sym.to_s[-1,1] == '='
320
+ raise ArgumentError, "Methods ending in = cannot be hooks"
321
+ end
322
+
323
+ unless [ :class, :instance ].include?(scope)
324
+ raise ArgumentError, 'You need to pass :class or :instance as scope'
325
+ end
326
+
327
+ if registered_as_hook?(target_method, scope)
328
+ hooks = hooks_with_scope(scope)
329
+
330
+ #if this hook is previously declared in a sibling or cousin we must move the :in class
331
+ #to the common ancestor to get both hooks to run.
332
+ if !(hooks[target_method][:in] <=> self)
333
+ before_hook_name = hook_method_name(target_method, 'execute_before', 'hook_stack')
334
+ after_hook_name = hook_method_name(target_method, 'execute_after', 'hook_stack')
335
+
336
+ hooks[target_method][:in].class_eval <<-RUBY, __FILE__, __LINE__ + 1
337
+ remove_method :#{before_hook_name} if instance_methods(false).any? { |m| m.to_sym == :#{before_hook_name} }
338
+ def #{before_hook_name}(*args)
339
+ super
340
+ end
341
+
342
+ remove_method :#{after_hook_name} if instance_methods(false).any? { |m| m.to_sym == :#{before_hook_name} }
343
+ def #{after_hook_name}(*args)
344
+ super
345
+ end
346
+ RUBY
347
+
348
+ while !(hooks[target_method][:in] <=> self) do
349
+ hooks[target_method][:in] = hooks[target_method][:in].superclass
350
+ end
351
+
352
+ define_hook_stack_execution_methods(target_method, scope)
353
+ hooks[target_method][:in].class_eval{define_advised_method(target_method, scope)}
354
+ end
355
+ else
356
+ register_hook(target_method, scope)
357
+ hooks = hooks_with_scope(scope)
358
+ end
359
+
360
+ #if we were passed a block, create a method out of it.
361
+ if block
362
+ method_sym = "__hooks_#{type}_#{quote_method(target_method)}_#{hooks[target_method][type].length}".to_sym
363
+ if scope == :class
364
+ singleton_class.instance_eval do
365
+ define_method(method_sym, &block)
366
+ end
367
+ else
368
+ define_method(method_sym, &block)
369
+ end
370
+ end
371
+
372
+ # Adds method to the stack an redefines the hook invocation method
373
+ hooks[target_method][type] << { :name => method_sym, :from => self }
374
+ define_hook_stack_execution_methods(target_method, scope)
375
+ end
376
+
377
+ # --- Helpers ---
378
+
379
+ def args_for(method)
380
+ if method.arity == 0
381
+ "&block"
382
+ elsif method.arity > 0
383
+ "_" << (1 .. method.arity).to_a.join(", _") << ", &block"
384
+ elsif (method.arity + 1) < 0
385
+ "_" << (1 .. (method.arity).abs - 1).to_a.join(", _") << ", *args, &block"
386
+ else
387
+ "*args, &block"
388
+ end
389
+ end
390
+
391
+ def method_with_scope(name, scope)
392
+ case scope
393
+ when :class then method(name)
394
+ when :instance then instance_method(name)
395
+ else raise ArgumentError, 'You need to pass :class or :instance as scope'
396
+ end
397
+ end
398
+
399
+ def quote_method(name)
400
+ name.to_s.gsub(/\?$/, '_q_').gsub(/!$/, '_b_').gsub(/=$/, '_eq_')
401
+ end
402
+ end
403
+
404
+ end
405
+ end
@@ -0,0 +1,60 @@
1
+ module DataMapper
2
+ Inflector.inflections do |inflect|
3
+ inflect.plural(/$/, 's')
4
+ inflect.plural(/s$/i, 's')
5
+ inflect.plural(/(ax|test)is$/i, '\1es')
6
+ inflect.plural(/(octop|vir)us$/i, '\1i')
7
+ inflect.plural(/(octop|vir)i$/i, '\1i')
8
+ inflect.plural(/(alias|status)$/i, '\1es')
9
+ inflect.plural(/(bu)s$/i, '\1ses')
10
+ inflect.plural(/(buffal|tomat)o$/i, '\1oes')
11
+ inflect.plural(/([ti])um$/i, '\1a')
12
+ inflect.plural(/([ti])a$/i, '\1a')
13
+ inflect.plural(/sis$/i, 'ses')
14
+ inflect.plural(/(?:([^f])fe|([lr])f)$/i, '\1\2ves')
15
+ inflect.plural(/(hive)$/i, '\1s')
16
+ inflect.plural(/([^aeiouy]|qu)y$/i, '\1ies')
17
+ inflect.plural(/(x|ch|ss|sh)$/i, '\1es')
18
+ inflect.plural(/(matr|vert|ind)(?:ix|ex)$/i, '\1ices')
19
+ inflect.plural(/([m|l])ouse$/i, '\1ice')
20
+ inflect.plural(/([m|l])ice$/i, '\1ice')
21
+ inflect.plural(/^(ox)$/i, '\1en')
22
+ inflect.plural(/^(oxen)$/i, '\1')
23
+ inflect.plural(/(quiz)$/i, '\1zes')
24
+
25
+ inflect.singular(/s$/i, '')
26
+ inflect.singular(/(n)ews$/i, '\1ews')
27
+ inflect.singular(/([ti])a$/i, '\1um')
28
+ inflect.singular(/((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/i, '\1\2sis')
29
+ inflect.singular(/(^analy)ses$/i, '\1sis')
30
+ inflect.singular(/([^f])ves$/i, '\1fe')
31
+ inflect.singular(/(hive)s$/i, '\1')
32
+ inflect.singular(/(tive)s$/i, '\1')
33
+ inflect.singular(/([lr])ves$/i, '\1f')
34
+ inflect.singular(/([^aeiouy]|qu)ies$/i, '\1y')
35
+ inflect.singular(/(s)eries$/i, '\1eries')
36
+ inflect.singular(/(m)ovies$/i, '\1ovie')
37
+ inflect.singular(/(x|ch|ss|sh)es$/i, '\1')
38
+ inflect.singular(/([m|l])ice$/i, '\1ouse')
39
+ inflect.singular(/(bus)es$/i, '\1')
40
+ inflect.singular(/(o)es$/i, '\1')
41
+ inflect.singular(/(shoe)s$/i, '\1')
42
+ inflect.singular(/(cris|ax|test)es$/i, '\1is')
43
+ inflect.singular(/(octop|vir)i$/i, '\1us')
44
+ inflect.singular(/(alias|status)(es)?$/i, '\1')
45
+ inflect.singular(/^(ox)en/i, '\1')
46
+ inflect.singular(/(vert|ind)ices$/i, '\1ex')
47
+ inflect.singular(/(matr)ices$/i, '\1ix')
48
+ inflect.singular(/(quiz)zes$/i, '\1')
49
+ inflect.singular(/(database)s$/i, '\1')
50
+
51
+ inflect.irregular('person', 'people')
52
+ inflect.irregular('man', 'men')
53
+ inflect.irregular('child', 'children')
54
+ inflect.irregular('sex', 'sexes')
55
+ inflect.irregular('move', 'moves')
56
+ inflect.irregular('cow', 'kine')
57
+
58
+ inflect.uncountable(%w(equipment information rice money species series fish sheep jeans))
59
+ end
60
+ end