conject 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,3 +1,6 @@
1
+ Conject v0.1.4
2
+ * Removed Conject::BorroredInflector, moved copies of the underscore and camelize functions into Conject::Utilities
3
+
1
4
  Conject v0.1.3
2
5
  * Changed the way Class.construct_with rewrites Class.new(). It is now safe to re-load the source code of a class that uses construct_with.
3
6
 
@@ -13,7 +13,6 @@ require 'conject/class_finder'
13
13
  require 'conject/dependency_resolver'
14
14
  require 'conject/utilities'
15
15
  require 'conject/composition_error'
16
- require 'conject/borrowed_active_support_inflector'
17
16
 
18
17
  module Conject
19
18
  #
@@ -2,7 +2,7 @@
2
2
  module Conject
3
3
  class ClassFinder
4
4
  def find_class(name)
5
- cname = name.to_s.camelize
5
+ cname = Utilities.camelize(name.to_s)
6
6
  cname_components = cname.split("::")
7
7
  dig_for_class Object, cname_components
8
8
  end
@@ -12,7 +12,7 @@ module Conject
12
12
  return nil unless cname =~ /::/
13
13
  cname_components = cname.split("::")
14
14
  cname_components.pop
15
- cname_components.join("::").underscore
15
+ Utilities.underscore(cname_components.join("::"))
16
16
  end
17
17
 
18
18
  private
@@ -10,6 +10,53 @@ module Conject
10
10
  parts = object_name.to_s.split("/").reject do |x| x == "" end
11
11
  [ parts.join("_").to_sym, parts.last.to_sym ].uniq
12
12
  end
13
+
14
+ # (LIFTED FROM ActiveSupport::Inflector)
15
+ # By default, +camelize+ converts strings to UpperCamelCase. If the argument to +camelize+
16
+ # is set to <tt>:lower</tt> then +camelize+ produces lowerCamelCase.
17
+ #
18
+ # +camelize+ will also convert '/' to '::' which is useful for converting paths to namespaces.
19
+ #
20
+ # Examples:
21
+ # "active_record".camelize # => "ActiveRecord"
22
+ # "active_record".camelize(:lower) # => "activeRecord"
23
+ # "active_record/errors".camelize # => "ActiveRecord::Errors"
24
+ # "active_record/errors".camelize(:lower) # => "activeRecord::Errors"
25
+ #
26
+ # As a rule of thumb you can think of +camelize+ as the inverse of +underscore+,
27
+ # though there are cases where that does not hold:
28
+ #
29
+ # "SSLError".underscore.camelize # => "SslError"
30
+ def camelize(lower_case_and_underscored_word, first_letter_in_uppercase = true)
31
+ if first_letter_in_uppercase
32
+ lower_case_and_underscored_word.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
33
+ else
34
+ lower_case_and_underscored_word.to_s[0].chr.downcase + camelize(lower_case_and_underscored_word)[1..-1]
35
+ end
36
+ end
37
+
38
+ # (LIFTED FROM ActiveSupport::Inflector)
39
+ # Makes an underscored, lowercase form from the expression in the string.
40
+ #
41
+ # Changes '::' to '/' to convert namespaces to paths.
42
+ #
43
+ # Examples:
44
+ # "ActiveRecord".underscore # => "active_record"
45
+ # "ActiveRecord::Errors".underscore # => active_record/errors
46
+ #
47
+ # As a rule of thumb you can think of +underscore+ as the inverse of +camelize+,
48
+ # though there are cases where that does not hold:
49
+ #
50
+ # "SSLError".underscore.camelize # => "SslError"
51
+ def underscore(camel_cased_word)
52
+ word = camel_cased_word.to_s.dup
53
+ word.gsub!(/::/, '/')
54
+ word.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
55
+ word.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
56
+ word.tr!("-", "_")
57
+ word.downcase!
58
+ word
59
+ end
13
60
  end
14
61
  end
15
62
  end
@@ -1,3 +1,3 @@
1
1
  module Conject
2
- VERSION = "0.1.3"
2
+ VERSION = "0.1.4"
3
3
  end
@@ -53,7 +53,7 @@ class ObjectBuilder
53
53
  end
54
54
 
55
55
  def find_or_generate_producer(object_name)
