colincasey-sequel 2.10.0 → 2.10.1
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +7 -1
- data/doc/advanced_associations.rdoc +614 -0
- data/doc/cheat_sheet.rdoc +223 -0
- data/doc/dataset_filtering.rdoc +158 -0
- data/doc/prepared_statements.rdoc +104 -0
- data/doc/release_notes/1.0.txt +38 -0
- data/doc/release_notes/1.1.txt +143 -0
- data/doc/release_notes/1.3.txt +101 -0
- data/doc/release_notes/1.4.0.txt +53 -0
- data/doc/release_notes/1.5.0.txt +155 -0
- data/doc/release_notes/2.0.0.txt +298 -0
- data/doc/release_notes/2.1.0.txt +271 -0
- data/doc/release_notes/2.10.0.txt +328 -0
- data/doc/release_notes/2.2.0.txt +253 -0
- data/doc/release_notes/2.3.0.txt +88 -0
- data/doc/release_notes/2.4.0.txt +106 -0
- data/doc/release_notes/2.5.0.txt +137 -0
- data/doc/release_notes/2.6.0.txt +157 -0
- data/doc/release_notes/2.7.0.txt +166 -0
- data/doc/release_notes/2.8.0.txt +171 -0
- data/doc/release_notes/2.9.0.txt +97 -0
- data/doc/schema.rdoc +29 -0
- data/doc/sharding.rdoc +113 -0
- data/lib/sequel.rb +1 -0
- data/lib/sequel_core/adapters/ado.rb +89 -0
- data/lib/sequel_core/adapters/db2.rb +143 -0
- data/lib/sequel_core/adapters/dbi.rb +112 -0
- data/lib/sequel_core/adapters/do/mysql.rb +38 -0
- data/lib/sequel_core/adapters/do/postgres.rb +92 -0
- data/lib/sequel_core/adapters/do/sqlite.rb +31 -0
- data/lib/sequel_core/adapters/do.rb +205 -0
- data/lib/sequel_core/adapters/firebird.rb +298 -0
- data/lib/sequel_core/adapters/informix.rb +85 -0
- data/lib/sequel_core/adapters/jdbc/h2.rb +69 -0
- data/lib/sequel_core/adapters/jdbc/mysql.rb +66 -0
- data/lib/sequel_core/adapters/jdbc/oracle.rb +23 -0
- data/lib/sequel_core/adapters/jdbc/postgresql.rb +113 -0
- data/lib/sequel_core/adapters/jdbc/sqlite.rb +43 -0
- data/lib/sequel_core/adapters/jdbc.rb +491 -0
- data/lib/sequel_core/adapters/mysql.rb +369 -0
- data/lib/sequel_core/adapters/odbc.rb +174 -0
- data/lib/sequel_core/adapters/openbase.rb +68 -0
- data/lib/sequel_core/adapters/oracle.rb +107 -0
- data/lib/sequel_core/adapters/postgres.rb +456 -0
- data/lib/sequel_core/adapters/shared/ms_access.rb +110 -0
- data/lib/sequel_core/adapters/shared/mssql.rb +102 -0
- data/lib/sequel_core/adapters/shared/mysql.rb +325 -0
- data/lib/sequel_core/adapters/shared/oracle.rb +61 -0
- data/lib/sequel_core/adapters/shared/postgres.rb +715 -0
- data/lib/sequel_core/adapters/shared/progress.rb +31 -0
- data/lib/sequel_core/adapters/shared/sqlite.rb +265 -0
- data/lib/sequel_core/adapters/sqlite.rb +248 -0
- data/lib/sequel_core/connection_pool.rb +258 -0
- data/lib/sequel_core/core_ext.rb +217 -0
- data/lib/sequel_core/core_sql.rb +202 -0
- data/lib/sequel_core/database/schema.rb +164 -0
- data/lib/sequel_core/database.rb +691 -0
- data/lib/sequel_core/dataset/callback.rb +13 -0
- data/lib/sequel_core/dataset/convenience.rb +237 -0
- data/lib/sequel_core/dataset/pagination.rb +96 -0
- data/lib/sequel_core/dataset/prepared_statements.rb +220 -0
- data/lib/sequel_core/dataset/query.rb +41 -0
- data/lib/sequel_core/dataset/schema.rb +15 -0
- data/lib/sequel_core/dataset/sql.rb +1010 -0
- data/lib/sequel_core/dataset/stored_procedures.rb +75 -0
- data/lib/sequel_core/dataset/unsupported.rb +43 -0
- data/lib/sequel_core/dataset.rb +511 -0
- data/lib/sequel_core/deprecated.rb +26 -0
- data/lib/sequel_core/exceptions.rb +44 -0
- data/lib/sequel_core/migration.rb +212 -0
- data/lib/sequel_core/object_graph.rb +230 -0
- data/lib/sequel_core/pretty_table.rb +71 -0
- data/lib/sequel_core/schema/generator.rb +320 -0
- data/lib/sequel_core/schema/sql.rb +325 -0
- data/lib/sequel_core/schema.rb +2 -0
- data/lib/sequel_core/sql.rb +887 -0
- data/lib/sequel_core/version.rb +11 -0
- data/lib/sequel_core.rb +172 -0
- data/lib/sequel_model/association_reflection.rb +267 -0
- data/lib/sequel_model/associations.rb +499 -0
- data/lib/sequel_model/base.rb +523 -0
- data/lib/sequel_model/caching.rb +82 -0
- data/lib/sequel_model/dataset_methods.rb +26 -0
- data/lib/sequel_model/eager_loading.rb +370 -0
- data/lib/sequel_model/exceptions.rb +7 -0
- data/lib/sequel_model/hooks.rb +101 -0
- data/lib/sequel_model/inflector.rb +281 -0
- data/lib/sequel_model/plugins.rb +62 -0
- data/lib/sequel_model/record.rb +568 -0
- data/lib/sequel_model/schema.rb +49 -0
- data/lib/sequel_model/validations.rb +429 -0
- data/lib/sequel_model.rb +91 -0
- data/spec/adapters/ado_spec.rb +46 -0
- data/spec/adapters/firebird_spec.rb +376 -0
- data/spec/adapters/informix_spec.rb +96 -0
- data/spec/adapters/mysql_spec.rb +881 -0
- data/spec/adapters/oracle_spec.rb +244 -0
- data/spec/adapters/postgres_spec.rb +687 -0
- data/spec/adapters/spec_helper.rb +10 -0
- data/spec/adapters/sqlite_spec.rb +555 -0
- data/spec/integration/dataset_test.rb +134 -0
- data/spec/integration/eager_loader_test.rb +696 -0
- data/spec/integration/prepared_statement_test.rb +130 -0
- data/spec/integration/schema_test.rb +180 -0
- data/spec/integration/spec_helper.rb +58 -0
- data/spec/integration/type_test.rb +96 -0
- data/spec/rcov.opts +6 -0
- data/spec/sequel_core/connection_pool_spec.rb +526 -0
- data/spec/sequel_core/core_ext_spec.rb +156 -0
- data/spec/sequel_core/core_sql_spec.rb +522 -0
- data/spec/sequel_core/database_spec.rb +1188 -0
- data/spec/sequel_core/dataset_spec.rb +3481 -0
- data/spec/sequel_core/expression_filters_spec.rb +363 -0
- data/spec/sequel_core/migration_spec.rb +261 -0
- data/spec/sequel_core/object_graph_spec.rb +272 -0
- data/spec/sequel_core/pretty_table_spec.rb +58 -0
- data/spec/sequel_core/schema_generator_spec.rb +167 -0
- data/spec/sequel_core/schema_spec.rb +780 -0
- data/spec/sequel_core/spec_helper.rb +55 -0
- data/spec/sequel_core/version_spec.rb +7 -0
- data/spec/sequel_model/association_reflection_spec.rb +93 -0
- data/spec/sequel_model/associations_spec.rb +1767 -0
- data/spec/sequel_model/base_spec.rb +419 -0
- data/spec/sequel_model/caching_spec.rb +215 -0
- data/spec/sequel_model/dataset_methods_spec.rb +78 -0
- data/spec/sequel_model/eager_loading_spec.rb +1165 -0
- data/spec/sequel_model/hooks_spec.rb +485 -0
- data/spec/sequel_model/inflector_spec.rb +119 -0
- data/spec/sequel_model/model_spec.rb +588 -0
- data/spec/sequel_model/plugins_spec.rb +80 -0
- data/spec/sequel_model/record_spec.rb +1184 -0
- data/spec/sequel_model/schema_spec.rb +90 -0
- data/spec/sequel_model/spec_helper.rb +78 -0
- data/spec/sequel_model/validations_spec.rb +1067 -0
- data/spec/spec.opts +0 -0
- data/spec/spec_config.rb.example +10 -0
- metadata +177 -3
@@ -0,0 +1,281 @@
|
|
1
|
+
# Add inflection methods to String, which allows the easy transformation of
|
2
|
+
# words from singular to plural,class names to table names, modularized class
|
3
|
+
# names to ones without, and class names to foreign keys.
|
4
|
+
|
5
|
+
class String
|
6
|
+
# This module acts as a singleton returned/yielded by String.inflections,
|
7
|
+
# which is used to override or specify additional inflection rules. Examples:
|
8
|
+
#
|
9
|
+
# String.inflections do |inflect|
|
10
|
+
# inflect.plural /^(ox)$/i, '\1\2en'
|
11
|
+
# inflect.singular /^(ox)en/i, '\1'
|
12
|
+
#
|
13
|
+
# inflect.irregular 'octopus', 'octopi'
|
14
|
+
#
|
15
|
+
# inflect.uncountable "equipment"
|
16
|
+
# end
|
17
|
+
#
|
18
|
+
# New rules are added at the top. So in the example above, the irregular rule for octopus will now be the first of the
|
19
|
+
# pluralization and singularization rules that is runs. This guarantees that your rules run before any of the rules that may
|
20
|
+
# already have been loaded.
|
21
|
+
module Inflections
|
22
|
+
@plurals, @singulars, @uncountables = [], [], []
|
23
|
+
|
24
|
+
metaattr_reader :plurals, :singulars, :uncountables
|
25
|
+
|
26
|
+
# Clears the loaded inflections within a given scope (default is :all). Give the scope as a symbol of the inflection type,
|
27
|
+
# the options are: :plurals, :singulars, :uncountables
|
28
|
+
#
|
29
|
+
# Examples:
|
30
|
+
# clear :all
|
31
|
+
# clear :plurals
|
32
|
+
def self.clear(scope = :all)
|
33
|
+
case scope
|
34
|
+
when :all
|
35
|
+
@plurals, @singulars, @uncountables = [], [], []
|
36
|
+
else
|
37
|
+
instance_variable_set("@#{scope}", [])
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
# Specifies a new irregular that applies to both pluralization and singularization at the same time. This can only be used
|
42
|
+
# for strings, not regular expressions. You simply pass the irregular in singular and plural form.
|
43
|
+
#
|
44
|
+
# Examples:
|
45
|
+
# irregular 'octopus', 'octopi'
|
46
|
+
# irregular 'person', 'people'
|
47
|
+
def self.irregular(singular, plural)
|
48
|
+
plural(Regexp.new("(#{singular[0,1]})#{singular[1..-1]}$", "i"), '\1' + plural[1..-1])
|
49
|
+
singular(Regexp.new("(#{plural[0,1]})#{plural[1..-1]}$", "i"), '\1' + singular[1..-1])
|
50
|
+
end
|
51
|
+
|
52
|
+
# Specifies a new pluralization rule and its replacement. The rule can either be a string or a regular expression.
|
53
|
+
# The replacement should always be a string that may include references to the matched data from the rule.
|
54
|
+
#
|
55
|
+
# Example:
|
56
|
+
# plural(/(x|ch|ss|sh)$/i, '\1es')
|
57
|
+
def self.plural(rule, replacement)
|
58
|
+
@plurals.insert(0, [rule, replacement])
|
59
|
+
end
|
60
|
+
|
61
|
+
# Specifies a new singularization rule and its replacement. The rule can either be a string or a regular expression.
|
62
|
+
# The replacement should always be a string that may include references to the matched data from the rule.
|
63
|
+
#
|
64
|
+
# Example:
|
65
|
+
# singular(/([^aeiouy]|qu)ies$/i, '\1y')
|
66
|
+
def self.singular(rule, replacement)
|
67
|
+
@singulars.insert(0, [rule, replacement])
|
68
|
+
end
|
69
|
+
|
70
|
+
# Add uncountable words that shouldn't be attempted inflected.
|
71
|
+
#
|
72
|
+
# Examples:
|
73
|
+
# uncountable "money"
|
74
|
+
# uncountable "money", "information"
|
75
|
+
# uncountable %w( money information rice )
|
76
|
+
def self.uncountable(*words)
|
77
|
+
(@uncountables << words).flatten!
|
78
|
+
end
|
79
|
+
|
80
|
+
# Setup the default inflections
|
81
|
+
plural(/$/, 's')
|
82
|
+
plural(/s$/i, 's')
|
83
|
+
plural(/(ax|test)is$/i, '\1es')
|
84
|
+
plural(/(octop|vir)us$/i, '\1i')
|
85
|
+
plural(/(alias|status)$/i, '\1es')
|
86
|
+
plural(/(bu)s$/i, '\1ses')
|
87
|
+
plural(/(buffal|tomat)o$/i, '\1oes')
|
88
|
+
plural(/([ti])um$/i, '\1a')
|
89
|
+
plural(/sis$/i, 'ses')
|
90
|
+
plural(/(?:([^f])fe|([lr])f)$/i, '\1\2ves')
|
91
|
+
plural(/(hive)$/i, '\1s')
|
92
|
+
plural(/([^aeiouy]|qu)y$/i, '\1ies')
|
93
|
+
plural(/(x|ch|ss|sh)$/i, '\1es')
|
94
|
+
plural(/(matr|vert|ind)ix|ex$/i, '\1ices')
|
95
|
+
plural(/([m|l])ouse$/i, '\1ice')
|
96
|
+
plural(/^(ox)$/i, '\1en')
|
97
|
+
plural(/(quiz)$/i, '\1zes')
|
98
|
+
|
99
|
+
singular(/s$/i, '')
|
100
|
+
singular(/(n)ews$/i, '\1ews')
|
101
|
+
singular(/([ti])a$/i, '\1um')
|
102
|
+
singular(/((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/i, '\1\2sis')
|
103
|
+
singular(/(^analy)ses$/i, '\1sis')
|
104
|
+
singular(/([^f])ves$/i, '\1fe')
|
105
|
+
singular(/(hive)s$/i, '\1')
|
106
|
+
singular(/(tive)s$/i, '\1')
|
107
|
+
singular(/([lr])ves$/i, '\1f')
|
108
|
+
singular(/([^aeiouy]|qu)ies$/i, '\1y')
|
109
|
+
singular(/(s)eries$/i, '\1eries')
|
110
|
+
singular(/(m)ovies$/i, '\1ovie')
|
111
|
+
singular(/(x|ch|ss|sh)es$/i, '\1')
|
112
|
+
singular(/([m|l])ice$/i, '\1ouse')
|
113
|
+
singular(/(bus)es$/i, '\1')
|
114
|
+
singular(/(o)es$/i, '\1')
|
115
|
+
singular(/(shoe)s$/i, '\1')
|
116
|
+
singular(/(cris|ax|test)es$/i, '\1is')
|
117
|
+
singular(/(octop|vir)i$/i, '\1us')
|
118
|
+
singular(/(alias|status)es$/i, '\1')
|
119
|
+
singular(/^(ox)en/i, '\1')
|
120
|
+
singular(/(vert|ind)ices$/i, '\1ex')
|
121
|
+
singular(/(matr)ices$/i, '\1ix')
|
122
|
+
singular(/(quiz)zes$/i, '\1')
|
123
|
+
|
124
|
+
irregular('person', 'people')
|
125
|
+
irregular('man', 'men')
|
126
|
+
irregular('child', 'children')
|
127
|
+
irregular('sex', 'sexes')
|
128
|
+
irregular('move', 'moves')
|
129
|
+
|
130
|
+
uncountable(%w(equipment information rice money species series fish sheep))
|
131
|
+
end
|
132
|
+
|
133
|
+
# Yield the Inflections module if a block is given, and return
|
134
|
+
# the Inflections module.
|
135
|
+
def self.inflections
|
136
|
+
yield Inflections if block_given?
|
137
|
+
Inflections
|
138
|
+
end
|
139
|
+
|
140
|
+
# By default, camelize converts the string to UpperCamelCase. If the argument to camelize
|
141
|
+
# is set to :lower then camelize produces lowerCamelCase.
|
142
|
+
#
|
143
|
+
# camelize will also convert '/' to '::' which is useful for converting paths to namespaces
|
144
|
+
#
|
145
|
+
# Examples
|
146
|
+
# "active_record".camelize #=> "ActiveRecord"
|
147
|
+
# "active_record".camelize(:lower) #=> "activeRecord"
|
148
|
+
# "active_record/errors".camelize #=> "ActiveRecord::Errors"
|
149
|
+
# "active_record/errors".camelize(:lower) #=> "activeRecord::Errors"
|
150
|
+
def camelize(first_letter_in_uppercase = :upper)
|
151
|
+
s = gsub(/\/(.?)/){|x| "::#{x[-1..-1].upcase unless x == '/'}"}.gsub(/(^|_)(.)/){|x| x[-1..-1].upcase}
|
152
|
+
s[0...1] = s[0...1].downcase unless first_letter_in_uppercase == :upper
|
153
|
+
s
|
154
|
+
end
|
155
|
+
alias_method :camelcase, :camelize
|
156
|
+
|
157
|
+
# Singularizes and camelizes the string. Also strips out all characters preceding
|
158
|
+
# and including a period (".").
|
159
|
+
#
|
160
|
+
# Examples
|
161
|
+
# "egg_and_hams".classify #=> "EggAndHam"
|
162
|
+
# "post".classify #=> "Post"
|
163
|
+
# "schema.post".classify #=> "Post"
|
164
|
+
def classify
|
165
|
+
sub(/.*\./, '').singularize.camelize
|
166
|
+
end
|
167
|
+
|
168
|
+
# Constantize tries to find a declared constant with the name specified
|
169
|
+
# in the string. It raises a NameError when the name is not in CamelCase
|
170
|
+
# or is not initialized.
|
171
|
+
#
|
172
|
+
# Examples
|
173
|
+
# "Module".constantize #=> Module
|
174
|
+
# "Class".constantize #=> Class
|
175
|
+
def constantize
|
176
|
+
raise(NameError, "#{inspect} is not a valid constant name!") unless m = /\A(?:::)?([A-Z]\w*(?:::[A-Z]\w*)*)\z/.match(self)
|
177
|
+
Object.module_eval("::#{m[1]}", __FILE__, __LINE__)
|
178
|
+
end
|
179
|
+
|
180
|
+
# Replaces underscores with dashes in the string.
|
181
|
+
#
|
182
|
+
# Example
|
183
|
+
# "puni_puni".dasherize #=> "puni-puni"
|
184
|
+
def dasherize
|
185
|
+
gsub(/_/, '-')
|
186
|
+
end
|
187
|
+
|
188
|
+
# Removes the module part from the expression in the string
|
189
|
+
#
|
190
|
+
# Examples
|
191
|
+
# "ActiveRecord::CoreExtensions::String::Inflections".demodulize #=> "Inflections"
|
192
|
+
# "Inflections".demodulize #=> "Inflections"
|
193
|
+
def demodulize
|
194
|
+
gsub(/^.*::/, '')
|
195
|
+
end
|
196
|
+
|
197
|
+
# Creates a foreign key name from a class name.
|
198
|
+
# +use_underscore+ sets whether the method should put '_' between the name and 'id'.
|
199
|
+
#
|
200
|
+
# Examples
|
201
|
+
# "Message".foreign_key #=> "message_id"
|
202
|
+
# "Message".foreign_key(false) #=> "messageid"
|
203
|
+
# "Admin::Post".foreign_key #=> "post_id"
|
204
|
+
def foreign_key(use_underscore = true)
|
205
|
+
"#{demodulize.underscore}#{'_' if use_underscore}id"
|
206
|
+
end
|
207
|
+
|
208
|
+
# Capitalizes the first word and turns underscores into spaces and strips _id.
|
209
|
+
# Like titleize, this is meant for creating pretty output.
|
210
|
+
#
|
211
|
+
# Examples
|
212
|
+
# "employee_salary" #=> "Employee salary"
|
213
|
+
# "author_id" #=> "Author"
|
214
|
+
def humanize
|
215
|
+
gsub(/_id$/, "").gsub(/_/, " ").capitalize
|
216
|
+
end
|
217
|
+
|
218
|
+
# Returns the plural form of the word in the string.
|
219
|
+
#
|
220
|
+
# Examples
|
221
|
+
# "post".pluralize #=> "posts"
|
222
|
+
# "octopus".pluralize #=> "octopi"
|
223
|
+
# "sheep".pluralize #=> "sheep"
|
224
|
+
# "words".pluralize #=> "words"
|
225
|
+
# "the blue mailman".pluralize #=> "the blue mailmen"
|
226
|
+
# "CamelOctopus".pluralize #=> "CamelOctopi"
|
227
|
+
def pluralize
|
228
|
+
result = dup
|
229
|
+
Inflections.plurals.each{|(rule, replacement)| break if result.gsub!(rule, replacement)} unless Inflections.uncountables.include?(downcase)
|
230
|
+
result
|
231
|
+
end
|
232
|
+
|
233
|
+
# The reverse of pluralize, returns the singular form of a word in a string.
|
234
|
+
#
|
235
|
+
# Examples
|
236
|
+
# "posts".singularize #=> "post"
|
237
|
+
# "octopi".singularize #=> "octopus"
|
238
|
+
# "sheep".singluarize #=> "sheep"
|
239
|
+
# "word".singluarize #=> "word"
|
240
|
+
# "the blue mailmen".singularize #=> "the blue mailman"
|
241
|
+
# "CamelOctopi".singularize #=> "CamelOctopus"
|
242
|
+
def singularize
|
243
|
+
result = dup
|
244
|
+
Inflections.singulars.each{|(rule, replacement)| break if result.gsub!(rule, replacement)} unless Inflections.uncountables.include?(downcase)
|
245
|
+
result
|
246
|
+
end
|
247
|
+
|
248
|
+
# Underscores and pluralizes the string.
|
249
|
+
#
|
250
|
+
# Examples
|
251
|
+
# "RawScaledScorer".tableize #=> "raw_scaled_scorers"
|
252
|
+
# "egg_and_ham".tableize #=> "egg_and_hams"
|
253
|
+
# "fancyCategory".tableize #=> "fancy_categories"
|
254
|
+
def tableize
|
255
|
+
underscore.pluralize
|
256
|
+
end
|
257
|
+
|
258
|
+
# Capitalizes all the words and replaces some characters in the string to create
|
259
|
+
# a nicer looking title. Titleize is meant for creating pretty output.
|
260
|
+
#
|
261
|
+
# titleize is also aliased as as titlecase
|
262
|
+
#
|
263
|
+
# Examples
|
264
|
+
# "man from the boondocks".titleize #=> "Man From The Boondocks"
|
265
|
+
# "x-men: the last stand".titleize #=> "X Men: The Last Stand"
|
266
|
+
def titleize
|
267
|
+
underscore.humanize.gsub(/\b([a-z])/){|x| x[-1..-1].upcase}
|
268
|
+
end
|
269
|
+
alias_method :titlecase, :titleize
|
270
|
+
|
271
|
+
# The reverse of camelize. Makes an underscored form from the expression in the string.
|
272
|
+
# Also changes '::' to '/' to convert namespaces to paths.
|
273
|
+
#
|
274
|
+
# Examples
|
275
|
+
# "ActiveRecord".underscore #=> "active_record"
|
276
|
+
# "ActiveRecord::Errors".underscore #=> active_record/errors
|
277
|
+
def underscore
|
278
|
+
gsub(/::/, '/').gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
|
279
|
+
gsub(/([a-z\d])([A-Z])/,'\1_\2').tr("-", "_").downcase
|
280
|
+
end
|
281
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
module Sequel
|
2
|
+
# Empty namespace that plugins should use to store themselves,
|
3
|
+
# so they can be loaded via Model.is.
|
4
|
+
#
|
5
|
+
# Plugins should be modules with one of the following conditions:
|
6
|
+
# * A singleton method named apply, which takes a model and
|
7
|
+
# additional arguments.
|
8
|
+
# * A module inside the plugin module named InstanceMethods,
|
9
|
+
# which will be included in the model class.
|
10
|
+
# * A module inside the plugin module named ClassMethods,
|
11
|
+
# which will extend the model class.
|
12
|
+
# * A module inside the plugin module named DatasetMethods,
|
13
|
+
# which will extend the model's dataset.
|
14
|
+
module Plugins
|
15
|
+
end
|
16
|
+
|
17
|
+
class Model
|
18
|
+
# Loads a plugin for use with the model class, passing optional arguments
|
19
|
+
# to the plugin. If the plugin has a DatasetMethods module and the model
|
20
|
+
# doesn't have a dataset, raise an Error.
|
21
|
+
def self.is(plugin, *args)
|
22
|
+
m = plugin_module(plugin)
|
23
|
+
raise(Error, "Plugin cannot be applied because the model class has no dataset") if m.const_defined?("DatasetMethods") && !@dataset
|
24
|
+
if m.respond_to?(:apply)
|
25
|
+
m.apply(self, *args)
|
26
|
+
end
|
27
|
+
if m.const_defined?("InstanceMethods")
|
28
|
+
class_def(:"#{plugin}_opts") {args.first}
|
29
|
+
include(m::InstanceMethods)
|
30
|
+
end
|
31
|
+
if m.const_defined?("ClassMethods")
|
32
|
+
meta_def(:"#{plugin}_opts") {args.first}
|
33
|
+
extend(m::ClassMethods)
|
34
|
+
end
|
35
|
+
if m.const_defined?("DatasetMethods")
|
36
|
+
dataset.meta_def(:"#{plugin}_opts") {args.first}
|
37
|
+
dataset.extend(m::DatasetMethods)
|
38
|
+
def_dataset_method(*m::DatasetMethods.public_instance_methods)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
metaalias :is_a, :is
|
42
|
+
|
43
|
+
### Private Class Methods ###
|
44
|
+
|
45
|
+
# Returns the gem name for the given plugin.
|
46
|
+
def self.plugin_gem(plugin) # :nodoc:
|
47
|
+
"sequel_#{plugin}"
|
48
|
+
end
|
49
|
+
|
50
|
+
# Returns the module for the specified plugin. If the module is not
|
51
|
+
# defined, the corresponding plugin gem is automatically loaded.
|
52
|
+
def self.plugin_module(plugin) # :nodoc:
|
53
|
+
module_name = plugin.to_s.gsub(/(^|_)(.)/){|x| x[-1..-1].upcase}
|
54
|
+
if not Sequel::Plugins.const_defined?(module_name)
|
55
|
+
require plugin_gem(plugin)
|
56
|
+
end
|
57
|
+
Sequel::Plugins.const_get(module_name)
|
58
|
+
end
|
59
|
+
|
60
|
+
private_class_method :plugin_gem, :plugin_module
|
61
|
+
end
|
62
|
+
end
|