inflector 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,149 @@
1
+ module Inflector
2
+ extend self
3
+
4
+ module CoreExtensions
5
+ autoload :String, 'inflector/core_ext/string'
6
+ end
7
+
8
+ class Words
9
+ attr_reader :singulars, :plurals
10
+
11
+ def initialize
12
+ @singulars, @singular_rules = {}, []
13
+ @plurals, @plural_rules = {}, []
14
+ end
15
+
16
+ def add(singular, plural = nil)
17
+ plural = singular unless plural
18
+
19
+ @plurals[singular] = plural
20
+ @singulars[plural] = singular
21
+ end
22
+
23
+ def inflect(singular, plural)
24
+ add_singular_rule(singular, plural)
25
+ add_plural_rule(singular, plural)
26
+ end
27
+
28
+ def add_singular_rule(singular, plural)
29
+ @singular_rules << [plural, singular]
30
+ end
31
+
32
+ def add_plural_rule(singular, plural)
33
+ @plural_rules << [singular, plural]
34
+ end
35
+
36
+ def write_singular_rules
37
+ return @singular_inflections if @singular_inflections
38
+
39
+ rex = /(#{@singular_rules.map { |pl, si| pl }.join('|')})$/i
40
+ hash = Hash[*@singular_rules.flatten]
41
+
42
+ @singular_inflections = [rex, hash]
43
+ end
44
+
45
+ def write_plural_rules
46
+ return @plural_inflections if @plural_inflections
47
+
48
+ rex = /(#{@plural_rules.map { |si, pl| si }.join('|')})$/i
49
+ hash = Hash[*@plural_rules.flatten]
50
+
51
+ @plural_inflections = [rex, hash]
52
+ end
53
+
54
+ def self.instance
55
+ @__instance__ ||= new
56
+ end
57
+ end
58
+
59
+ def words
60
+ if block_given?
61
+ yield Words.instance
62
+ else
63
+ Words.instance
64
+ end
65
+ end
66
+
67
+ def camelize(s)
68
+ uncapitalize pascalize(s)
69
+ end
70
+
71
+ def pascalize(s)
72
+ return s if s !~ /_/ && s =~ /[A-Z]+.*/
73
+ s.split('_').map { |e| e.capitalize }.join
74
+ end
75
+
76
+ def constantize(s)
77
+ const = Object
78
+ s.split('::').each do |name|
79
+ const = const.const_get(name)
80
+ end
81
+
82
+ const
83
+ end
84
+
85
+ def underscore(s)
86
+ return s.downcase if s =~ /^[A-Z]+$/
87
+ s.gsub(/([A-Z]+)(?=[A-Z][a-z]?)|\B[A-Z]/, '_\&') =~ /_*(.*)/
88
+ return $+.downcase
89
+ end
90
+
91
+ def pluralize(s)
92
+ inflect_word(s, words.plurals, words.write_plural_rules)
93
+ end
94
+
95
+ def singularize(s)
96
+ inflect_word(s, words.singulars, words.write_singular_rules)
97
+ end
98
+
99
+ def uncapitalize(s)
100
+ s[0, 1].downcase + s[1..-1]
101
+ end
102
+
103
+ private
104
+
105
+ def inflect_word(word, collection, rules)
106
+ return "" if word.empty?
107
+
108
+ if r = collection[word]
109
+ return r.dup
110
+ end
111
+
112
+ inflected_word = word.dup
113
+ rex, hash = rules
114
+
115
+ inflected_word.sub!(rex) { |m| hash[m] }
116
+ collection[word] = inflected_word
117
+ inflected_word
118
+ end
119
+
120
+ words do |word|
121
+ word.add 'fish'
122
+ word.add 'wife', 'wives'
123
+
124
+ word.inflect 'ox', 'oxes'
125
+ word.inflect 'us', 'uses'
126
+ word.inflect '', 's'
127
+ word.inflect 'ero', 'eroes'
128
+ word.inflect 'rf', 'rves'
129
+ word.inflect 'af', 'aves'
130
+ word.inflect 'ero', 'eroes'
131
+ word.inflect 'man', 'men'
132
+ word.inflect 'ch', 'ches'
133
+ word.inflect 'sh', 'shes'
134
+ word.inflect 'ss', 'sses'
135
+ word.inflect 'ta', 'tum'
136
+ word.inflect 'ia', 'ium'
137
+ word.inflect 'ra', 'rum'
138
+ word.inflect 'ay', 'ays'
139
+ word.inflect 'ey', 'eys'
140
+ word.inflect 'oy', 'oys'
141
+ word.inflect 'uy', 'uys'
142
+ word.inflect 'y', 'ies'
143
+ word.inflect 'x', 'xes'
144
+ word.inflect 'lf', 'lves'
145
+ word.inflect 'ffe', 'ffes'
146
+ word.inflect 'afe', 'aves'
147
+ word.inflect 'ouse', 'ouses'
148
+ end
149
+ end
@@ -0,0 +1,33 @@
1
+ module Inflector
2
+ module CoreExtensions
3
+ module String
4
+ def pluralize
5
+ Inflector.pluralize(self)
6
+ end
7
+
8
+ def singularize
9
+ Inflector.singularize(self)
10
+ end
11
+
12
+ def camelize
13
+ Inflector.camelize(self)
14
+ end
15
+
16
+ def underscore
17
+ Inflector.underscore(self)
18
+ end
19
+
20
+ def pascalize
21
+ Inflector.pascalize(self)
22
+ end
23
+
24
+ def constantize
25
+ Inflector.constantize(self)
26
+ end
27
+
28
+ def uncapitalize
29
+ Inflector.uncapitalize(self)
30
+ end
31
+ end
32
+ end
33
+ end
metadata ADDED
@@ -0,0 +1,47 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: inflector
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Thomas Monzel
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-22 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: A simple class for plural-, singular-, camel-, pascal-, constantization
15
+ and underscore
16
+ email: tom.monzel@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - lib/inflector.rb
22
+ - lib/inflector/core_ext/string.rb
23
+ homepage: https://github.com/tmonzel/inflector
24
+ licenses: []
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ! '>='
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ none: false
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ requirements: []
42
+ rubyforge_project:
43
+ rubygems_version: 1.8.24
44
+ signing_key:
45
+ specification_version: 3
46
+ summary: English inflections
47
+ test_files: []