formattedstring 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (53) hide show
  1. data/LICENSE +16 -0
  2. data/README +57 -0
  3. data/Rakefile +79 -0
  4. data/doc/classes/Enumerable.html +146 -0
  5. data/doc/classes/Enumerable.src/M000025.html +21 -0
  6. data/doc/classes/Enumerable.src/M000026.html +34 -0
  7. data/doc/classes/FormattedString/Formats/Xml.html +144 -0
  8. data/doc/classes/FormattedString/Formats/Xml.src/M000004.html +49 -0
  9. data/doc/classes/FormattedString/Formats.html +111 -0
  10. data/doc/classes/FormattedString.html +111 -0
  11. data/doc/classes/Inflector/Inflections.html +321 -0
  12. data/doc/classes/Inflector/Inflections.src/M000019.html +18 -0
  13. data/doc/classes/Inflector/Inflections.src/M000020.html +18 -0
  14. data/doc/classes/Inflector/Inflections.src/M000021.html +18 -0
  15. data/doc/classes/Inflector/Inflections.src/M000022.html +26 -0
  16. data/doc/classes/Inflector/Inflections.src/M000023.html +18 -0
  17. data/doc/classes/Inflector/Inflections.src/M000024.html +23 -0
  18. data/doc/classes/Inflector.html +516 -0
  19. data/doc/classes/Inflector.src/M000005.html +22 -0
  20. data/doc/classes/Inflector.src/M000006.html +25 -0
  21. data/doc/classes/Inflector.src/M000007.html +25 -0
  22. data/doc/classes/Inflector.src/M000008.html +22 -0
  23. data/doc/classes/Inflector.src/M000009.html +18 -0
  24. data/doc/classes/Inflector.src/M000010.html +22 -0
  25. data/doc/classes/Inflector.src/M000011.html +18 -0
  26. data/doc/classes/Inflector.src/M000012.html +18 -0
  27. data/doc/classes/Inflector.src/M000013.html +18 -0
  28. data/doc/classes/Inflector.src/M000014.html +18 -0
  29. data/doc/classes/Inflector.src/M000015.html +19 -0
  30. data/doc/classes/Inflector.src/M000016.html +18 -0
  31. data/doc/classes/Inflector.src/M000017.html +22 -0
  32. data/doc/classes/Inflector.src/M000018.html +27 -0
  33. data/doc/classes/String.html +191 -0
  34. data/doc/classes/String.src/M000001.html +18 -0
  35. data/doc/classes/String.src/M000002.html +20 -0
  36. data/doc/classes/String.src/M000003.html +19 -0
  37. data/doc/created.rid +1 -0
  38. data/doc/files/LICENSE.html +129 -0
  39. data/doc/files/README.html +196 -0
  40. data/doc/files/lib/formatted_string/formats/xml_rb.html +119 -0
  41. data/doc/files/lib/formatted_string_rb.html +108 -0
  42. data/doc/files/lib/inflections_rb.html +101 -0
  43. data/doc/files/lib/inflector_rb.html +108 -0
  44. data/doc/fr_class_index.html +33 -0
  45. data/doc/fr_file_index.html +32 -0
  46. data/doc/fr_method_index.html +52 -0
  47. data/doc/index.html +24 -0
  48. data/doc/rdoc-style.css +208 -0
  49. data/lib/formatted_string/formats/xml.rb +163 -0
  50. data/lib/formatted_string.rb +34 -0
  51. data/lib/inflections.rb +53 -0
  52. data/lib/inflector.rb +282 -0
  53. metadata +120 -0