56
- c = Object.const_get(object_name.to_s.camelize)
56
+ c = Object.const_get(Conject::Utilities.camelize(object_name.to_s))
57
57
  if c
58
58
  return SimpleBuilder.new(c)
59
59
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: conject
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -89,7 +89,6 @@ files:
89
89
  - conject.gemspec
90
90
  - doc/inheritance_woes.txt
91
91
  - lib/conject.rb
92
- - lib/conject/borrowed_active_support_inflector.rb
93
92
  - lib/conject/class_ext_construct_with.rb
94
93
  - lib/conject/class_ext_object_context.rb
95
94
  - lib/conject/class_ext_object_peers.rb
@@ -133,7 +132,6 @@ files:
133
132
  - spec/acceptance/regression/nested_contexts_spec.rb
134
133
  - spec/acceptance/regression/non_singleton_spec.rb
135
134
  - spec/acceptance/regression/object_peers_spec.rb
136
- - spec/conject/borrowed_active_support_inflector_spec.rb
137
135
  - spec/conject/class_ext_construct_with_spec.rb
138
136
  - spec/conject/class_finder_spec.rb
139
137
  - spec/conject/composition_error_spec.rb
@@ -201,7 +199,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
201
199
  version: '0'
202
200
  segments:
203
201
  - 0
204
- hash: 4140830812471281535
202
+ hash: 3533860768866738609
205
203
  required_rubygems_version: !ruby/object:Gem::Requirement
206
204
  none: false
207
205
  requirements:
@@ -210,7 +208,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
210
208
  version: '0'
211
209
  segments:
212
210
  - 0
213
- hash: 4140830812471281535
211
+ hash: 3533860768866738609
214
212
  requirements: []
215
213
  rubyforge_project:
216
214
  rubygems_version: 1.8.24
@@ -232,7 +230,6 @@ test_files:
232
230
  - spec/acceptance/regression/nested_contexts_spec.rb
233
231
  - spec/acceptance/regression/non_singleton_spec.rb
234
232
  - spec/acceptance/regression/object_peers_spec.rb
235
- - spec/conject/borrowed_active_support_inflector_spec.rb
236
233
  - spec/conject/class_ext_construct_with_spec.rb
237
234
  - spec/conject/class_finder_spec.rb
238
235
  - spec/conject/composition_error_spec.rb
