ardm-core 1.2.1

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 +35 -0
  5. data/.travis.yml +23 -0
  6. data/.yardopts +1 -0
  7. data/Gemfile +63 -0
  8. data/LICENSE +20 -0
  9. data/README.rdoc +237 -0
  10. data/Rakefile +4 -0
  11. data/VERSION +1 -0
  12. data/ardm-core.gemspec +25 -0
  13. data/lib/ardm-core.rb +1 -0
  14. data/lib/dm-core.rb +285 -0
  15. data/lib/dm-core/adapters.rb +222 -0
  16. data/lib/dm-core/adapters/abstract_adapter.rb +236 -0
  17. data/lib/dm-core/adapters/in_memory_adapter.rb +113 -0
  18. data/lib/dm-core/associations/many_to_many.rb +496 -0
  19. data/lib/dm-core/associations/many_to_one.rb +296 -0
  20. data/lib/dm-core/associations/one_to_many.rb +345 -0
  21. data/lib/dm-core/associations/one_to_one.rb +86 -0
  22. data/lib/dm-core/associations/relationship.rb +663 -0
  23. data/lib/dm-core/backwards.rb +13 -0
  24. data/lib/dm-core/collection.rb +1514 -0
  25. data/lib/dm-core/core_ext/kernel.rb +23 -0
  26. data/lib/dm-core/core_ext/pathname.rb +6 -0
  27. data/lib/dm-core/core_ext/symbol.rb +10 -0
  28. data/lib/dm-core/identity_map.rb +7 -0
  29. data/lib/dm-core/model.rb +869 -0
  30. data/lib/dm-core/model/hook.rb +102 -0
  31. data/lib/dm-core/model/is.rb +32 -0
  32. data/lib/dm-core/model/property.rb +253 -0
  33. data/lib/dm-core/model/relationship.rb +377 -0
  34. data/lib/dm-core/model/scope.rb +89 -0
  35. data/lib/dm-core/property.rb +839 -0
  36. data/lib/dm-core/property/binary.rb +22 -0
  37. data/lib/dm-core/property/boolean.rb +31 -0
  38. data/lib/dm-core/property/class.rb +24 -0
  39. data/lib/dm-core/property/date.rb +45 -0
  40. data/lib/dm-core/property/date_time.rb +44 -0
  41. data/lib/dm-core/property/decimal.rb +50 -0
  42. data/lib/dm-core/property/discriminator.rb +46 -0
  43. data/lib/dm-core/property/float.rb +28 -0
  44. data/lib/dm-core/property/integer.rb +32 -0
  45. data/lib/dm-core/property/lookup.rb +29 -0
  46. data/lib/dm-core/property/numeric.rb +40 -0
  47. data/lib/dm-core/property/object.rb +28 -0
  48. data/lib/dm-core/property/serial.rb +13 -0
  49. data/lib/dm-core/property/string.rb +50 -0
  50. data/lib/dm-core/property/text.rb +12 -0
  51. data/lib/dm-core/property/time.rb +46 -0
  52. data/lib/dm-core/property/typecast/numeric.rb +32 -0
  53. data/lib/dm-core/property/typecast/time.rb +33 -0
  54. data/lib/dm-core/property_set.rb +177 -0
  55. data/lib/dm-core/query.rb +1444 -0
  56. data/lib/dm-core/query/conditions/comparison.rb +910 -0
  57. data/lib/dm-core/query/conditions/operation.rb +720 -0
  58. data/lib/dm-core/query/direction.rb +36 -0
  59. data/lib/dm-core/query/operator.rb +35 -0
  60. data/lib/dm-core/query/path.rb +114 -0
  61. data/lib/dm-core/query/sort.rb +39 -0
  62. data/lib/dm-core/relationship_set.rb +72 -0
  63. data/lib/dm-core/repository.rb +226 -0
  64. data/lib/dm-core/resource.rb +1228 -0
  65. data/lib/dm-core/resource/persistence_state.rb +75 -0
  66. data/lib/dm-core/resource/persistence_state/clean.rb +40 -0
  67. data/lib/dm-core/resource/persistence_state/deleted.rb +30 -0
  68. data/lib/dm-core/resource/persistence_state/dirty.rb +96 -0
  69. data/lib/dm-core/resource/persistence_state/immutable.rb +34 -0
  70. data/lib/dm-core/resource/persistence_state/persisted.rb +29 -0
  71. data/lib/dm-core/resource/persistence_state/transient.rb +78 -0
  72. data/lib/dm-core/spec/lib/adapter_helpers.rb +54 -0
  73. data/lib/dm-core/spec/lib/collection_helpers.rb +20 -0
  74. data/lib/dm-core/spec/lib/counter_adapter.rb +38 -0
  75. data/lib/dm-core/spec/lib/pending_helpers.rb +50 -0
  76. data/lib/dm-core/spec/lib/spec_helper.rb +74 -0
  77. data/lib/dm-core/spec/setup.rb +173 -0
  78. data/lib/dm-core/spec/shared/adapter_spec.rb +326 -0
  79. data/lib/dm-core/spec/shared/public/property_spec.rb +229 -0
  80. data/lib/dm-core/spec/shared/resource_spec.rb +1236 -0
  81. data/lib/dm-core/spec/shared/sel_spec.rb +111 -0
  82. data/lib/dm-core/spec/shared/semipublic/property_spec.rb +134 -0
  83. data/lib/dm-core/spec/shared/semipublic/query/conditions/abstract_comparison_spec.rb +261 -0
  84. data/lib/dm-core/support/assertions.rb +8 -0
  85. data/lib/dm-core/support/chainable.rb +18 -0
  86. data/lib/dm-core/support/deprecate.rb +12 -0
  87. data/lib/dm-core/support/descendant_set.rb +89 -0
  88. data/lib/dm-core/support/equalizer.rb +48 -0
  89. data/lib/dm-core/support/ext/array.rb +22 -0
  90. data/lib/dm-core/support/ext/blank.rb +25 -0
  91. data/lib/dm-core/support/ext/hash.rb +67 -0
  92. data/lib/dm-core/support/ext/module.rb +47 -0
  93. data/lib/dm-core/support/ext/object.rb +57 -0
  94. data/lib/dm-core/support/ext/string.rb +24 -0
  95. data/lib/dm-core/support/ext/try_dup.rb +12 -0
  96. data/lib/dm-core/support/hook.rb +402 -0
  97. data/lib/dm-core/support/inflections.rb +60 -0
  98. data/lib/dm-core/support/inflector/inflections.rb +211 -0
  99. data/lib/dm-core/support/inflector/methods.rb +151 -0
  100. data/lib/dm-core/support/lazy_array.rb +451 -0
  101. data/lib/dm-core/support/local_object_space.rb +12 -0
  102. data/lib/dm-core/support/logger.rb +199 -0
  103. data/lib/dm-core/support/mash.rb +176 -0
  104. data/lib/dm-core/support/naming_conventions.rb +90 -0
  105. data/lib/dm-core/support/ordered_set.rb +380 -0
  106. data/lib/dm-core/support/subject.rb +33 -0
  107. data/lib/dm-core/support/subject_set.rb +250 -0
  108. data/lib/dm-core/version.rb +3 -0
  109. data/script/performance.rb +275 -0
  110. data/script/profile.rb +218 -0
  111. data/spec/lib/rspec_immediate_feedback_formatter.rb +54 -0
  112. data/spec/public/associations/many_to_many/read_multiple_join_spec.rb +68 -0
  113. data/spec/public/associations/many_to_many_spec.rb +197 -0
  114. data/spec/public/associations/many_to_one_spec.rb +83 -0
  115. data/spec/public/associations/many_to_one_with_boolean_cpk_spec.rb +40 -0
  116. data/spec/public/associations/many_to_one_with_custom_fk_spec.rb +49 -0
  117. data/spec/public/associations/one_to_many_spec.rb +81 -0
  118. data/spec/public/associations/one_to_one_spec.rb +176 -0
  119. data/spec/public/associations/one_to_one_with_boolean_cpk_spec.rb +46 -0
  120. data/spec/public/collection_spec.rb +69 -0
  121. data/spec/public/finalize_spec.rb +76 -0
  122. data/spec/public/model/hook_spec.rb +246 -0
  123. data/spec/public/model/property_spec.rb +88 -0
  124. data/spec/public/model/relationship_spec.rb +1040 -0
  125. data/spec/public/model_spec.rb +458 -0
  126. data/spec/public/property/binary_spec.rb +41 -0
  127. data/spec/public/property/boolean_spec.rb +22 -0
  128. data/spec/public/property/class_spec.rb +28 -0
  129. data/spec/public/property/date_spec.rb +22 -0
  130. data/spec/public/property/date_time_spec.rb +22 -0
  131. data/spec/public/property/decimal_spec.rb +23 -0
  132. data/spec/public/property/discriminator_spec.rb +135 -0
  133. data/spec/public/property/float_spec.rb +22 -0
  134. data/spec/public/property/integer_spec.rb +22 -0
  135. data/spec/public/property/object_spec.rb +107 -0
  136. data/spec/public/property/serial_spec.rb +22 -0
  137. data/spec/public/property/string_spec.rb +22 -0
  138. data/spec/public/property/text_spec.rb +63 -0
  139. data/spec/public/property/time_spec.rb +22 -0
  140. data/spec/public/property_spec.rb +341 -0
  141. data/spec/public/resource_spec.rb +284 -0
  142. data/spec/public/sel_spec.rb +53 -0
  143. data/spec/public/setup_spec.rb +145 -0
  144. data/spec/public/shared/association_collection_shared_spec.rb +309 -0
  145. data/spec/public/shared/collection_finder_shared_spec.rb +267 -0
  146. data/spec/public/shared/collection_shared_spec.rb +1669 -0
  147. data/spec/public/shared/finder_shared_spec.rb +1629 -0
  148. data/spec/rcov.opts +6 -0
  149. data/spec/semipublic/adapters/abstract_adapter_spec.rb +30 -0
  150. data/spec/semipublic/adapters/in_memory_adapter_spec.rb +12 -0
  151. data/spec/semipublic/associations/many_to_many_spec.rb +94 -0
  152. data/spec/semipublic/associations/many_to_one_spec.rb +63 -0
  153. data/spec/semipublic/associations/one_to_many_spec.rb +55 -0
  154. data/spec/semipublic/associations/one_to_one_spec.rb +53 -0
  155. data/spec/semipublic/associations/relationship_spec.rb +200 -0
  156. data/spec/semipublic/associations_spec.rb +177 -0
  157. data/spec/semipublic/collection_spec.rb +110 -0
  158. data/spec/semipublic/model_spec.rb +96 -0
  159. data/spec/semipublic/property/binary_spec.rb +13 -0
  160. data/spec/semipublic/property/boolean_spec.rb +47 -0
  161. data/spec/semipublic/property/class_spec.rb +33 -0
  162. data/spec/semipublic/property/date_spec.rb +43 -0
  163. data/spec/semipublic/property/date_time_spec.rb +46 -0
  164. data/spec/semipublic/property/decimal_spec.rb +83 -0
  165. data/spec/semipublic/property/discriminator_spec.rb +19 -0
  166. data/spec/semipublic/property/float_spec.rb +82 -0
  167. data/spec/semipublic/property/integer_spec.rb +82 -0
  168. data/spec/semipublic/property/lookup_spec.rb +29 -0
  169. data/spec/semipublic/property/serial_spec.rb +13 -0
  170. data/spec/semipublic/property/string_spec.rb +13 -0
  171. data/spec/semipublic/property/text_spec.rb +31 -0
  172. data/spec/semipublic/property/time_spec.rb +50 -0
  173. data/spec/semipublic/property_spec.rb +114 -0
  174. data/spec/semipublic/query/conditions/comparison_spec.rb +1501 -0
  175. data/spec/semipublic/query/conditions/operation_spec.rb +1294 -0
  176. data/spec/semipublic/query/path_spec.rb +471 -0
  177. data/spec/semipublic/query_spec.rb +3777 -0
  178. data/spec/semipublic/resource/state/clean_spec.rb +88 -0
  179. data/spec/semipublic/resource/state/deleted_spec.rb +78 -0
  180. data/spec/semipublic/resource/state/dirty_spec.rb +156 -0
  181. data/spec/semipublic/resource/state/immutable_spec.rb +105 -0
  182. data/spec/semipublic/resource/state/transient_spec.rb +162 -0
  183. data/spec/semipublic/resource/state_spec.rb +230 -0
  184. data/spec/semipublic/resource_spec.rb +23 -0
  185. data/spec/semipublic/shared/condition_shared_spec.rb +9 -0
  186. data/spec/semipublic/shared/resource_shared_spec.rb +199 -0
  187. data/spec/semipublic/shared/resource_state_shared_spec.rb +79 -0
  188. data/spec/semipublic/shared/subject_shared_spec.rb +79 -0
  189. data/spec/spec.opts +5 -0
  190. data/spec/spec_helper.rb +37 -0
  191. data/spec/support/core_ext/hash.rb +10 -0
  192. data/spec/support/core_ext/inheritable_attributes.rb +46 -0
  193. data/spec/support/properties/huge_integer.rb +17 -0
  194. data/spec/unit/array_spec.rb +23 -0
  195. data/spec/unit/blank_spec.rb +73 -0
  196. data/spec/unit/data_mapper/ordered_set/append_spec.rb +26 -0
  197. data/spec/unit/data_mapper/ordered_set/clear_spec.rb +24 -0
  198. data/spec/unit/data_mapper/ordered_set/delete_spec.rb +28 -0
  199. data/spec/unit/data_mapper/ordered_set/each_spec.rb +19 -0
  200. data/spec/unit/data_mapper/ordered_set/empty_spec.rb +20 -0
  201. data/spec/unit/data_mapper/ordered_set/entries_spec.rb +22 -0
  202. data/spec/unit/data_mapper/ordered_set/eql_spec.rb +51 -0
  203. data/spec/unit/data_mapper/ordered_set/equal_value_spec.rb +84 -0
  204. data/spec/unit/data_mapper/ordered_set/hash_spec.rb +12 -0
  205. data/spec/unit/data_mapper/ordered_set/include_spec.rb +23 -0
  206. data/spec/unit/data_mapper/ordered_set/index_spec.rb +28 -0
  207. data/spec/unit/data_mapper/ordered_set/initialize_spec.rb +32 -0
  208. data/spec/unit/data_mapper/ordered_set/merge_spec.rb +36 -0
  209. data/spec/unit/data_mapper/ordered_set/shared/append_spec.rb +24 -0
  210. data/spec/unit/data_mapper/ordered_set/shared/clear_spec.rb +9 -0
  211. data/spec/unit/data_mapper/ordered_set/shared/delete_spec.rb +25 -0
  212. data/spec/unit/data_mapper/ordered_set/shared/each_spec.rb +17 -0
  213. data/spec/unit/data_mapper/ordered_set/shared/empty_spec.rb +9 -0
  214. data/spec/unit/data_mapper/ordered_set/shared/entries_spec.rb +9 -0
  215. data/spec/unit/data_mapper/ordered_set/shared/include_spec.rb +9 -0
  216. data/spec/unit/data_mapper/ordered_set/shared/index_spec.rb +13 -0
  217. data/spec/unit/data_mapper/ordered_set/shared/initialize_spec.rb +28 -0
  218. data/spec/unit/data_mapper/ordered_set/shared/merge_spec.rb +28 -0
  219. data/spec/unit/data_mapper/ordered_set/shared/size_spec.rb +13 -0
  220. data/spec/unit/data_mapper/ordered_set/shared/to_ary_spec.rb +11 -0
  221. data/spec/unit/data_mapper/ordered_set/size_spec.rb +27 -0
  222. data/spec/unit/data_mapper/ordered_set/to_ary_spec.rb +23 -0
  223. data/spec/unit/data_mapper/subject_set/append_spec.rb +47 -0
  224. data/spec/unit/data_mapper/subject_set/clear_spec.rb +34 -0
  225. data/spec/unit/data_mapper/subject_set/delete_spec.rb +40 -0
  226. data/spec/unit/data_mapper/subject_set/each_spec.rb +30 -0
  227. data/spec/unit/data_mapper/subject_set/empty_spec.rb +31 -0
  228. data/spec/unit/data_mapper/subject_set/entries_spec.rb +31 -0
  229. data/spec/unit/data_mapper/subject_set/get_spec.rb +34 -0
  230. data/spec/unit/data_mapper/subject_set/include_spec.rb +32 -0
  231. data/spec/unit/data_mapper/subject_set/named_spec.rb +33 -0
  232. data/spec/unit/data_mapper/subject_set/shared/append_spec.rb +18 -0
  233. data/spec/unit/data_mapper/subject_set/shared/clear_spec.rb +9 -0
  234. data/spec/unit/data_mapper/subject_set/shared/delete_spec.rb +9 -0
  235. data/spec/unit/data_mapper/subject_set/shared/each_spec.rb +9 -0
  236. data/spec/unit/data_mapper/subject_set/shared/empty_spec.rb +9 -0
  237. data/spec/unit/data_mapper/subject_set/shared/entries_spec.rb +9 -0
  238. data/spec/unit/data_mapper/subject_set/shared/get_spec.rb +9 -0
  239. data/spec/unit/data_mapper/subject_set/shared/include_spec.rb +9 -0
  240. data/spec/unit/data_mapper/subject_set/shared/named_spec.rb +9 -0
  241. data/spec/unit/data_mapper/subject_set/shared/size_spec.rb +13 -0
  242. data/spec/unit/data_mapper/subject_set/shared/to_ary_spec.rb +9 -0
  243. data/spec/unit/data_mapper/subject_set/shared/values_at_spec.rb +44 -0
  244. data/spec/unit/data_mapper/subject_set/size_spec.rb +42 -0
  245. data/spec/unit/data_mapper/subject_set/to_ary_spec.rb +34 -0
  246. data/spec/unit/data_mapper/subject_set/values_at_spec.rb +57 -0
  247. data/spec/unit/hash_spec.rb +28 -0
  248. data/spec/unit/hook_spec.rb +1235 -0
  249. data/spec/unit/lazy_array_spec.rb +1949 -0
  250. data/spec/unit/mash_spec.rb +312 -0
  251. data/spec/unit/module_spec.rb +71 -0
  252. data/spec/unit/object_spec.rb +38 -0
  253. data/spec/unit/try_dup_spec.rb +46 -0
  254. data/tasks/ci.rake +1 -0
  255. data/tasks/db.rake +11 -0
  256. data/tasks/spec.rake +38 -0
  257. data/tasks/yard.rake +9 -0
  258. data/tasks/yardstick.rake +19 -0
  259. metadata +491 -0