data/lib/inflector.rb ADDED
@@ -0,0 +1,282 @@
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
+ if singular[0,1].upcase == plural[0,1].upcase
51
+ plural(Regexp.new("(#{singular[0,1]})#{singular[1..-1]}$", "i"), '\1' + plural[1..-1])
52
+ singular(Regexp.new("(#{plural[0,1]})#{plural[1..-1]}$", "i"), '\1' + singular[1..-1])
53
+ else
54
+ plural(Regexp.new("#{singular[0,1].upcase}(?i)#{singular[1..-1]}$"), plural[0,1].upcase + plural[1..-1])
55
+ plural(Regexp.new("#{singular[0,1].downcase}(?i)#{singular[1..-1]}$"), plural[0,1].downcase + plural[1..-1])
56
+ singular(Regexp.new("#{plural[0,1].upcase}(?i)#{plural[1..-1]}$"), singular[0,1].upcase + singular[1..-1])
57
+ singular(Regexp.new("#{plural[0,1].downcase}(?i)#{plural[1..-1]}$"), singular[0,1].downcase + singular[1..-1])
58
+ end
59
+ end
60
+
61
+ # Add uncountable words that shouldn't be attempted inflected.
62
+ #
63
+ # Examples:
64
+ # uncountable "money"
65
+ # uncountable "money", "information"
66
+ # uncountable %w( money information rice )
67
+ def uncountable(*words)
68
+ (@uncountables << words).flatten!
69
+ end
70
+
71
+ # Clears the loaded inflections within a given scope (default is :all). Give the scope as a symbol of the inflection type,
72
+ # the options are: :plurals, :singulars, :uncountables
73
+ #
74
+ # Examples:
75
+ # clear :all
76
+ # clear :plurals
77
+ def clear(scope = :all)
78
+ case scope
79
+ when :all
80
+ @plurals, @singulars, @uncountables = [], [], []
81
+ else
82
+ instance_variable_set "@#{scope}", []
83
+ end
84
+ end
85
+ end
86
+
87
+ extend self
88
+
89
+ def inflections
90
+ if block_given?
91
+ yield Inflections.instance
92
+ else
93
+ Inflections.instance
94
+ end
95
+ end
96
+
97
+ # Returns the plural form of the word in the string.
98
+ #
99
+ # Examples
100
+ # "post".pluralize #=> "posts"
101
+ # "octopus".pluralize #=> "octopi"
102
+ # "sheep".pluralize #=> "sheep"
103
+ # "words".pluralize #=> "words"
104
+ # "the blue mailman".pluralize #=> "the blue mailmen"
105
+ # "CamelOctopus".pluralize #=> "CamelOctopi"
106
+ def pluralize(word)
107
+ result = word.to_s.dup
108
+
109
+ if word.empty? || inflections.uncountables.include?(result.downcase)
110
+ result
111
+ else
112
+ inflections.plurals.each { |(rule, replacement)| break if result.gsub!(rule, replacement) }
113
+ result
114
+ end
115
+ end
116
+
117
+ # The reverse of pluralize, returns the singular form of a word in a string.
118
+ #
119
+ # Examples
120
+ # "posts".singularize #=> "post"
121
+ # "octopi".singularize #=> "octopus"
122
+ # "sheep".singluarize #=> "sheep"
123
+ # "word".singluarize #=> "word"
124
+ # "the blue mailmen".singularize #=> "the blue mailman"
125
+ # "CamelOctopi".singularize #=> "CamelOctopus"
126
+ def singularize(word)
127
+ result = word.to_s.dup
128
+
129
+ if inflections.uncountables.include?(result.downcase)
130
+ result
131
+ else
132
+ inflections.singulars.each { |(rule, replacement)| break if result.gsub!(rule, replacement) }
133
+ result
134
+ end
135
+ end
136
+
137
+ # By default, camelize converts strings to UpperCamelCase. If the argument to camelize
138
+ # is set to ":lower" then camelize produces lowerCamelCase.
139
+ #
140
+ # camelize will also convert '/' to '::' which is useful for converting paths to namespaces
141
+ #
142
+ # Examples
143
+ # "active_record".camelize #=> "ActiveRecord"
144
+ # "active_record".camelize(:lower) #=> "activeRecord"
145
+ # "active_record/errors".camelize #=> "ActiveRecord::Errors"
146
+ # "active_record/errors".camelize(:lower) #=> "activeRecord::Errors"
147
+ def camelize(lower_case_and_underscored_word, first_letter_in_uppercase = true)
148
+ if first_letter_in_uppercase
149
+ lower_case_and_underscored_word.to_s.gsub(/\/(.?)/) { "::" + $1.upcase }.gsub(/(^|_)(.)/) { $2.upcase }
150
+ else
151
+ lower_case_and_underscored_word.first + camelize(lower_case_and_underscored_word)[1..-1]
152
+ end
153
+ end
154
+
155
+ # Capitalizes all the words and replaces some characters in the string to create
156
+ # a nicer looking title. Titleize is meant for creating pretty output. It is not
157
+ # used in the Rails internals.
158
+ #
159
+ # titleize is also aliased as as titlecase
160
+ #
161
+ # Examples
162
+ # "man from the boondocks".titleize #=> "Man From The Boondocks"
163
+ # "x-men: the last stand".titleize #=> "X Men: The Last Stand"
164
+ def titleize(word)
165
+ humanize(underscore(word)).gsub(/\b([a-z])/) { $1.capitalize }
166
+ end
167
+
168
+ # The reverse of +camelize+. Makes an underscored form from the expression in the string.
169
+ #
170
+ # Changes '::' to '/' to convert namespaces to paths.
171
+ #
172
+ # Examples
173
+ # "ActiveRecord".underscore #=> "active_record"
174
+ # "ActiveRecord::Errors".underscore #=> active_record/errors
175
+ def underscore(camel_cased_word)
176
+ camel_cased_word.to_s.gsub(/::/, '/').
177
+ gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
178
+ gsub(/([a-z\d])([A-Z])/,'\1_\2').
179
+ tr("-", "_").
180
+ downcase
181
+ end
182
+
183
+ # Replaces underscores with dashes in the string.
184
+ #
185
+ # Example
186
+ # "puni_puni" #=> "puni-puni"
187
+ def dasherize(underscored_word)
188
+ underscored_word.gsub(/_/, '-')
189
+ end
190
+
191
+ # Capitalizes the first word and turns underscores into spaces and strips _id.
192
+ # Like titleize, this is meant for creating pretty output.
193
+ #
194
+ # Examples
195
+ # "employee_salary" #=> "Employee salary"
196
+ # "author_id" #=> "Author"
197
+ def humanize(lower_case_and_underscored_word)
198
+ lower_case_and_underscored_word.to_s.gsub(/_id$/, "").gsub(/_/, " ").capitalize
199
+ end
200
+
201
+ # Removes the module part from the expression in the string
202
+ #
203
+ # Examples
204
+ # "ActiveRecord::CoreExtensions::String::Inflections".demodulize #=> "Inflections"
205
+ # "Inflections".demodulize #=> "Inflections"
206
+ def demodulize(class_name_in_module)
207
+ class_name_in_module.to_s.gsub(/^.*::/, '')
208
+ end
209
+
210
+ # Create the name of a table like Rails does for models to table names. This method
211
+ # uses the pluralize method on the last word in the string.
212
+ #
213
+ # Examples
214
+ # "RawScaledScorer".tableize #=> "raw_scaled_scorers"
215
+ # "egg_and_ham".tableize #=> "egg_and_hams"
216
+ # "fancyCategory".tableize #=> "fancy_categories"
217
+ def tableize(class_name)
218
+ pluralize(underscore(class_name))
219
+ end
220
+
221
+ # Create a class name from a table name like Rails does for table names to models.
222
+ # Note that this returns a string and not a Class. (To convert to an actual class
223
+ # follow classify with constantize.)
224
+ #
225
+ # Examples
226
+ # "egg_and_hams".classify #=> "EggAndHam"
227
+ # "post".classify #=> "Post"
228
+ def classify(table_name)
229
+ # strip out any leading schema name
230
+ camelize(singularize(table_name.to_s.sub(/.*\./, '')))
231
+ end
232
+
233
+ # Creates a foreign key name from a class name.
234
+ # +separate_class_name_and_id_with_underscore+ sets whether
235
+ # the method should put '_' between the name and 'id'.
236
+ #
237
+ # Examples
238
+ # "Message".foreign_key #=> "message_id"
239
+ # "Message".foreign_key(false) #=> "messageid"
240
+ # "Admin::Post".foreign_key #=> "post_id"
241
+ def foreign_key(class_name, separate_class_name_and_id_with_underscore = true)
242
+ underscore(demodulize(class_name)) + (separate_class_name_and_id_with_underscore ? "_id" : "id")
243
+ end
244
+
245
+ # Constantize tries to find a declared constant with the name specified
246
+ # in the string. It raises a NameError when the name is not in CamelCase
247
+ # or is not initialized.
248
+ #
249
+ # Examples
250
+ # "Module".constantize #=> Module
251
+ # "Class".constantize #=> Class
252
+ def constantize(camel_cased_word)
253
+ unless /\A(?:::)?([A-Z]\w*(?:::[A-Z]\w*)*)\z/ =~ camel_cased_word
254
+ raise NameError, "#{camel_cased_word.inspect} is not a valid constant name!"
255
+ end
256
+
257
+ Object.module_eval("::#{$1}", __FILE__, __LINE__)
258
+ end
259
+
260
+ # Ordinalize turns a number into an ordinal string used to denote the
261
+ # position in an ordered sequence such as 1st, 2nd, 3rd, 4th.
262
+ #
263
+ # Examples
264
+ # ordinalize(1) # => "1st"
265
+ # ordinalize(2) # => "2nd"
266
+ # ordinalize(1002) # => "1002nd"
267
+ # ordinalize(1003) # => "1003rd"
268
+ def ordinalize(number)
269
+ if (11..13).include?(number.to_i % 100)
270
+ "#{number}th"
271
+ else
272
+ case number.to_i % 10
273
+ when 1; "#{number}st"
274
+ when 2; "#{number}nd"
275
+ when 3; "#{number}rd"
276
+ else "#{number}th"
277
+ end
278
+ end
279
+ end
280
+ end
281
+
282
+ require File.dirname(__FILE__) + '/inflections'
metadata ADDED
@@ -0,0 +1,120 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: formattedstring
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Daniel Parker
8
+ autorequire: formatted_string
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-03-17 00:00:00 -04:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Brings all parsing of Formatted Strings into one common place.
17
+ email: gems@behindlogic.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README
24
+ - LICENSE
25
+ files:
26
+ - lib/formatted_string
27
+ - lib/formatted_string/formats
28
+ - lib/formatted_string/formats/xml.rb
29
+ - lib/formatted_string.rb
30
+ - lib/inflections.rb
31
+ - lib/inflector.rb
32
+ - LICENSE
33
+ - Rakefile
34
+ - README
35
+ - doc/classes
36
+ - doc/classes/Enumerable.html
37
+ - doc/classes/Enumerable.src
38
+ - doc/classes/Enumerable.src/M000025.html
39
+ - doc/classes/Enumerable.src/M000026.html
40
+ - doc/classes/FormattedString
41
+ - doc/classes/FormattedString/Formats
42
+ - doc/classes/FormattedString/Formats/Xml.html
43
+ - doc/classes/FormattedString/Formats/Xml.src
44
+ - doc/classes/FormattedString/Formats/Xml.src/M000004.html
45
+ - doc/classes/FormattedString/Formats.html
46
+ - doc/classes/FormattedString.html
47
+ - doc/classes/Inflector
48
+ - doc/classes/Inflector/Inflections.html
49
+ - doc/classes/Inflector/Inflections.src
50
+ - doc/classes/Inflector/Inflections.src/M000019.html
51
+ - doc/classes/Inflector/Inflections.src/M000020.html
52
+ - doc/classes/Inflector/Inflections.src/M000021.html
53
+ - doc/classes/Inflector/Inflections.src/M000022.html
54
+ - doc/classes/Inflector/Inflections.src/M000023.html
55
+ - doc/classes/Inflector/Inflections.src/M000024.html
56
+ - doc/classes/Inflector.html
57
+ - doc/classes/Inflector.src
58
+ - doc/classes/Inflector.src/M000005.html
59
+ - doc/classes/Inflector.src/M000006.html
60
+ - doc/classes/Inflector.src/M000007.html
61
+ - doc/classes/Inflector.src/M000008.html
62
+ - doc/classes/Inflector.src/M000009.html
63
+ - doc/classes/Inflector.src/M000010.html
64
+ - doc/classes/Inflector.src/M000011.html
65
+ - doc/classes/Inflector.src/M000012.html
66
+ - doc/classes/Inflector.src/M000013.html
67
+ - doc/classes/Inflector.src/M000014.html
68
+ - doc/classes/Inflector.src/M000015.html
69
+ - doc/classes/Inflector.src/M000016.html
70
+ - doc/classes/Inflector.src/M000017.html
71
+ - doc/classes/Inflector.src/M000018.html
72
+ - doc/classes/String.html
73
+ - doc/classes/String.src
74
+ - doc/classes/String.src/M000001.html
75
+ - doc/classes/String.src/M000002.html
76
+ - doc/classes/String.src/M000003.html
77
+ - doc/created.rid
78
+ - doc/files
79
+ - doc/files/lib
80
+ - doc/files/lib/formatted_string
81
+ - doc/files/lib/formatted_string/formats
82
+ - doc/files/lib/formatted_string/formats/xml_rb.html
83
+ - doc/files/lib/formatted_string_rb.html
84
+ - doc/files/lib/inflections_rb.html
85
+ - doc/files/lib/inflector_rb.html
86
+ - doc/files/LICENSE.html
87
+ - doc/files/README.html
88
+ - doc/fr_class_index.html
89
+ - doc/fr_file_index.html
90
+ - doc/fr_method_index.html
91
+ - doc/index.html
92
+ - doc/rdoc-style.css
93
+ has_rdoc: true
94
+ homepage: http://formattedstring.rubyforge.org
95
+ post_install_message:
96
+ rdoc_options: []
97
+
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: "0"
105
+ version:
106
+ required_rubygems_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: "0"
111
+ version:
112
+ requirements: []
113
+
114
+ rubyforge_project: formattedstring
115
+ rubygems_version: 1.0.1
116
+ signing_key:
117
+ specification_version: 2
118
+ summary: Brings all parsing of Formatted Strings into one common place.
119
+ test_files: []
120
+