sequel 1.4.0 → 1.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -23,33 +23,37 @@ module Sequel
23
23
  end
24
24
  }
25
25
 
26
- def self.def_hook_method(m) #:nodoc:
27
- instance_eval(HOOK_METHOD_STR % [m.to_s, m.inspect])
28
- end
29
-
30
- HOOKS.each {|h| define_method(h) {}}
31
- HOOKS.each {|h| def_hook_method(h)}
32
-
33
- # Returns the hooks hash for the model class.
34
- def self.hooks #:nodoc:
35
- @hooks ||= Hash.new {|h, k| h[k] = []}
36
- end
37
-
38
- def self.add_hook(hook, &block) #:nodoc:
39
- chain = hooks[hook]
40
- chain << block
41
- define_method(hook) do
42
- return false if super == false
43
- chain.each {|h| break false if instance_eval(&h) == false}
26
+ class << self
27
+ # Returns true if the model class or any of its ancestors have defined
28
+ # hooks for the given hook key. Notice that this method cannot detect
29
+ # hooks defined using overridden methods.
30
+ def has_hooks?(key)
31
+ has = hooks[key] && !hooks[key].empty?
32
+ has || ((self != Model) && superclass.has_hooks?(key))
44
33
  end
34
+ private
35
+ def def_hook_method(m) #:nodoc:
36
+ instance_eval(HOOK_METHOD_STR % [m.to_s, m.inspect])
37
+ end
38
+
39
+ # Returns the hooks hash for the model class.
40
+ def hooks #:nodoc:
41
+ @hooks ||= Hash.new {|h, k| h[k] = []}
42
+ end
43
+
44
+ def add_hook(hook, &block) #:nodoc:
45
+ chain = hooks[hook]
46
+ chain << block
47
+ define_method(hook) do
48
+ return false if super == false
49
+ chain.each {|h| break false if instance_eval(&h) == false}
50
+ end
51
+ end
45
52
  end
46
53
 
47
- # Returns true if the model class or any of its ancestors have defined
48
- # hooks for the given hook key. Notice that this method cannot detect
49
- # hooks defined using overridden methods.
50
- def self.has_hooks?(key)
51
- has = hooks[key] && !hooks[key].empty?
52
- has || ((self != Model) && superclass.has_hooks?(key))
54
+ HOOKS.each do |h|
55
+ define_method(h){}
56
+ def_hook_method(h)
53
57
  end
54
58
  end