@@ -1,525 +0,0 @@
1
- # in case active_support/inflector is required without the rest of active_support
2
- module BorrowedActiveSupport
3
- module Inflector
4
- # A singleton instance of this class is yielded by Inflector.inflections, which can then be used to specify additional
5
- # inflection rules. Examples:
6
- #
7
- # BorrowedActiveSupport::Inflector.inflections do |inflect|
8
- # inflect.plural /^(ox)$/i, '\1\2en'
9
- # inflect.singular /^(ox)en/i, '\1'
10
- #
11
- # inflect.irregular 'octopus', 'octopi'
12
- #
13
- # inflect.uncountable "equipment"
14
- # end
15
- #
16
- # New rules are added at the top. So in the example above, the irregular rule for octopus will now be the first of the
17
- # pluralization and singularization rules that is runs. This guarantees that your rules run before any of the rules that may
18
- # already have been loaded.
19
- class Inflections
20
- def self.instance
21
- @__instance__ ||= new
22
- end
23
-
24
- attr_reader :plurals, :singulars, :uncountables, :humans
25
-
26
- def initialize
27
- @plurals, @singulars, @uncountables, @humans = [], [], [], []
28
- end
29
-
30
- # Specifies a new pluralization rule and its replacement. The rule can either be a string or a regular expression.
31
- # The replacement should always be a string that may include references to the matched data from the rule.
32
- def plural(rule, replacement)
33
- @uncountables.delete(rule) if rule.is_a?(String)
34
- @uncountables.delete(replacement)
35
- @plurals.insert(0, [rule, replacement])
36
- end
37
-
38
- # Specifies a new singularization rule and its replacement. The rule can either be a string or a regular expression.
39
- # The replacement should always be a string that may include references to the matched data from the rule.
40
- def singular(rule, replacement)
41
- @uncountables.delete(rule) if rule.is_a?(String)
42
- @uncountables.delete(replacement)
43
- @singulars.insert(0, [rule, replacement])
44
- end
45
-
46
- # Specifies a new irregular that applies to both pluralization and singularization at the same time. This can only be used
47
- # for strings, not regular expressions. You simply pass the irregular in singular and plural form.
48
- #
49
- # Examples:
50
- # irregular 'octopus', 'octopi'
51
- # irregular 'person', 'people'
52
- def irregular(singular, plural)
53
- @uncountables.delete(singular)
54
- @uncountables.delete(plural)
55
- if singular[0,1].upcase == plural[0,1].upcase
56
- plural(Regexp.new("(#{singular[0,1]})#{singular[1..-1]}$", "i"), '\1' + plural[1..-1])
57
- plural(Regexp.new("(#{plural[0,1]})#{plural[1..-1]}$", "i"), '\1' + plural[1..-1])
58
- singular(Regexp.new("(#{plural[0,1]})#{plural[1..-1]}$", "i"), '\1' + singular[1..-1])
59
- else
60
- plural(Regexp.new("#{singular[0,1].upcase}(?i)#{singular[1..-1]}$"), plural[0,1].upcase + plural[1..-1])
61
- plural(Regexp.new("#{singular[0,1].downcase}(?i)#{singular[1..-1]}$"), plural[0,1].downcase + plural[1..-1])
62
- plural(Regexp.new("#{plural[0,1].upcase}(?i)#{plural[1..-1]}$"), plural[0,1].upcase + plural[1..-1])
63
- plural(Regexp.new("#{plural[0,1].downcase}(?i)#{plural[1..-1]}$"), plural[0,1].downcase + plural[1..-1])
64
- singular(Regexp.new("#{plural[0,1].upcase}(?i)#{plural[1..-1]}$"), singular[0,1].upcase + singular[1..-1])
65
- singular(Regexp.new("#{plural[0,1].downcase}(?i)#{plural[1..-1]}$"), singular[0,1].downcase + singular[1..-1])
66
- end
67
- end
68
-
69
- # Add uncountable words that shouldn't be attempted inflected.
70
- #
71
- # Examples:
72
- # uncountable "money"
73
- # uncountable "money", "information"
74
- # uncountable %w( money information rice )
75
- def uncountable(*words)
76
- (@uncountables << words).flatten!
77
- end
78
-
79
- # Specifies a humanized form of a string by a regular expression rule or by a string mapping.
80
- # When using a regular expression based replacement, the normal humanize formatting is called after the replacement.
81
- # When a string is used, the human form should be specified as desired (example: 'The name', not 'the_name')
82
- #
83
- # Examples:
84
- # human /_cnt$/i, '\1_count'
85
- # human "legacy_col_person_name", "Name"
86
- def human(rule, replacement)
87
- @humans.insert(0, [rule, replacement])
88
- end
89
-
90
- # Clears the loaded inflections within a given scope (default is <tt>:all</tt>).
91
- # Give the scope as a symbol of the inflection type, the options are: <tt>:plurals</tt>,
92
- # <tt>:singulars</tt>, <tt>:uncountables</tt>, <tt>:humans</tt>.
93
- #
94
- # Examples:
95
- # clear :all
96
- # clear :plurals
97
- def clear(scope = :all)
98
- case scope
99
- when :all
100
- @plurals, @singulars, @uncountables, @humans = [], [], [], []
101
- else
102
- instance_variable_set "@#{scope}", []
103
- end
104
- end
105
- end
106
-
107
- # Yields a singleton instance of Inflector::Inflections so you can specify additional
108
- # inflector rules.
109
- #
110
- # Example:
111
- # BorrowedActiveSupport::Inflector.inflections do |inflect|
112
- # inflect.uncountable "rails"
113
- # end
114
- def inflections
115
- if block_given?
116
- yield Inflections.instance
117
- else
118
- Inflections.instance
119
- end
120
- end
121
-
122
- # Returns the plural form of the word in the string.
123
- #
124
- # Examples:
125
- # "post".pluralize # => "posts"
126
- # "octopus".pluralize # => "octopi"
127
- # "sheep".pluralize # => "sheep"
128
- # "words".pluralize # => "words"
129
- # "CamelOctopus".pluralize # => "CamelOctopi"
130
- def pluralize(word)
131
- result = word.to_s.dup
132
-
133
- if word.empty? || inflections.uncountables.include?(result.downcase)
134
- result
135
- else
136
- inflections.plurals.each { |(rule, replacement)| break if result.gsub!(rule, replacement) }
137
- result
138
- end
139
- end
140
-
141
- # The reverse of +pluralize+, returns the singular form of a word in a string.
142
- #
143
- # Examples:
144
- # "posts".singularize # => "post"
145
- # "octopi".singularize # => "octopus"
146
- # "sheep".singularize # => "sheep"
147
- # "word".singularize # => "word"
148
- # "CamelOctopi".singularize # => "CamelOctopus"
149
- def singularize(word)
150
- result = word.to_s.dup
151
-
152
- if inflections.uncountables.any? { |inflection| result =~ /\b(#{inflection})\Z/i }
153
- result
154
- else
155
- inflections.singulars.each { |(rule, replacement)| break if result.gsub!(rule, replacement) }
156
- result
157
- end
158
- end
159
-
160
- # Capitalizes the first word and turns underscores into spaces and strips a
161
- # trailing "_id", if any. Like +titleize+, this is meant for creating pretty output.
162
- #
163
- # Examples:
164
- # "employee_salary" # => "Employee salary"
165
- # "author_id" # => "Author"
166
- def humanize(lower_case_and_underscored_word)
167
- result = lower_case_and_underscored_word.to_s.dup
168
-
169
- inflections.humans.each { |(rule, replacement)| break if result.gsub!(rule, replacement) }
170
- result.gsub(/_id$/, "").gsub(/_/, " ").capitalize
171
- end
172
-
173
- # Capitalizes all the words and replaces some characters in the string to create
174
- # a nicer looking title. +titleize+ is meant for creating pretty output. It is not
175
- # used in the Rails internals.
176
- #
177
- # +titleize+ is also aliased as as +titlecase+.
178
- #
179
- # Examples:
180
- # "man from the boondocks".titleize # => "Man From The Boondocks"
181
- # "x-men: the last stand".titleize # => "X Men: The Last Stand"
182
- def titleize(word)
183
- humanize(underscore(word)).gsub(/\b('?[a-z])/) { $1.capitalize }
184
- end
185
-
186
- # Create the name of a table like Rails does for models to table names. This method
187
- # uses the +pluralize+ method on the last word in the string.
188
- #
189
- # Examples
190
- # "RawScaledScorer".tableize # => "raw_scaled_scorers"
191
- # "egg_and_ham".tableize # => "egg_and_hams"
192
- # "fancyCategory".tableize # => "fancy_categories"
193
- def tableize(class_name)
194
- pluralize(underscore(class_name))
195
- end
196
-
197
- # Create a class name from a plural table name like Rails does for table names to models.
198
- # Note that this returns a string and not a Class. (To convert to an actual class
199
- # follow +classify+ with +constantize+.)
200
- #
201
- # Examples:
202
- # "egg_and_hams".classify # => "EggAndHam"
203
- # "posts".classify # => "Post"
204
- #
205
- # Singular names are not handled correctly:
206
- # "business".classify # => "Busines"
207
- def classify(table_name)
208
- # strip out any leading schema name
209
- camelize(singularize(table_name.to_s.sub(/.*\./, '')))
210
- end
211
- end
212
-
213
- # The Inflector transforms words from singular to plural, class names to table names, modularized class names to ones without,
214
- # and class names to foreign keys. The default inflections for pluralization, singularization, and uncountable words are kept
215
- # in inflections.rb.
216
- #
217
- # The Rails core team has stated patches for the inflections library will not be accepted
218
- # in order to avoid breaking legacy applications which may be relying on errant inflections.
219
- # If you discover an incorrect inflection and require it for your application, you'll need
220
- # to correct it yourself (explained below).
221
- module Inflector
222
- extend self
223
-
224
- # By default, +camelize+ converts strings to UpperCamelCase. If the argument to +camelize+
225
- # is set to <tt>:lower</tt> then +camelize+ produces lowerCamelCase.
226
- #
227
- # +camelize+ will also convert '/' to '::' which is useful for converting paths to namespaces.
228
- #
229
- # Examples:
230
- # "active_record".camelize # => "ActiveRecord"
231
- # "active_record".camelize(:lower) # => "activeRecord"
232
- # "active_record/errors".camelize # => "ActiveRecord::Errors"
233
- # "active_record/errors".camelize(:lower) # => "activeRecord::Errors"
234
- #
235
- # As a rule of thumb you can think of +camelize+ as the inverse of +underscore+,
236
- # though there are cases where that does not hold:
237
- #
238
- # "SSLError".underscore.camelize # => "SslError"
239
- def camelize(lower_case_and_underscored_word, first_letter_in_uppercase = true)
240
- if first_letter_in_uppercase
241
- lower_case_and_underscored_word.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
242
- else
243
- lower_case_and_underscored_word.to_s[0].chr.downcase + camelize(lower_case_and_underscored_word)[1..-1]
244
- end
245
- end
246
-
247
- # Makes an underscored, lowercase form from the expression in the string.
248
- #
249
- # Changes '::' to '/' to convert namespaces to paths.
250
- #
251
- # Examples:
252
- # "ActiveRecord".underscore # => "active_record"
253
- # "ActiveRecord::Errors".underscore # => active_record/errors
254
- #
255
- # As a rule of thumb you can think of +underscore+ as the inverse of +camelize+,
256
- # though there are cases where that does not hold:
257
- #
258
- # "SSLError".underscore.camelize # => "SslError"
259
- def underscore(camel_cased_word)
260
- word = camel_cased_word.to_s.dup
261
- word.gsub!(/::/, '/')
262
- word.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
263
- word.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
264
- word.tr!("-", "_")
265
- word.downcase!
266
- word
267
- end
268
-
269
- # Replaces underscores with dashes in the string.
270
- #
271
- # Example:
272
- # "puni_puni" # => "puni-puni"
273
- def dasherize(underscored_word)
274
- underscored_word.gsub(/_/, '-')
275
- end
276
-
277
- # Removes the module part from the expression in the string.
278
- #
279
- # Examples:
280
- # "ActiveRecord::CoreExtensions::String::Inflections".demodulize # => "Inflections"
281
- # "Inflections".demodulize # => "Inflections"
282
- def demodulize(class_name_in_module)
283
- class_name_in_module.to_s.gsub(/^.*::/, '')
284
- end
285
-
286
- # Creates a foreign key name from a class name.
287
- # +separate_class_name_and_id_with_underscore+ sets whether
288
- # the method should put '_' between the name and 'id'.
289
- #
290
- # Examples:
291
- # "Message".foreign_key # => "message_id"
292
- # "Message".foreign_key(false) # => "messageid"
293
- # "Admin::Post".foreign_key # => "post_id"
294
- def foreign_key(class_name, separate_class_name_and_id_with_underscore = true)
295
- underscore(demodulize(class_name)) + (separate_class_name_and_id_with_underscore ? "_id" : "id")
296
- end
297
-
298
- # Ruby 1.9 introduces an inherit argument for Module#const_get and
299
- # #const_defined? and changes their default behavior.
300
- if Module.method(:const_get).arity == 1
301
- # Tries to find a constant with the name specified in the argument string:
302
- #
303
- # "Module".constantize # => Module
304
- # "Test::Unit".constantize # => Test::Unit
305
- #
306
- # The name is assumed to be the one of a top-level constant, no matter whether
307
- # it starts with "::" or not. No lexical context is taken into account:
308
- #
309
- # C = 'outside'
310
- # module M
311
- # C = 'inside'
312
- # C # => 'inside'
313
- # "C".constantize # => 'outside', same as ::C
314
- # end
315
- #
316
- # NameError is raised when the name is not in CamelCase or the constant is
317
- # unknown.
318
- def constantize(camel_cased_word)
319
- names = camel_cased_word.split('::')
320
- names.shift if names.empty? || names.first.empty?
321
-
322
- constant = Object
323
- names.each do |name|
324
- constant = constant.const_defined?(name) ? constant.const_get(name) : constant.const_missing(name)
325
- end
326
- constant
327
- end
328
- else
329
- def constantize(camel_cased_word) #:nodoc:
330
- names = camel_cased_word.split('::')
331
- names.shift if names.empty? || names.first.empty?
332
-
333
- constant = Object
334
- names.each do |name|
335
- constant = constant.const_defined?(name, false) ? constant.const_get(name) : constant.const_missing(name)
336
- end
337
- constant
338
- end
339
- end
340
-
341
- # Turns a number into an ordinal string used to denote the position in an
342
- # ordered sequence such as 1st, 2nd, 3rd, 4th.
343
- #
344
- # Examples:
345
- # ordinalize(1) # => "1st"
346
- # ordinalize(2) # => "2nd"
347
- # ordinalize(1002) # => "1002nd"
348
- # ordinalize(1003) # => "1003rd"
349
- # ordinalize(-11) # => "-11th"
350
- # ordinalize(-1021) # => "-1021st"
351
- def ordinalize(number)
352
- if (11..13).include?(number.to_i.abs % 100)
353
- "#{number}th"
354
- else
355
- case number.to_i.abs % 10
356
- when 1; "#{number}st"
357
- when 2; "#{number}nd"
358
- when 3; "#{number}rd"
359
- else "#{number}th"
360
- end
361
- end
362
- end
363
- end
364
- end
365
-
366
- # String inflections define new methods on the String class to transform names for different purposes.
367
- # For instance, you can figure out the name of a table from the name of a class.
368
- #
369
- # "ScaleScore".tableize # => "scale_scores"
370
- #
371
- class String
372
- # Returns the plural form of the word in the string.
373
- #
374
- # "post".pluralize # => "posts"
375
- # "octopus".pluralize # => "octopi"
376
- # "sheep".pluralize # => "sheep"
377
- # "words".pluralize # => "words"
378
- # "the blue mailman".pluralize # => "the blue mailmen"
379
- # "CamelOctopus".pluralize # => "CamelOctopi"
380
- def pluralize
381
- BorrowedActiveSupport::Inflector.pluralize(self)
382
- end
383
-
384
- # The reverse of +pluralize+, returns the singular form of a word in a string.
385
- #
386
- # "posts".singularize # => "post"
387
- # "octopi".singularize # => "octopus"
388
- # "sheep".singularize # => "sheep"
389
- # "word".singularize # => "word"
390
- # "the blue mailmen".singularize # => "the blue mailman"
391
- # "CamelOctopi".singularize # => "CamelOctopus"
392
- def singularize
393
- BorrowedActiveSupport::Inflector.singularize(self)
394
- end
395
-
396
- # +constantize+ tries to find a declared constant with the name specified
397
- # in the string. It raises a NameError when the name is not in CamelCase
398
- # or is not initialized.
399
- #
400
- # Examples
401
- # "Module".constantize # => Module
402
- # "Class".constantize # => Class
403
- def constantize
404
- BorrowedActiveSupport::Inflector.constantize(self)
405
- end
406
-
407
- # By default, +camelize+ converts strings to UpperCamelCase. If the argument to camelize
408
- # is set to <tt>:lower</tt> then camelize produces lowerCamelCase.
409
- #
410
- # +camelize+ will also convert '/' to '::' which is useful for converting paths to namespaces.
411
- #
412
- # "active_record".camelize # => "ActiveRecord"
413
- # "active_record".camelize(:lower) # => "activeRecord"
414
- # "active_record/errors".camelize # => "ActiveRecord::Errors"
415
- # "active_record/errors".camelize(:lower) # => "activeRecord::Errors"
416
- def camelize(first_letter = :upper)
417
- case first_letter
418
- when :upper then BorrowedActiveSupport::Inflector.camelize(self, true)
419
- when :lower then BorrowedActiveSupport::Inflector.camelize(self, false)
420
- end
421
- end
422
- alias_method :camelcase, :camelize
423
-
424
- # Capitalizes all the words and replaces some characters in the string to create
425
- # a nicer looking title. +titleize+ is meant for creating pretty output. It is not
426
- # used in the Rails internals.
427
- #
428
- # +titleize+ is also aliased as +titlecase+.
429
- #
430
- # "man from the boondocks".titleize # => "Man From The Boondocks"
431
- # "x-men: the last stand".titleize # => "X Men: The Last Stand"
432
- def titleize
433
- BorrowedActiveSupport::Inflector.titleize(self)
434
- end
435
- alias_method :titlecase, :titleize
436
-
437
- # The reverse of +camelize+. Makes an underscored, lowercase form from the expression in the string.
438
- #
439
- # +underscore+ will also change '::' to '/' to convert namespaces to paths.
440
- #
441
- # "ActiveRecord".underscore # => "active_record"
442
- # "ActiveRecord::Errors".underscore # => active_record/errors
443
- def underscore
444
- BorrowedActiveSupport::Inflector.underscore(self)
445
- end
446
-
447
- # Replaces underscores with dashes in the string.
448
- #
449
- # "puni_puni" # => "puni-puni"
450
- def dasherize
451
- BorrowedActiveSupport::Inflector.dasherize(self)
452
- end
453
-
454
- # Removes the module part from the constant expression in the string.
455
- #
456
- # "ActiveRecord::CoreExtensions::String::Inflections".demodulize # => "Inflections"
457
- # "Inflections".demodulize # => "Inflections"
458
- def demodulize
459
- BorrowedActiveSupport::Inflector.demodulize(self)
460
- end
461
-
462
- # Replaces special characters in a string so that it may be used as part of a 'pretty' URL.
463
- #
464
- # ==== Examples
465
- #
466
- # class Person
467
- # def to_param
468
- # "#{id}-#{name.parameterize}"
469
- # end
470
- # end
471
- #
472
- # @person = Person.find(1)
473
- # # => #<Person id: 1, name: "Donald E. Knuth">
474
- #
475
- # <%= link_to(@person.name, person_path %>
476
- # # => <a href="/person/1-donald-e-knuth">Donald E. Knuth</a>
477
- def parameterize(sep = '-')
478
- BorrowedActiveSupport::Inflector.parameterize(self, sep)
479
- end
480
-
481
- # Creates the name of a table like Rails does for models to table names. This method
482
- # uses the +pluralize+ method on the last word in the string.
483
- #
484
- # "RawScaledScorer".tableize # => "raw_scaled_scorers"
485
- # "egg_and_ham".tableize # => "egg_and_hams"
486
- # "fancyCategory".tableize # => "fancy_categories"
487
- def tableize
488
- BorrowedActiveSupport::Inflector.tableize(self)
489
- end
490
-
491
- # Create a class name from a plural table name like Rails does for table names to models.
492
- # Note that this returns a string and not a class. (To convert to an actual class
493
- # follow +classify+ with +constantize+.)
494
- #
495
- # "egg_and_hams".classify # => "EggAndHam"
496
- # "posts".classify # => "Post"
497
- #
498
- # Singular names are not handled correctly.
499
- #
500
- # "business".classify # => "Busines"
501
- def classify
502
- BorrowedActiveSupport::Inflector.classify(self)
503
- end
504
-
505
- # Capitalizes the first word, turns underscores into spaces, and strips '_id'.
506
- # Like +titleize+, this is meant for creating pretty output.
507
- #
508
- # "employee_salary" # => "Employee salary"
509
- # "author_id" # => "Author"
510
- def humanize
511
- BorrowedActiveSupport::Inflector.humanize(self)
512
- end
513
-
514
- # Creates a foreign key name from a class name.
515
- # +separate_class_name_and_id_with_underscore+ sets whether
516
- # the method should put '_' between the name and 'id'.
517
- #
518
- # Examples
519
- # "Message".foreign_key # => "message_id"
520
- # "Message".foreign_key(false) # => "messageid"
521
- # "Admin::Post".foreign_key # => "post_id"
522
- def foreign_key(separate_class_name_and_id_with_underscore = true)
523
- BorrowedActiveSupport::Inflector.foreign_key(self, separate_class_name_and_id_with_underscore)
524
- end
525
- end
@@ -1,28 +0,0 @@
1
- require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
2
-
3
- #
4
- # Some super-basic specs that don't really exercise Inflector,
5
- # but demonstrate that we have this capability built-in
6
- # when using ObjectContext.
7
- #
8
- describe "Borrowed ActiveSupport Inflector" do
9
- describe "#camelize" do
10
- it "converts underscored strings to camel case" do
11
- "four_five_six".camelize.should == "FourFiveSix"
12
- end
13
-
14
- it "leaves camel case words along" do
15
- "HoppingSizzler".camelize.should == "HoppingSizzler"
16
- end
17
- end
18
-
19
- describe "#underscore" do
20
- it "converts camel-case words to underscored" do
21
- "HoppingSizzler".underscore.should == "hopping_sizzler"
22
- end
23
-
24
- it "leaves underscored strings alone" do
25
- "four_five_six".underscore.should == "four_five_six"
26
- end
27
- end
28
- end