@@ -0,0 +1,211 @@
1
+ module DataMapper
2
+ module Inflector
3
+ # A singleton instance of this class is yielded by Inflector.inflections, which can then be used to specify additional
4
+ # inflection rules. Examples:
5
+ #
6
+ # DataMapper::Inflector.inflections do |inflect|
7
+ # inflect.plural /^(ox)$/i, '\1\2en'
8
+ # inflect.singular /^(ox)en/i, '\1'
9
+ #
10
+ # inflect.irregular 'octopus', 'octopi'
11
+ #
12
+ # inflect.uncountable "equipment"
13
+ # end
14
+ #
15
+ # New rules are added at the top. So in the example above, the irregular rule for octopus will now be the first of the
16
+ # pluralization and singularization rules that is runs. This guarantees that your rules run before any of the rules that may
17
+ # already have been loaded.
18
+ class Inflections
19
+ def self.instance
20
+ @__instance__ ||= new
21
+ end
22
+
23
+ attr_reader :plurals, :singulars, :uncountables, :humans
24
+
25
+ def initialize
26
+ @plurals, @singulars, @uncountables, @humans = [], [], [], []
27
+ end
28
+
29
+ # Specifies a new pluralization rule and its replacement. The rule can either be a string or a regular expression.
30
+ # The replacement should always be a string that may include references to the matched data from the rule.
31
+ def plural(rule, replacement)
32
+ @uncountables.delete(rule) if rule.is_a?(String)
33
+ @uncountables.delete(replacement)
34
+ @plurals.insert(0, [rule, replacement])
35
+ end
36
+
37
+ # Specifies a new singularization rule and its replacement. The rule can either be a string or a regular expression.
38
+ # The replacement should always be a string that may include references to the matched data from the rule.
39
+ def singular(rule, replacement)
40
+ @uncountables.delete(rule) if rule.is_a?(String)
41
+ @uncountables.delete(replacement)
42
+ @singulars.insert(0, [rule, replacement])
43
+ end
44
+
45
+ # Specifies a new irregular that applies to both pluralization and singularization at the same time. This can only be used
46
+ # for strings, not regular expressions. You simply pass the irregular in singular and plural form.
47
+ #
48
+ # Examples:
49
+ # irregular 'octopus', 'octopi'
50
+ # irregular 'person', 'people'
51
+ def irregular(singular, plural)
52
+ @uncountables.delete(singular)
53
+ @uncountables.delete(plural)
54
+ if singular[0,1].upcase == plural[0,1].upcase
55
+ plural(Regexp.new("(#{singular[0,1]})#{singular[1..-1]}$", "i"), '\1' + plural[1..-1])
56
+ plural(Regexp.new("(#{plural[0,1]})#{plural[1..-1]}$", "i"), '\1' + plural[1..-1])
57
+ singular(Regexp.new("(#{plural[0,1]})#{plural[1..-1]}$", "i"), '\1' + singular[1..-1])
58
+ else
59
+ plural(Regexp.new("#{singular[0,1].upcase}(?i)#{singular[1..-1]}$"), plural[0,1].upcase + plural[1..-1])
60
+ plural(Regexp.new("#{singular[0,1].downcase}(?i)#{singular[1..-1]}$"), plural[0,1].downcase + plural[1..-1])
61
+ plural(Regexp.new("#{plural[0,1].upcase}(?i)#{plural[1..-1]}$"), plural[0,1].upcase + plural[1..-1])
62
+ plural(Regexp.new("#{plural[0,1].downcase}(?i)#{plural[1..-1]}$"), plural[0,1].downcase + plural[1..-1])
63
+ singular(Regexp.new("#{plural[0,1].upcase}(?i)#{plural[1..-1]}$"), singular[0,1].upcase + singular[1..-1])
64
+ singular(Regexp.new("#{plural[0,1].downcase}(?i)#{plural[1..-1]}$"), singular[0,1].downcase + singular[1..-1])
65
+ end
66
+ end
67
+
68
+ # Add uncountable words that shouldn't be attempted inflected.
69
+ #
70
+ # Examples:
71
+ # uncountable "money"
72
+ # uncountable "money", "information"
73
+ # uncountable %w( money information rice )
74
+ def uncountable(*words)
75
+ (@uncountables << words).flatten!
76
+ end
77
+
78
+ # Specifies a humanized form of a string by a regular expression rule or by a string mapping.
79
+ # When using a regular expression based replacement, the normal humanize formatting is called after the replacement.
80
+ # When a string is used, the human form should be specified as desired (example: 'The name', not 'the_name')
81
+ #
82
+ # Examples:
83
+ # human /_cnt$/i, '\1_count'
84
+ # human "legacy_col_person_name", "Name"
85
+ def human(rule, replacement)
86
+ @humans.insert(0, [rule, replacement])
87
+ end
88
+
89
+ # Clears the loaded inflections within a given scope (default is <tt>:all</tt>).
90
+ # Give the scope as a symbol of the inflection type, the options are: <tt>:plurals</tt>,
91
+ # <tt>:singulars</tt>, <tt>:uncountables</tt>, <tt>:humans</tt>.
92
+ #
93
+ # Examples:
94
+ # clear :all
95
+ # clear :plurals
96
+ def clear(scope = :all)
97
+ case scope
98
+ when :all
99
+ @plurals, @singulars, @uncountables = [], [], []
100
+ else
101
+ instance_variable_set "@#{scope}", []
102
+ end
103
+ end
104
+ end
105
+
106
+ # Yields a singleton instance of Inflector::Inflections so you can specify additional
107
+ # inflector rules.
108
+ #
109
+ # Example:
110
+ # DataMapper::Inflector.inflections do |inflect|
111
+ # inflect.uncountable "rails"
112
+ # end
113
+ def inflections
114
+ if block_given?
115
+ yield Inflections.instance
116
+ else
117
+ Inflections.instance
118
+ end
119
+ end
120
+
121
+ # Returns the plural form of the word in the string.
122
+ #
123
+ # Examples:
124
+ # "post".pluralize # => "posts"
125
+ # "octopus".pluralize # => "octopi"
126
+ # "sheep".pluralize # => "sheep"
127
+ # "words".pluralize # => "words"
128
+ # "CamelOctopus".pluralize # => "CamelOctopi"
129
+ def pluralize(word)
130
+ result = word.to_s.dup
131
+
132
+ if word.empty? || inflections.uncountables.include?(result.downcase)
133
+ result
134
+ else
135
+ inflections.plurals.each { |(rule, replacement)| break if result.gsub!(rule, replacement) }
136
+ result
137
+ end
138
+ end
139
+
140
+ # The reverse of +pluralize+, returns the singular form of a word in a string.
141
+ #
142
+ # Examples:
143
+ # "posts".singularize # => "post"
144
+ # "octopi".singularize # => "octopus"
145
+ # "sheep".singularize # => "sheep"
146
+ # "word".singularize # => "word"
147
+ # "CamelOctopi".singularize # => "CamelOctopus"
148
+ def singularize(word)
149
+ result = word.to_s.dup
150
+
151
+ if inflections.uncountables.any? { |inflection| result =~ /\b(#{inflection})\Z/i }
152
+ result
153
+ else
154
+ inflections.singulars.each { |(rule, replacement)| break if result.gsub!(rule, replacement) }
155
+ result
156
+ end
157
+ end
158
+
159
+ # Capitalizes the first word and turns underscores into spaces and strips a
160
+ # trailing "_id", if any. Like +titleize+, this is meant for creating pretty output.
161
+ #
162
+ # Examples:
163
+ # "employee_salary" # => "Employee salary"
164
+ # "author_id" # => "Author"
165
+ def humanize(lower_case_and_underscored_word)
166
+ result = lower_case_and_underscored_word.to_s.dup
167
+
168
+ inflections.humans.each { |(rule, replacement)| break if result.gsub!(rule, replacement) }
169
+ result.gsub(/_id$/, "").gsub(/_/, " ").capitalize
170
+ end
171
+
172
+ # Capitalizes all the words and replaces some characters in the string to create
173
+ # a nicer looking title. +titleize+ is meant for creating pretty output. It is not
174
+ # used in the Rails internals.
175
+ #
176
+ # +titleize+ is also aliased as as +titlecase+.
177
+ #
178
+ # Examples:
179
+ # "man from the boondocks".titleize # => "Man From The Boondocks"
180
+ # "x-men: the last stand".titleize # => "X Men: The Last Stand"
181
+ def titleize(word)
182
+ humanize(underscore(word)).gsub(/\b('?[a-z])/) { $1.capitalize }
183
+ end
184
+
185
+ # Create the name of a table like Rails does for models to table names. This method
186
+ # uses the +pluralize+ method on the last word in the string.
187
+ #
188
+ # Examples
189
+ # "RawScaledScorer".tableize # => "raw_scaled_scorers"
190
+ # "egg_and_ham".tableize # => "egg_and_hams"
191
+ # "fancyCategory".tableize # => "fancy_categories"
192
+ def tableize(class_name)
193
+ pluralize(underscore(class_name))
194
+ end
195
+
196
+ # Create a class name from a plural table name like Rails does for table names to models.
197
+ # Note that this returns a string and not a Class. (To convert to an actual class
198
+ # follow +classify+ with +constantize+.)
199
+ #
200
+ # Examples:
201
+ # "egg_and_hams".classify # => "EggAndHam"
202
+ # "posts".classify # => "Post"
203
+ #
204
+ # Singular names are not handled correctly:
205
+ # "business".classify # => "Busines"
206
+ def classify(table_name)
207
+ # strip out any leading schema name
208
+ camelize(singularize(table_name.to_s.sub(/.*\./, '')))
209
+ end
210
+ end
211
+ end
@@ -0,0 +1,151 @@
1
+ module DataMapper
2
+ # The Inflector transforms words from singular to plural, class names to table names, modularized class names to ones without,
3
+ # and class names to foreign keys. The default inflections for pluralization, singularization, and uncountable words are kept
4
+ # in inflections.rb.
5
+ #
6
+ # The Rails core team has stated patches for the inflections library will not be accepted
7
+ # in order to avoid breaking legacy applications which may be relying on errant inflections.
8
+ # If you discover an incorrect inflection and require it for your application, you'll need
9
+ # to correct it yourself (explained below).
10
+ module Inflector
11
+ extend self
12
+
13
+ # By default, +camelize+ converts strings to UpperCamelCase. If the argument to +camelize+
14
+ # is set to <tt>:lower</tt> then +camelize+ produces lowerCamelCase.
15
+ #
16
+ # +camelize+ will also convert '/' to '::' which is useful for converting paths to namespaces.
17
+ #
18
+ # Examples:
19
+ # "active_record".camelize # => "ActiveRecord"
20
+ # "active_record".camelize(:lower) # => "activeRecord"
21
+ # "active_record/errors".camelize # => "ActiveRecord::Errors"
22
+ # "active_record/errors".camelize(:lower) # => "activeRecord::Errors"
23
+ #
24
+ # As a rule of thumb you can think of +camelize+ as the inverse of +underscore+,
25
+ # though there are cases where that does not hold:
26
+ #
27
+ # "SSLError".underscore.camelize # => "SslError"
28
+ def camelize(lower_case_and_underscored_word, first_letter_in_uppercase = true)
29
+ if first_letter_in_uppercase
30
+ lower_case_and_underscored_word.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
31
+ else
32
+ lower_case_and_underscored_word.to_s[0].chr.downcase + camelize(lower_case_and_underscored_word)[1..-1]
33
+ end
34
+ end
35
+
36
+ # Makes an underscored, lowercase form from the expression in the string.
37
+ #
38
+ # Changes '::' to '/' to convert namespaces to paths.
39
+ #
40
+ # Examples:
41
+ # "ActiveRecord".underscore # => "active_record"
42
+ # "ActiveRecord::Errors".underscore # => active_record/errors
43
+ #
44
+ # As a rule of thumb you can think of +underscore+ as the inverse of +camelize+,
45
+ # though there are cases where that does not hold:
46
+ #
47
+ # "SSLError".underscore.camelize # => "SslError"
48
+ def underscore(camel_cased_word)
49
+ word = camel_cased_word.to_s.dup
50
+ word.gsub!(/::/, '/')
51
+ word.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
52
+ word.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
53
+ word.tr!("-", "_")
54
+ word.downcase!
55
+ word
56
+ end
57
+
58
+ # Replaces underscores with dashes in the string.
59
+ #
60
+ # Example:
61
+ # "puni_puni" # => "puni-puni"
62
+ def dasherize(underscored_word)
63
+ underscored_word.gsub(/_/, '-')
64
+ end
65
+
66
+ # Removes the module part from the expression in the string.
67
+ #
68
+ # Examples:
69
+ # "ActiveRecord::CoreExtensions::String::Inflections".demodulize # => "Inflections"
70
+ # "Inflections".demodulize # => "Inflections"
71
+ def demodulize(class_name_in_module)
72
+ class_name_in_module.to_s.gsub(/^.*::/, '')
73
+ end
74
+
75
+ # Creates a foreign key name from a class name.
76
+ # +separate_class_name_and_id_with_underscore+ sets whether
77
+ # the method should put '_' between the name and 'id'.
78
+ #
79
+ # Examples:
80
+ # "Message".foreign_key # => "message_id"
81
+ # "Message".foreign_key(false) # => "messageid"
82
+ # "Admin::Post".foreign_key # => "post_id"
83
+ def foreign_key(class_name, separate_class_name_and_id_with_underscore = true)
84
+ underscore(demodulize(class_name)) + (separate_class_name_and_id_with_underscore ? "_id" : "id")
85
+ end
86
+
87
+ # Ruby 1.9 introduces an inherit argument for Module#const_get and
88
+ # #const_defined? and changes their default behavior.
89
+ if Module.method(:const_get).arity == 1
90
+ # Tries to find a constant with the name specified in the argument string:
91
+ #
92
+ # "Module".constantize # => Module
93
+ # "Test::Unit".constantize # => Test::Unit
94
+ #
95
+ # The name is assumed to be the one of a top-level constant, no matter whether
96
+ # it starts with "::" or not. No lexical context is taken into account:
97
+ #
98
+ # C = 'outside'
99
+ # module M
100
+ # C = 'inside'
101
+ # C # => 'inside'
102
+ # "C".constantize # => 'outside', same as ::C
103
+ # end
104
+ #
105
+ # NameError is raised when the name is not in CamelCase or the constant is
106
+ # unknown.
107
+ def constantize(camel_cased_word)
108
+ names = camel_cased_word.split('::')
109
+ names.shift if names.empty? || names.first.empty?
110
+
111
+ constant = Object
112
+ names.each do |name|
113
+ constant = constant.const_defined?(name) ? constant.const_get(name) : constant.const_missing(name)
114
+ end
115
+ constant
116
+ end
117
+ else
118
+ def constantize(camel_cased_word) #:nodoc:
119
+ names = camel_cased_word.split('::')
120
+ names.shift if names.empty? || names.first.empty?
121
+
122
+ constant = Object
123
+ names.each do |name|
124
+ constant = constant.const_defined?(name, false) ? constant.const_get(name) : constant.const_missing(name)
125
+ end
126
+ constant
127
+ end
128
+ end
129
+
130
+ # Turns a number into an ordinal string used to denote the position in an
131
+ # ordered sequence such as 1st, 2nd, 3rd, 4th.
132
+ #
133
+ # Examples:
134
+ # ordinalize(1) # => "1st"
135
+ # ordinalize(2) # => "2nd"
136
+ # ordinalize(1002) # => "1002nd"
137
+ # ordinalize(1003) # => "1003rd"
138
+ def ordinalize(number)
139
+ if (11..13).include?(number.to_i % 100)
140
+ "#{number}th"
141
+ else
142
+ case number.to_i % 10
143
+ when 1; "#{number}st"
144
+ when 2; "#{number}nd"
145
+ when 3; "#{number}rd"
146
+ else "#{number}th"
147
+ end
148
+ end
149
+ end
150
+ end
151
+ end
@@ -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