garterbelt 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/TODO +3 -0
- data/VERSION +1 -1
- data/garterbelt.gemspec +1 -1
- data/lib/support/string.rb +158 -150
- metadata +3 -3
data/TODO
CHANGED
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.4
|
data/garterbelt.gemspec
CHANGED
data/lib/support/string.rb
CHANGED
@@ -1,165 +1,173 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
unless String.instance_methods.include?('underscore')
|
2
|
+
begin
|
3
|
+
# being selective if activesupport 3.0.x
|
4
|
+
require 'active_support/inflector/methods'
|
5
|
+
require 'active_support/inflector/inflections'
|
3
6
|
|
4
|
-
# including the string extension directly require i18l gem, which is lame when not needed
|
5
|
-
|
6
|
-
|
7
|
-
#
|
8
|
-
#
|
9
|
-
#
|
7
|
+
# including the string extension directly require i18l gem, which is lame when not needed :(
|
8
|
+
|
9
|
+
|
10
|
+
# String inflections define new methods on the String class to transform names for different purposes.
|
11
|
+
# For instance, you can figure out the name of a database from the name of a class.
|
12
|
+
#
|
13
|
+
# "ScaleScore".tableize # => "scale_scores"
|
14
|
+
#
|
15
|
+
class String
|
16
|
+
# Returns the plural form of the word in the string.
|
17
|
+
#
|
18
|
+
# "post".pluralize # => "posts"
|
19
|
+
# "octopus".pluralize # => "octopi"
|
20
|
+
# "sheep".pluralize # => "sheep"
|
21
|
+
# "words".pluralize # => "words"
|
22
|
+
# "the blue mailman".pluralize # => "the blue mailmen"
|
23
|
+
# "CamelOctopus".pluralize # => "CamelOctopi"
|
24
|
+
def pluralize
|
25
|
+
ActiveSupport::Inflector.pluralize(self)
|
26
|
+
end
|
10
27
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
end
|
28
|
+
# The reverse of +pluralize+, returns the singular form of a word in a string.
|
29
|
+
#
|
30
|
+
# "posts".singularize # => "post"
|
31
|
+
# "octopi".singularize # => "octopus"
|
32
|
+
# "sheep".singularize # => "sheep"
|
33
|
+
# "word".singularize # => "word"
|
34
|
+
# "the blue mailmen".singularize # => "the blue mailman"
|
35
|
+
# "CamelOctopi".singularize # => "CamelOctopus"
|
36
|
+
def singularize
|
37
|
+
ActiveSupport::Inflector.singularize(self)
|
38
|
+
end
|
23
39
|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
end
|
40
|
+
# +constantize+ tries to find a declared constant with the name specified
|
41
|
+
# in the string. It raises a NameError when the name is not in CamelCase
|
42
|
+
# or is not initialized.
|
43
|
+
#
|
44
|
+
# Examples
|
45
|
+
# "Module".constantize # => Module
|
46
|
+
# "Class".constantize # => Class
|
47
|
+
def constantize
|
48
|
+
ActiveSupport::Inflector.constantize(self)
|
49
|
+
end
|
35
50
|
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
51
|
+
# By default, +camelize+ converts strings to UpperCamelCase. If the argument to camelize
|
52
|
+
# is set to <tt>:lower</tt> then camelize produces lowerCamelCase.
|
53
|
+
#
|
54
|
+
# +camelize+ will also convert '/' to '::' which is useful for converting paths to namespaces.
|
55
|
+
#
|
56
|
+
# "active_record".camelize # => "ActiveRecord"
|
57
|
+
# "active_record".camelize(:lower) # => "activeRecord"
|
58
|
+
# "active_record/errors".camelize # => "ActiveRecord::Errors"
|
59
|
+
# "active_record/errors".camelize(:lower) # => "activeRecord::Errors"
|
60
|
+
def camelize(first_letter = :upper)
|
61
|
+
case first_letter
|
62
|
+
when :upper then ActiveSupport::Inflector.camelize(self, true)
|
63
|
+
when :lower then ActiveSupport::Inflector.camelize(self, false)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
alias_method :camelcase, :camelize
|
46
67
|
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
when :lower then ActiveSupport::Inflector.camelize(self, false)
|
60
|
-
end
|
61
|
-
end
|
62
|
-
alias_method :camelcase, :camelize
|
68
|
+
# Capitalizes all the words and replaces some characters in the string to create
|
69
|
+
# a nicer looking title. +titleize+ is meant for creating pretty output. It is not
|
70
|
+
# used in the Rails internals.
|
71
|
+
#
|
72
|
+
# +titleize+ is also aliased as +titlecase+.
|
73
|
+
#
|
74
|
+
# "man from the boondocks".titleize # => "Man From The Boondocks"
|
75
|
+
# "x-men: the last stand".titleize # => "X Men: The Last Stand"
|
76
|
+
def titleize
|
77
|
+
ActiveSupport::Inflector.titleize(self)
|
78
|
+
end
|
79
|
+
alias_method :titlecase, :titleize
|
63
80
|
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
ActiveSupport::Inflector.titleize(self)
|
74
|
-
end
|
75
|
-
alias_method :titlecase, :titleize
|
81
|
+
# The reverse of +camelize+. Makes an underscored, lowercase form from the expression in the string.
|
82
|
+
#
|
83
|
+
# +underscore+ will also change '::' to '/' to convert namespaces to paths.
|
84
|
+
#
|
85
|
+
# "ActiveRecord".underscore # => "active_record"
|
86
|
+
# "ActiveRecord::Errors".underscore # => active_record/errors
|
87
|
+
def underscore
|
88
|
+
ActiveSupport::Inflector.underscore(self)
|
89
|
+
end
|
76
90
|
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
def underscore
|
84
|
-
ActiveSupport::Inflector.underscore(self)
|
85
|
-
end
|
91
|
+
# Replaces underscores with dashes in the string.
|
92
|
+
#
|
93
|
+
# "puni_puni" # => "puni-puni"
|
94
|
+
def dasherize
|
95
|
+
ActiveSupport::Inflector.dasherize(self)
|
96
|
+
end
|
86
97
|
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
98
|
+
# Removes the module part from the constant expression in the string.
|
99
|
+
#
|
100
|
+
# "ActiveRecord::CoreExtensions::String::Inflections".demodulize # => "Inflections"
|
101
|
+
# "Inflections".demodulize # => "Inflections"
|
102
|
+
def demodulize
|
103
|
+
ActiveSupport::Inflector.demodulize(self)
|
104
|
+
end
|
93
105
|
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
106
|
+
# Replaces special characters in a string so that it may be used as part of a 'pretty' URL.
|
107
|
+
#
|
108
|
+
# ==== Examples
|
109
|
+
#
|
110
|
+
# class Person
|
111
|
+
# def to_param
|
112
|
+
# "#{id}-#{name.parameterize}"
|
113
|
+
# end
|
114
|
+
# end
|
115
|
+
#
|
116
|
+
# @person = Person.find(1)
|
117
|
+
# # => #<Person id: 1, name: "Donald E. Knuth">
|
118
|
+
#
|
119
|
+
# <%= link_to(@person.name, person_path %>
|
120
|
+
# # => <a href="/person/1-donald-e-knuth">Donald E. Knuth</a>
|
121
|
+
def parameterize(sep = '-')
|
122
|
+
ActiveSupport::Inflector.parameterize(self, sep)
|
123
|
+
end
|
101
124
|
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
#
|
112
|
-
# @person = Person.find(1)
|
113
|
-
# # => #<Person id: 1, name: "Donald E. Knuth">
|
114
|
-
#
|
115
|
-
# <%= link_to(@person.name, person_path %>
|
116
|
-
# # => <a href="/person/1-donald-e-knuth">Donald E. Knuth</a>
|
117
|
-
def parameterize(sep = '-')
|
118
|
-
ActiveSupport::Inflector.parameterize(self, sep)
|
119
|
-
end
|
125
|
+
# Creates the name of a table like Rails does for models to table names. This method
|
126
|
+
# uses the +pluralize+ method on the last word in the string.
|
127
|
+
#
|
128
|
+
# "RawScaledScorer".tableize # => "raw_scaled_scorers"
|
129
|
+
# "egg_and_ham".tableize # => "egg_and_hams"
|
130
|
+
# "fancyCategory".tableize # => "fancy_categories"
|
131
|
+
def tableize
|
132
|
+
ActiveSupport::Inflector.tableize(self)
|
133
|
+
end
|
120
134
|
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
135
|
+
# Create a class name from a plural table name like Rails does for table names to models.
|
136
|
+
# Note that this returns a string and not a class. (To convert to an actual class
|
137
|
+
# follow +classify+ with +constantize+.)
|
138
|
+
#
|
139
|
+
# "egg_and_hams".classify # => "EggAndHam"
|
140
|
+
# "posts".classify # => "Post"
|
141
|
+
#
|
142
|
+
# Singular names are not handled correctly.
|
143
|
+
#
|
144
|
+
# "business".classify # => "Busines"
|
145
|
+
def classify
|
146
|
+
ActiveSupport::Inflector.classify(self)
|
147
|
+
end
|
130
148
|
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
#
|
140
|
-
# "business".classify # => "Busines"
|
141
|
-
def classify
|
142
|
-
ActiveSupport::Inflector.classify(self)
|
143
|
-
end
|
149
|
+
# Capitalizes the first word, turns underscores into spaces, and strips '_id'.
|
150
|
+
# Like +titleize+, this is meant for creating pretty output.
|
151
|
+
#
|
152
|
+
# "employee_salary" # => "Employee salary"
|
153
|
+
# "author_id" # => "Author"
|
154
|
+
def humanize
|
155
|
+
ActiveSupport::Inflector.humanize(self)
|
156
|
+
end
|
144
157
|
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
# "Message".foreign_key # => "message_id"
|
160
|
-
# "Message".foreign_key(false) # => "messageid"
|
161
|
-
# "Admin::Post".foreign_key # => "post_id"
|
162
|
-
def foreign_key(separate_class_name_and_id_with_underscore = true)
|
163
|
-
ActiveSupport::Inflector.foreign_key(self, separate_class_name_and_id_with_underscore)
|
158
|
+
# Creates a foreign key name from a class name.
|
159
|
+
# +separate_class_name_and_id_with_underscore+ sets whether
|
160
|
+
# the method should put '_' between the name and 'id'.
|
161
|
+
#
|
162
|
+
# Examples
|
163
|
+
# "Message".foreign_key # => "message_id"
|
164
|
+
# "Message".foreign_key(false) # => "messageid"
|
165
|
+
# "Admin::Post".foreign_key # => "post_id"
|
166
|
+
def foreign_key(separate_class_name_and_id_with_underscore = true)
|
167
|
+
ActiveSupport::Inflector.foreign_key(self, separate_class_name_and_id_with_underscore)
|
168
|
+
end
|
169
|
+
end
|
170
|
+
rescue Exception => e
|
171
|
+
require 'activesupport'
|
164
172
|
end
|
165
173
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: garterbelt
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 23
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 4
|
10
|
+
version: 0.0.4
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Kane Baccigalupi
|