55
- end
59
+ end
@@ -0,0 +1,112 @@
1
+ Inflector.inflections do |inflect|
2
+ inflect.plural(/$/, 's')
3
+ inflect.plural(/s$/i, 's')
4
+ inflect.plural(/(ax|test)is$/i, '\1es')
5
+ inflect.plural(/(octop|vir)us$/i, '\1i')
6
+ inflect.plural(/(alias|status)$/i, '\1es')
7
+ inflect.plural(/(bu)s$/i, '\1ses')
8
+ inflect.plural(/(buffal|tomat)o$/i, '\1oes')
9
+ inflect.plural(/([ti])um$/i, '\1a')
10
+ inflect.plural(/sis$/i, 'ses')
11
+ inflect.plural(/(?:([^f])fe|([lr])f)$/i, '\1\2ves')
12
+ inflect.plural(/(hive)$/i, '\1s')
13
+ inflect.plural(/([^aeiouy]|qu)y$/i, '\1ies')
14
+ inflect.plural(/(x|ch|ss|sh)$/i, '\1es')
15
+ inflect.plural(/(matr|vert|ind)ix|ex$/i, '\1ices')
16
+ inflect.plural(/([m|l])ouse$/i, '\1ice')
17
+ inflect.plural(/^(ox)$/i, '\1en')
18
+ inflect.plural(/(quiz)$/i, '\1zes')
19
+
20
+ inflect.singular(/s$/i, '')
21
+ inflect.singular(/(n)ews$/i, '\1ews')
22
+ inflect.singular(/([ti])a$/i, '\1um')
23
+ inflect.singular(/((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/i, '\1\2sis')
24
+ inflect.singular(/(^analy)ses$/i, '\1sis')
25
+ inflect.singular(/([^f])ves$/i, '\1fe')
26
+ inflect.singular(/(hive)s$/i, '\1')
27
+ inflect.singular(/(tive)s$/i, '\1')
28
+ inflect.singular(/([lr])ves$/i, '\1f')
29
+ inflect.singular(/([^aeiouy]|qu)ies$/i, '\1y')
30
+ inflect.singular(/(s)eries$/i, '\1eries')
31
+ inflect.singular(/(m)ovies$/i, '\1ovie')
32
+ inflect.singular(/(x|ch|ss|sh)es$/i, '\1')
33
+ inflect.singular(/([m|l])ice$/i, '\1ouse')
34
+ inflect.singular(/(bus)es$/i, '\1')
35
+ inflect.singular(/(o)es$/i, '\1')
36
+ inflect.singular(/(shoe)s$/i, '\1')
37
+ inflect.singular(/(cris|ax|test)es$/i, '\1is')
38
+ inflect.singular(/(octop|vir)i$/i, '\1us')
39
+ inflect.singular(/(alias|status)es$/i, '\1')
40
+ inflect.singular(/^(ox)en/i, '\1')
41
+ inflect.singular(/(vert|ind)ices$/i, '\1ex')
42
+ inflect.singular(/(matr)ices$/i, '\1ix')
43
+ inflect.singular(/(quiz)zes$/i, '\1')
44
+
45
+ inflect.irregular('person', 'people')
46
+ inflect.irregular('man', 'men')
47
+ inflect.irregular('child', 'children')
48
+ inflect.irregular('sex', 'sexes')
49
+ inflect.irregular('move', 'moves')
50
+
51
+ inflect.uncountable(%w(equipment information rice money species series fish sheep))
52
+ end
53
+
54
+ module Inflections #:nodoc:
55
+
56
+ def pluralize
57
+ Inflector.pluralize(self)
58
+ end
59
+
60
+ def singularize
61
+ Inflector.singularize(self)
62
+ end
63
+
64
+ def camelize(first_letter = :upper)
65
+ case first_letter
66
+ when :upper then Inflector.camelize(self, true)
67
+ when :lower then Inflector.camelize(self, false)
68
+ end
69
+ end
70
+ alias_method :camelcase, :camelize
71
+
72
+ def titleize
73
+ Inflector.titleize(self)
74
+ end
75
+ alias_method :titlecase, :titleize
76
+
77
+ def underscore
78
+ Inflector.underscore(self)
79
+ end
80
+
81
+ def dasherize
82
+ Inflector.dasherize(self)
83
+ end
84
+
85
+ def demodulize
86
+ Inflector.demodulize(self)
87
+ end
88
+
89
+ def tableize
90
+ Inflector.tableize(self)
91
+ end
92
+
93
+ def classify
94
+ Inflector.classify(self)
95
+ end
96
+
97
+ def humanize
98
+ Inflector.humanize(self)
99
+ end
100
+
101
+ def foreign_key(separate_class_name_and_id_with_underscore = true)
102
+ Inflector.foreign_key(self, separate_class_name_and_id_with_underscore)
103
+ end
104
+
105
+ def constantize
106
+ Inflector.constantize(self)
107
+ end
108
+ end
109
+
110
+ class String
111
+ include Inflections
112
+ end
@@ -0,0 +1,279 @@
1
+ require 'singleton'
2
+
3
+ # The Inflector transforms words from singular to plural, class names to table names, modularized class names to ones without,
4
+ # and class names to foreign keys. The default inflections for pluralization, singularization, and uncountable words are kept
5
+ # in inflections.rb.
6
+ module Inflector
7
+ # A singleton instance of this class is yielded by Inflector.inflections, which can then be used to specify additional
8
+ # inflection rules. Examples:
9
+ #
10
+ # Inflector.inflections do |inflect|
11
+ # inflect.plural /^(ox)$/i, '\1\2en'
12
+ # inflect.singular /^(ox)en/i, '\1'
13
+ #
14
+ # inflect.irregular 'octopus', 'octopi'
15
+ #
16
+ # inflect.uncountable "equipment"
17
+ # end
18
+ #
19
+ # New rules are added at the top. So in the example above, the irregular rule for octopus will now be the first of the
20
+ # pluralization and singularization rules that is runs. This guarantees that your rules run before any of the rules that may
21
+ # already have been loaded.
22
+ class Inflections
23
+ include Singleton
24
+
25
+ attr_reader :plurals, :singulars, :uncountables
26
+
27
+ def initialize
28
+ @plurals, @singulars, @uncountables = [], [], []
29
+ end
30
+
31
+ # Specifies a new pluralization rule and its replacement. The rule can either be a string or a regular expression.
32
+ # The replacement should always be a string that may include references to the matched data from the rule.
33
+ def plural(rule, 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
+ @singulars.insert(0, [rule, replacement])
41
+ end
42
+
43
+ # Specifies a new irregular that applies to both pluralization and singularization at the same time. This can only be used
44
+ # for strings, not regular expressions. You simply pass the irregular in singular and plural form.
45
+ #
46
+ # Examples:
47
+ # irregular 'octopus', 'octopi'
48
+ # irregular 'person', 'people'
49
+ def irregular(singular, plural)
50
+ plural(Regexp.new("(#{singular[0,1]})#{singular[1..-1]}$", "i"), '\1' + plural[1..-1])
51
+ singular(Regexp.new("(#{plural[0,1]})#{plural[1..-1]}$", "i"), '\1' + singular[1..-1])
52
+ end
53
+
54
+ # Add uncountable words that shouldn't be attempted inflected.
55
+ #
56
+ # Examples:
57
+ # uncountable "money"
58
+ # uncountable "money", "information"
59
+ # uncountable %w( money information rice )
60
+ def uncountable(*words)
61
+ (@uncountables << words).flatten!
62
+ end
63
+
64
+ # Clears the loaded inflections within a given scope (default is :all). Give the scope as a symbol of the inflection type,
65
+ # the options are: :plurals, :singulars, :uncountables
66
+ #
67
+ # Examples:
68
+ # clear :all
69
+ # clear :plurals
70
+ def clear(scope = :all)
71
+ case scope
72
+ when :all
73
+ @plurals, @singulars, @uncountables = [], [], []
74
+ else
75
+ instance_variable_set "@#{scope}", []
76
+ end
77
+ end
78
+ end
79
+
80
+ extend self
81
+
82
+ def inflections
83
+ if block_given?
84
+ yield Inflections.instance
85
+ else
86
+ Inflections.instance
87
+ end
88
+ end
89
+
90
+ # Returns the plural form of the word in the string.
91
+ #
92
+ # Examples
93
+ # "post".pluralize #=> "posts"
94
+ # "octopus".pluralize #=> "octopi"
95
+ # "sheep".pluralize #=> "sheep"
96
+ # "words".pluralize #=> "words"
97
+ # "the blue mailman".pluralize #=> "the blue mailmen"
98
+ # "CamelOctopus".pluralize #=> "CamelOctopi"
99
+ def pluralize(word)
100
+ result = word.to_s.dup
101
+
102
+ if inflections.uncountables.include?(result.downcase)
103
+ result
104
+ else
105
+ inflections.plurals.each { |(rule, replacement)| break if result.gsub!(rule, replacement) }
106
+ result
107
+ end
108
+ end
109
+
110
+ # The reverse of pluralize, returns the singular form of a word in a string.
111
+ #
112
+ # Examples
113
+ # "posts".singularize #=> "post"
114
+ # "octopi".singularize #=> "octopus"
115
+ # "sheep".singluarize #=> "sheep"
116
+ # "word".singluarize #=> "word"
117
+ # "the blue mailmen".singularize #=> "the blue mailman"
118
+ # "CamelOctopi".singularize #=> "CamelOctopus"
119
+ def singularize(word)
120
+ result = word.to_s.dup
121
+
122
+ if inflections.uncountables.include?(result.downcase)
123
+ result
124
+ else
125
+ inflections.singulars.each { |(rule, replacement)| break if result.gsub!(rule, replacement) }
126
+ result
127
+ end
128
+ end
129
+
130
+ # By default, camelize converts strings to UpperCamelCase. If the argument to camelize
131
+ # is set to ":lower" then camelize produces lowerCamelCase.
132
+ #
133
+ # camelize will also convert '/' to '::' which is useful for converting paths to namespaces
134
+ #
135
+ # Examples
136
+ # "active_record".camelize #=> "ActiveRecord"
137
+ # "active_record".camelize(:lower) #=> "activeRecord"
138
+ # "active_record/errors".camelize #=> "ActiveRecord::Errors"
139
+ # "active_record/errors".camelize(:lower) #=> "activeRecord::Errors"
140
+ def camelize(lower_case_and_underscored_word, first_letter_in_uppercase = true)
141
+ if first_letter_in_uppercase
142
+ lower_case_and_underscored_word.to_s.gsub(/\/(.?)/) { "::" + $1.upcase }.gsub(/(^|_)(.)/) { $2.upcase }
143
+ else
144
+ lower_case_and_underscored_word.first + camelize(lower_case_and_underscored_word)[1..-1]
145
+ end
146
+ end
147
+
148
+ # Capitalizes all the words and replaces some characters in the string to create
149
+ # a nicer looking title. Titleize is meant for creating pretty output. It is not
150
+ # used in the Rails internals.
151
+ #
152
+ # titleize is also aliased as as titlecase
153
+ #
154
+ # Examples
155
+ # "man from the boondocks".titleize #=> "Man From The Boondocks"
156
+ # "x-men: the last stand".titleize #=> "X Men: The Last Stand"
157
+ def titleize(word)
158
+ humanize(underscore(word)).gsub(/\b([a-z])/) { $1.capitalize }
159
+ end
160
+
161
+ # The reverse of +camelize+. Makes an underscored form from the expression in the string.
162
+ #
163
+ # Changes '::' to '/' to convert namespaces to paths.
164
+ #
165
+ # Examples
166
+ # "ActiveRecord".underscore #=> "active_record"
167
+ # "ActiveRecord::Errors".underscore #=> active_record/errors
168
+ def underscore(camel_cased_word)
169
+ camel_cased_word.to_s.gsub(/::/, '/').
170
+ gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
171
+ gsub(/([a-z\d])([A-Z])/,'\1_\2').
172
+ tr("-", "_").
173
+ downcase
174
+ end
175
+
176
+ # Replaces underscores with dashes in the string.
177
+ #
178
+ # Example
179
+ # "puni_puni" #=> "puni-puni"
180
+ def dasherize(underscored_word)
181
+ underscored_word.gsub(/_/, '-')
182
+ end
183
+
184
+ # Capitalizes the first word and turns underscores into spaces and strips _id.
185
+ # Like titleize, this is meant for creating pretty output.
186
+ #
187
+ # Examples
188
+ # "employee_salary" #=> "Employee salary"
189
+ # "author_id" #=> "Author"
190
+ def humanize(lower_case_and_underscored_word)
191
+ lower_case_and_underscored_word.to_s.gsub(/_id$/, "").gsub(/_/, " ").capitalize
192
+ end
193
+
194
+ # Removes the module part from the expression in the string
195
+ #
196
+ # Examples
197
+ # "ActiveRecord::CoreExtensions::String::Inflections".demodulize #=> "Inflections"
198
+ # "Inflections".demodulize #=> "Inflections"
199
+ def demodulize(class_name_in_module)
200
+ class_name_in_module.to_s.gsub(/^.*::/, '')
201
+ end
202
+
203
+ # Create the name of a table like Rails does for models to table names. This method
204
+ # uses the pluralize method on the last word in the string.
205
+ #
206
+ # Examples
207
+ # "RawScaledScorer".tableize #=> "raw_scaled_scorers"
208
+ # "egg_and_ham".tableize #=> "egg_and_hams"
209
+ # "fancyCategory".tableize #=> "fancy_categories"
210
+ def tableize(class_name)
211
+ pluralize(underscore(class_name))
212
+ end
213
+
214
+ # Create a class name from a table name like Rails does for table names to models.
215
+ # Note that this returns a string and not a Class. (To convert to an actual class
216
+ # follow classify with constantize.)
217
+ #
218
+ # Examples
219
+ # "egg_and_hams".classify #=> "EggAndHam"
220
+ # "post".classify #=> "Post"
221
+ def classify(table_name)
222
+ # strip out any leading schema name
223
+ camelize(singularize(table_name.to_s.sub(/.*\./, '')))
224
+ end
225
+
226
+ # Creates a foreign key name from a class name.
227
+ # +separate_class_name_and_id_with_underscore+ sets whether
228
+ # the method should put '_' between the name and 'id'.
229
+ #
230
+ # Examples
231
+ # "Message".foreign_key #=> "message_id"
232
+ # "Message".foreign_key(false) #=> "messageid"
233
+ # "Admin::Post".foreign_key #=> "post_id"
234
+ def foreign_key(class_name, separate_class_name_and_id_with_underscore = true)
235
+ underscore(demodulize(class_name)) + (separate_class_name_and_id_with_underscore ? "_id" : "id")
236
+ end
237
+
238
+ # Constantize tries to find a declared constant with the name specified
239
+ # in the string. It raises a NameError when the name is not in CamelCase
240
+ # or is not initialized.
241
+ #
242
+ # Examples
243
+ # "Module".constantize #=> Module
244
+ # "Class".constantize #=> Class
245
+ def constantize(camel_cased_word)
246
+ unless /\A(?:::)?([A-Z]\w*(?:::[A-Z]\w*)*)\z/ =~ camel_cased_word
247
+ raise NameError, "#{camel_cased_word.inspect} is not a valid constant name!"
248
+ end
249
+
250
+ Object.module_eval("::#{$1}", __FILE__, __LINE__)
251
+ end
252
+
253
+ # Ordinalize turns a number into an ordinal string used to denote the
254
+ # position in an ordered sequence such as 1st, 2nd, 3rd, 4th.
255
+ #
256
+ # Examples
257
+ # ordinalize(1) # => "1st"
258
+ # ordinalize(2) # => "2nd"
259
+ # ordinalize(1002) # => "1002nd"
260
+ # ordinalize(1003) # => "1003rd"
261
+ def ordinalize(number)
262
+ if (11..13).include?(number.to_i % 100)
263
+ "#{number}th"
264
+ else
265
+ case number.to_i % 10
266
+ when 1
267
+ "#{number}st"
268
+ when 2
269
+ "#{number}nd"
270
+ when 3
271
+ "#{number}rd"
272
+ else
273
+ "#{number}th"
274
+ end
275
+ end
276
+ end
277
+ end
278
+
279
+ require File.dirname(__FILE__) + '/inflections'