abiquo-etk 0.6.0 → 0.6.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,170 @@
1
+ #
2
+ # Code from activesupport 2.3.8
3
+ #
4
+ require 'active_support/inflector' unless defined?(ActiveSupport::Inflector)
5
+
6
+ module ActiveSupport #:nodoc:
7
+ module CoreExtensions #:nodoc:
8
+ module String #:nodoc:
9
+ # String inflections define new methods on the String class to transform names for different purposes.
10
+ # For instance, you can figure out the name of a database from the name of a class.
11
+ #
12
+ # "ScaleScore".tableize # => "scale_scores"
13
+ module Inflections
14
+ # Returns the plural form of the word in the string.
15
+ #
16
+ # "post".pluralize # => "posts"
17
+ # "octopus".pluralize # => "octopi"
18
+ # "sheep".pluralize # => "sheep"
19
+ # "words".pluralize # => "words"
20
+ # "the blue mailman".pluralize # => "the blue mailmen"
21
+ # "CamelOctopus".pluralize # => "CamelOctopi"
22
+ def pluralize
23
+ Inflector.pluralize(self)
24
+ end
25
+
26
+ # The reverse of +pluralize+, returns the singular form of a word in a string.
27
+ #
28
+ # "posts".singularize # => "post"
29
+ # "octopi".singularize # => "octopus"
30
+ # "sheep".singularize # => "sheep"
31
+ # "word".singularize # => "word"
32
+ # "the blue mailmen".singularize # => "the blue mailman"
33
+ # "CamelOctopi".singularize # => "CamelOctopus"
34
+ def singularize
35
+ Inflector.singularize(self)
36
+ end
37
+
38
+ # By default, +camelize+ converts strings to UpperCamelCase. If the argument to camelize
39
+ # is set to <tt>:lower</tt> then camelize produces lowerCamelCase.
40
+ #
41
+ # +camelize+ will also convert '/' to '::' which is useful for converting paths to namespaces.
42
+ #
43
+ # "active_record".camelize # => "ActiveRecord"
44
+ # "active_record".camelize(:lower) # => "activeRecord"
45
+ # "active_record/errors".camelize # => "ActiveRecord::Errors"
46
+ # "active_record/errors".camelize(:lower) # => "activeRecord::Errors"
47
+ def camelize(first_letter = :upper)
48
+ case first_letter
49
+ when :upper then Inflector.camelize(self, true)
50
+ when :lower then Inflector.camelize(self, false)
51
+ end
52
+ end
53
+ alias_method :camelcase, :camelize
54
+
55
+ # Capitalizes all the words and replaces some characters in the string to create
56
+ # a nicer looking title. +titleize+ is meant for creating pretty output. It is not
57
+ # used in the Rails internals.
58
+ #
59
+ # +titleize+ is also aliased as +titlecase+.
60
+ #
61
+ # "man from the boondocks".titleize # => "Man From The Boondocks"
62
+ # "x-men: the last stand".titleize # => "X Men: The Last Stand"
63
+ def titleize
64
+ Inflector.titleize(self)
65
+ end
66
+ alias_method :titlecase, :titleize
67
+
68
+ # The reverse of +camelize+. Makes an underscored, lowercase form from the expression in the string.
69
+ #
70
+ # +underscore+ will also change '::' to '/' to convert namespaces to paths.
71
+ #
72
+ # "ActiveRecord".underscore # => "active_record"
73
+ # "ActiveRecord::Errors".underscore # => active_record/errors
74
+ def underscore
75
+ Inflector.underscore(self)
76
+ end
77
+
78
+ # Replaces underscores with dashes in the string.
79
+ #
80
+ # "puni_puni" # => "puni-puni"
81
+ def dasherize
82
+ Inflector.dasherize(self)
83
+ end
84
+
85
+ # Removes the module part from the constant expression in the string.
86
+ #
87
+ # "ActiveRecord::CoreExtensions::String::Inflections".demodulize # => "Inflections"
88
+ # "Inflections".demodulize # => "Inflections"
89
+ def demodulize
90
+ Inflector.demodulize(self)
91
+ end
92
+
93
+ # Replaces special characters in a string so that it may be used as part of a 'pretty' URL.
94
+ #
95
+ # ==== Examples
96
+ #
97
+ # class Person
98
+ # def to_param
99
+ # "#{id}-#{name.parameterize}"
100
+ # end
101
+ # end
102
+ #
103
+ # @person = Person.find(1)
104
+ # # => #<Person id: 1, name: "Donald E. Knuth">
105
+ #
106
+ # <%= link_to(@person.name, person_path %>
107
+ # # => <a href="/person/1-donald-e-knuth">Donald E. Knuth</a>
108
+ def parameterize(sep = '-')
109
+ Inflector.parameterize(self, sep)
110
+ end
111
+
112
+ # Creates the name of a table like Rails does for models to table names. This method
113
+ # uses the +pluralize+ method on the last word in the string.
114
+ #
115
+ # "RawScaledScorer".tableize # => "raw_scaled_scorers"
116
+ # "egg_and_ham".tableize # => "egg_and_hams"
117
+ # "fancyCategory".tableize # => "fancy_categories"
118
+ def tableize
119
+ Inflector.tableize(self)
120
+ end
121
+
122
+ # Create a class name from a plural table name like Rails does for table names to models.
123
+ # Note that this returns a string and not a class. (To convert to an actual class
124
+ # follow +classify+ with +constantize+.)
125
+ #
126
+ # "egg_and_hams".classify # => "EggAndHam"
127
+ # "posts".classify # => "Post"
128
+ #
129
+ # Singular names are not handled correctly.
130
+ #
131
+ # "business".classify # => "Busines"
132
+ def classify
133
+ Inflector.classify(self)
134
+ end
135
+
136
+ # Capitalizes the first word, turns underscores into spaces, and strips '_id'.
137
+ # Like +titleize+, this is meant for creating pretty output.
138
+ #
139
+ # "employee_salary" # => "Employee salary"
140
+ # "author_id" # => "Author"
141
+ def humanize
142
+ Inflector.humanize(self)
143
+ end
144
+
145
+ # Creates a foreign key name from a class name.
146
+ # +separate_class_name_and_id_with_underscore+ sets whether
147
+ # the method should put '_' between the name and 'id'.
148
+ #
149
+ # Examples
150
+ # "Message".foreign_key # => "message_id"
151
+ # "Message".foreign_key(false) # => "messageid"
152
+ # "Admin::Post".foreign_key # => "post_id"
153
+ def foreign_key(separate_class_name_and_id_with_underscore = true)
154
+ Inflector.foreign_key(self, separate_class_name_and_id_with_underscore)
155
+ end
156
+
157
+ # +constantize+ tries to find a declared constant with the name specified
158
+ # in the string. It raises a NameError when the name is not in CamelCase
159
+ # or is not initialized.
160
+ #
161
+ # Examples
162
+ # "Module".constantize # => Module
163
+ # "Class".constantize # => Class
164
+ def constantize
165
+ Inflector.constantize(self)
166
+ end
167
+ end
168
+ end
169
+ end
170
+ end
@@ -0,0 +1,411 @@
1
+ #
2
+ # Code from activesupport 2.3.8
3
+ #
4
+ # encoding: utf-8
5
+ require 'singleton'
6
+ require 'iconv'
7
+ require 'kconv'
8
+
9
+ module ActiveSupport
10
+ # The Inflector transforms words from singular to plural, class names to table names, modularized class names to ones without,
11
+ # and class names to foreign keys. The default inflections for pluralization, singularization, and uncountable words are kept
12
+ # in inflections.rb.
13
+ #
14
+ # The Rails core team has stated patches for the inflections library will not be accepted
15
+ # in order to avoid breaking legacy applications which may be relying on errant inflections.
16
+ # If you discover an incorrect inflection and require it for your application, you'll need
17
+ # to correct it yourself (explained below).
18
+ module Inflector
19
+ extend self
20
+
21
+ # A singleton instance of this class is yielded by Inflector.inflections, which can then be used to specify additional
22
+ # inflection rules. Examples:
23
+ #
24
+ # ActiveSupport::Inflector.inflections do |inflect|
25
+ # inflect.plural /^(ox)$/i, '\1\2en'
26
+ # inflect.singular /^(ox)en/i, '\1'
27
+ #
28
+ # inflect.irregular 'octopus', 'octopi'
29
+ #
30
+ # inflect.uncountable "equipment"
31
+ # end
32
+ #
33
+ # New rules are added at the top. So in the example above, the irregular rule for octopus will now be the first of the
34
+ # pluralization and singularization rules that is runs. This guarantees that your rules run before any of the rules that may
35
+ # already have been loaded.
36
+ class Inflections
37
+ include Singleton
38
+
39
+ attr_reader :plurals, :singulars, :uncountables, :humans
40
+
41
+ def initialize
42
+ @plurals, @singulars, @uncountables, @humans = [], [], [], []
43
+ end
44
+
45
+ # Specifies a new pluralization rule and its replacement. The rule can either be a string or a regular expression.
46
+ # The replacement should always be a string that may include references to the matched data from the rule.
47
+ def plural(rule, replacement)
48
+ @uncountables.delete(rule) if rule.is_a?(String)
49
+ @uncountables.delete(replacement)
50
+ @plurals.insert(0, [rule, replacement])
51
+ end
52
+
53
+ # Specifies a new singularization rule and its replacement. The rule can either be a string or a regular expression.
54
+ # The replacement should always be a string that may include references to the matched data from the rule.
55
+ def singular(rule, replacement)
56
+ @uncountables.delete(rule) if rule.is_a?(String)
57
+ @uncountables.delete(replacement)
58
+ @singulars.insert(0, [rule, replacement])
59
+ end
60
+
61
+ # Specifies a new irregular that applies to both pluralization and singularization at the same time. This can only be used
62
+ # for strings, not regular expressions. You simply pass the irregular in singular and plural form.
63
+ #
64
+ # Examples:
65
+ # irregular 'octopus', 'octopi'
66
+ # irregular 'person', 'people'
67
+ def irregular(singular, plural)
68
+ @uncountables.delete(singular)
69
+ @uncountables.delete(plural)
70
+ if singular[0,1].upcase == plural[0,1].upcase
71
+ plural(Regexp.new("(#{singular[0,1]})#{singular[1..-1]}$", "i"), '\1' + plural[1..-1])
72
+ singular(Regexp.new("(#{plural[0,1]})#{plural[1..-1]}$", "i"), '\1' + singular[1..-1])
73
+ else
74
+ plural(Regexp.new("#{singular[0,1].upcase}(?i)#{singular[1..-1]}$"), plural[0,1].upcase + plural[1..-1])
75
+ plural(Regexp.new("#{singular[0,1].downcase}(?i)#{singular[1..-1]}$"), plural[0,1].downcase + plural[1..-1])
76
+ singular(Regexp.new("#{plural[0,1].upcase}(?i)#{plural[1..-1]}$"), singular[0,1].upcase + singular[1..-1])
77
+ singular(Regexp.new("#{plural[0,1].downcase}(?i)#{plural[1..-1]}$"), singular[0,1].downcase + singular[1..-1])
78
+ end
79
+ end
80
+
81
+ # Add uncountable words that shouldn't be attempted inflected.
82
+ #
83
+ # Examples:
84
+ # uncountable "money"
85
+ # uncountable "money", "information"
86
+ # uncountable %w( money information rice )
87
+ def uncountable(*words)
88
+ (@uncountables << words).flatten!
89
+ end
90
+
91
+ # Specifies a humanized form of a string by a regular expression rule or by a string mapping.
92
+ # When using a regular expression based replacement, the normal humanize formatting is called after the replacement.
93
+ # When a string is used, the human form should be specified as desired (example: 'The name', not 'the_name')
94
+ #
95
+ # Examples:
96
+ # human /_cnt$/i, '\1_count'
97
+ # human "legacy_col_person_name", "Name"
98
+ def human(rule, replacement)
99
+ @humans.insert(0, [rule, replacement])
100
+ end
101
+
102
+ # Clears the loaded inflections within a given scope (default is <tt>:all</tt>).
103
+ # Give the scope as a symbol of the inflection type, the options are: <tt>:plurals</tt>,
104
+ # <tt>:singulars</tt>, <tt>:uncountables</tt>, <tt>:humans</tt>.
105
+ #
106
+ # Examples:
107
+ # clear :all
108
+ # clear :plurals
109
+ def clear(scope = :all)
110
+ case scope
111
+ when :all
112
+ @plurals, @singulars, @uncountables = [], [], []
113
+ else
114
+ instance_variable_set "@#{scope}", []
115
+ end
116
+ end
117
+ end
118
+
119
+ # Yields a singleton instance of Inflector::Inflections so you can specify additional
120
+ # inflector rules.
121
+ #
122
+ # Example:
123
+ # ActiveSupport::Inflector.inflections do |inflect|
124
+ # inflect.uncountable "rails"
125
+ # end
126
+ def inflections
127
+ if block_given?
128
+ yield Inflections.instance
129
+ else
130
+ Inflections.instance
131
+ end
132
+ end
133
+
134
+ # Returns the plural form of the word in the string.
135
+ #
136
+ # Examples:
137
+ # "post".pluralize # => "posts"
138
+ # "octopus".pluralize # => "octopi"
139
+ # "sheep".pluralize # => "sheep"
140
+ # "words".pluralize # => "words"
141
+ # "CamelOctopus".pluralize # => "CamelOctopi"
142
+ def pluralize(word)
143
+ result = word.to_s.dup
144
+
145
+ if word.empty? || inflections.uncountables.include?(result.downcase)
146
+ result
147
+ else
148
+ inflections.plurals.each { |(rule, replacement)| break if result.gsub!(rule, replacement) }
149
+ result
150
+ end
151
+ end
152
+
153
+ # The reverse of +pluralize+, returns the singular form of a word in a string.
154
+ #
155
+ # Examples:
156
+ # "posts".singularize # => "post"
157
+ # "octopi".singularize # => "octopus"
158
+ # "sheep".singluarize # => "sheep"
159
+ # "word".singularize # => "word"
160
+ # "CamelOctopi".singularize # => "CamelOctopus"
161
+ def singularize(word)
162
+ result = word.to_s.dup
163
+
164
+ if inflections.uncountables.any? { |inflection| result =~ /#{inflection}\Z/i }
165
+ result
166
+ else
167
+ inflections.singulars.each { |(rule, replacement)| break if result.gsub!(rule, replacement) }
168
+ result
169
+ end
170
+ end
171
+
172
+ # By default, +camelize+ converts strings to UpperCamelCase. If the argument to +camelize+
173
+ # is set to <tt>:lower</tt> then +camelize+ produces lowerCamelCase.
174
+ #
175
+ # +camelize+ will also convert '/' to '::' which is useful for converting paths to namespaces.
176
+ #
177
+ # Examples:
178
+ # "active_record".camelize # => "ActiveRecord"
179
+ # "active_record".camelize(:lower) # => "activeRecord"
180
+ # "active_record/errors".camelize # => "ActiveRecord::Errors"
181
+ # "active_record/errors".camelize(:lower) # => "activeRecord::Errors"
182
+ def camelize(lower_case_and_underscored_word, first_letter_in_uppercase = true)
183
+ if first_letter_in_uppercase
184
+ lower_case_and_underscored_word.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
185
+ else
186
+ lower_case_and_underscored_word.first.downcase + camelize(lower_case_and_underscored_word)[1..-1]
187
+ end
188
+ end
189
+
190
+ # Capitalizes all the words and replaces some characters in the string to create
191
+ # a nicer looking title. +titleize+ is meant for creating pretty output. It is not
192
+ # used in the Rails internals.
193
+ #
194
+ # +titleize+ is also aliased as as +titlecase+.
195
+ #
196
+ # Examples:
197
+ # "man from the boondocks".titleize # => "Man From The Boondocks"
198
+ # "x-men: the last stand".titleize # => "X Men: The Last Stand"
199
+ def titleize(word)
200
+ humanize(underscore(word)).gsub(/\b('?[a-z])/) { $1.capitalize }
201
+ end
202
+
203
+ # The reverse of +camelize+. Makes an underscored, lowercase form from the expression in the string.
204
+ #
205
+ # Changes '::' to '/' to convert namespaces to paths.
206
+ #
207
+ # Examples:
208
+ # "ActiveRecord".underscore # => "active_record"
209
+ # "ActiveRecord::Errors".underscore # => active_record/errors
210
+ def underscore(camel_cased_word)
211
+ camel_cased_word.to_s.gsub(/::/, '/').
212
+ gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
213
+ gsub(/([a-z\d])([A-Z])/,'\1_\2').
214
+ tr("-", "_").
215
+ downcase
216
+ end
217
+
218
+ # Replaces underscores with dashes in the string.
219
+ #
220
+ # Example:
221
+ # "puni_puni" # => "puni-puni"
222
+ def dasherize(underscored_word)
223
+ underscored_word.gsub(/_/, '-')
224
+ end
225
+
226
+ # Capitalizes the first word and turns underscores into spaces and strips a
227
+ # trailing "_id", if any. Like +titleize+, this is meant for creating pretty output.
228
+ #
229
+ # Examples:
230
+ # "employee_salary" # => "Employee salary"
231
+ # "author_id" # => "Author"
232
+ def humanize(lower_case_and_underscored_word)
233
+ result = lower_case_and_underscored_word.to_s.dup
234
+
235
+ inflections.humans.each { |(rule, replacement)| break if result.gsub!(rule, replacement) }
236
+ result.gsub(/_id$/, "").gsub(/_/, " ").capitalize
237
+ end
238
+
239
+ # Removes the module part from the expression in the string.
240
+ #
241
+ # Examples:
242
+ # "ActiveRecord::CoreExtensions::String::Inflections".demodulize # => "Inflections"
243
+ # "Inflections".demodulize # => "Inflections"
244
+ def demodulize(class_name_in_module)
245
+ class_name_in_module.to_s.gsub(/^.*::/, '')
246
+ end
247
+
248
+ # Replaces special characters in a string so that it may be used as part of a 'pretty' URL.
249
+ #
250
+ # ==== Examples
251
+ #
252
+ # class Person
253
+ # def to_param
254
+ # "#{id}-#{name.parameterize}"
255
+ # end
256
+ # end
257
+ #
258
+ # @person = Person.find(1)
259
+ # # => #<Person id: 1, name: "Donald E. Knuth">
260
+ #
261
+ # <%= link_to(@person.name, person_path(@person)) %>
262
+ # # => <a href="/person/1-donald-e-knuth">Donald E. Knuth</a>
263
+ def parameterize(string, sep = '-')
264
+ # remove malformed utf8 characters
265
+ string = string.toutf8 unless string.is_utf8?
266
+ # replace accented chars with ther ascii equivalents
267
+ parameterized_string = transliterate(string)
268
+ # Turn unwanted chars into the seperator
269
+ parameterized_string.gsub!(/[^a-z0-9\-_]+/i, sep)
270
+ unless sep.blank?
271
+ re_sep = Regexp.escape(sep)
272
+ # No more than one of the separator in a row.
273
+ parameterized_string.gsub!(/#{re_sep}{2,}/, sep)
274
+ # Remove leading/trailing separator.
275
+ parameterized_string.gsub!(/^#{re_sep}|#{re_sep}$/i, '')
276
+ end
277
+ parameterized_string.downcase
278
+ end
279
+
280
+
281
+ # Replaces accented characters with their ascii equivalents.
282
+ def transliterate(string)
283
+ Iconv.iconv('ascii//ignore//translit', 'utf-8', string).to_s
284
+ end
285
+
286
+ if RUBY_VERSION >= '1.9'
287
+ undef_method :transliterate
288
+ def transliterate(string)
289
+ warn "Ruby 1.9 doesn't support Unicode normalization yet"
290
+ string.dup
291
+ end
292
+
293
+ # The iconv transliteration code doesn't function correctly
294
+ # on some platforms, but it's very fast where it does function.
295
+ elsif "foo" != (Inflector.transliterate("föö") rescue nil)
296
+ undef_method :transliterate
297
+ def transliterate(string)
298
+ string.mb_chars.normalize(:kd). # Decompose accented characters
299
+ gsub(/[^\x00-\x7F]+/, '') # Remove anything non-ASCII entirely (e.g. diacritics).
300
+ end
301
+ end
302
+
303
+ # Create the name of a table like Rails does for models to table names. This method
304
+ # uses the +pluralize+ method on the last word in the string.
305
+ #
306
+ # Examples
307
+ # "RawScaledScorer".tableize # => "raw_scaled_scorers"
308
+ # "egg_and_ham".tableize # => "egg_and_hams"
309
+ # "fancyCategory".tableize # => "fancy_categories"
310
+ def tableize(class_name)
311
+ pluralize(underscore(class_name))
312
+ end
313
+
314
+ # Create a class name from a plural table name like Rails does for table names to models.
315
+ # Note that this returns a string and not a Class. (To convert to an actual class
316
+ # follow +classify+ with +constantize+.)
317
+ #
318
+ # Examples:
319
+ # "egg_and_hams".classify # => "EggAndHam"
320
+ # "posts".classify # => "Post"
321
+ #
322
+ # Singular names are not handled correctly:
323
+ # "business".classify # => "Busines"
324
+ def classify(table_name)
325
+ # strip out any leading schema name
326
+ camelize(singularize(table_name.to_s.sub(/.*\./, '')))
327
+ end
328
+
329
+ # Creates a foreign key name from a class name.
330
+ # +separate_class_name_and_id_with_underscore+ sets whether
331
+ # the method should put '_' between the name and 'id'.
332
+ #
333
+ # Examples:
334
+ # "Message".foreign_key # => "message_id"
335
+ # "Message".foreign_key(false) # => "messageid"
336
+ # "Admin::Post".foreign_key # => "post_id"
337
+ def foreign_key(class_name, separate_class_name_and_id_with_underscore = true)
338
+ underscore(demodulize(class_name)) + (separate_class_name_and_id_with_underscore ? "_id" : "id")
339
+ end
340
+
341
+ # Ruby 1.9 introduces an inherit argument for Module#const_get and
342
+ # #const_defined? and changes their default behavior.
343
+ if Module.method(:const_get).arity == 1
344
+ # Tries to find a constant with the name specified in the argument string:
345
+ #
346
+ # "Module".constantize # => Module
347
+ # "Test::Unit".constantize # => Test::Unit
348
+ #
349
+ # The name is assumed to be the one of a top-level constant, no matter whether
350
+ # it starts with "::" or not. No lexical context is taken into account:
351
+ #
352
+ # C = 'outside'
353
+ # module M
354
+ # C = 'inside'
355
+ # C # => 'inside'
356
+ # "C".constantize # => 'outside', same as ::C
357
+ # end
358
+ #
359
+ # NameError is raised when the name is not in CamelCase or the constant is
360
+ # unknown.
361
+ def constantize(camel_cased_word)
362
+ names = camel_cased_word.split('::')
363
+ names.shift if names.empty? || names.first.empty?
364
+
365
+ constant = Object
366
+ names.each do |name|
367
+ constant = constant.const_defined?(name) ? constant.const_get(name) : constant.const_missing(name)
368
+ end
369
+ constant
370
+ end
371
+ else
372
+ def constantize(camel_cased_word) #:nodoc:
373
+ names = camel_cased_word.split('::')
374
+ names.shift if names.empty? || names.first.empty?
375
+
376
+ constant = Object
377
+ names.each do |name|
378
+ constant = constant.const_get(name, false) || constant.const_missing(name)
379
+ end
380
+ constant
381
+ end
382
+ end
383
+
384
+ # Turns a number into an ordinal string used to denote the position in an
385
+ # ordered sequence such as 1st, 2nd, 3rd, 4th.
386
+ #
387
+ # Examples:
388
+ # ordinalize(1) # => "1st"
389
+ # ordinalize(2) # => "2nd"
390
+ # ordinalize(1002) # => "1002nd"
391
+ # ordinalize(1003) # => "1003rd"
392
+ def ordinalize(number)
393
+ if (11..13).include?(number.to_i % 100)
394
+ "#{number}th"
395
+ else
396
+ case number.to_i % 10
397
+ when 1; "#{number}st"
398
+ when 2; "#{number}nd"
399
+ when 3; "#{number}rd"
400
+ else "#{number}th"
401
+ end
402
+ end
403
+ end
404
+ end
405
+ end
406
+
407
+ # in case active_support/inflector is required without the rest of active_support
408
+ require 'active_support/inflections'
409
+ unless String.included_modules.include?(ActiveSupport::CoreExtensions::String::Inflections)
410
+ String.send :include, ActiveSupport::CoreExtensions::String::Inflections
411
+ end