swiss_db 0.4.3 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +10 -6
- data/lib/motion-support/array.rb +7 -0
- data/lib/motion-support/inflections.rb +227 -0
- data/lib/motion-support/inflector.rb +322 -0
- data/lib/motion-support/methods.rb +322 -0
- data/lib/motion-support/string.rb +178 -0
- data/lib/swiss_db/swiss_model.rb +19 -3
- data/lib/swiss_db.rb +5 -1
- metadata +6 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e116c10e8cc3bd87e03a39c0070503942d9705e5
|
4
|
+
data.tar.gz: 772ccee76d341e64f1d86cdaa6270ef3edef91a2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9ec1a9330cc0b83769f4e07e710679ee1ab06f8ef2b1598d79806877b7a6b8213a8a24d7861d8885e85271ecf0f53ddc49c93f48cd38ba26dc4c6077b439b1b7
|
7
|
+
data.tar.gz: c222afa056499b831c5547c57a8777d1d4de71f60e74a243ebb91a02900edaeb958588519b5e1c51e93c6b5c5e826c98703b7ca99782d20b4a67e847aa49eb8c
|
data/README.md
CHANGED
@@ -20,12 +20,10 @@ Or install it yourself as:
|
|
20
20
|
|
21
21
|
## Usage
|
22
22
|
|
23
|
-
Schemas are the exact same from CoreDataQuery and go in the same place.
|
23
|
+
Schemas are the exact same from CoreDataQuery and go in the same place. (schemas/)
|
24
24
|
|
25
|
-
|
25
|
+
Schema name does nothing.
|
26
26
|
|
27
|
-
```ruby
|
28
|
-
app.files += Dir.glob("schemas/*.rb")
|
29
27
|
```
|
30
28
|
|
31
29
|
Models are as such:
|
@@ -33,8 +31,8 @@ Models are as such:
|
|
33
31
|
```ruby
|
34
32
|
class Model < SwissModel
|
35
33
|
|
36
|
-
|
37
|
-
set_primary_key "primary_key_name"
|
34
|
+
set_class_name "Model" # there are currently no hacks to automatically get this. sorry.
|
35
|
+
set_primary_key "primary_key_name" # if not set, will default to "id"
|
38
36
|
|
39
37
|
end
|
40
38
|
```
|
@@ -54,6 +52,12 @@ end
|
|
54
52
|
|
55
53
|
That's it! #all, #last, #first, #count, #save, #update_attributes and the usual are now available!
|
56
54
|
|
55
|
+
KNOWN LIMITATION: This ORM compiles in the database name and the database version as a constant. Unfortunately I don't know of a way around this yet. This means no DB migrations yet by doing the simple version bump that is supported by Android. If we get a way to configure these from outside the gem, it will open up possibilities such as multiple schemas and migrations. To get around this simply delete your local database when you need to migrate. You can delete the app from the simulator/device (probably) or use my convenience command:
|
56
|
+
|
57
|
+
```ruby
|
58
|
+
DataStore.new($app_context).drop_db #=> true if the DB was dropped, false if not
|
59
|
+
```
|
60
|
+
|
57
61
|
## Development
|
58
62
|
|
59
63
|
After checking out the repo, run `bin/setup` to install dependencies.
|
@@ -0,0 +1,227 @@
|
|
1
|
+
module MotionSupport
|
2
|
+
module Inflector
|
3
|
+
class << self
|
4
|
+
|
5
|
+
# A singleton instance of this class is yielded by Inflector.inflections,
|
6
|
+
# which can then be used to specify additional inflection rules.
|
7
|
+
#
|
8
|
+
# MotionSupport::Inflector.inflections do |inflect|
|
9
|
+
# inflect.plural /^(ox)$/i, '\1\2en'
|
10
|
+
# inflect.singular /^(ox)en/i, '\1'
|
11
|
+
#
|
12
|
+
# inflect.irregular 'octopus', 'octopi'
|
13
|
+
#
|
14
|
+
# inflect.uncountable 'equipment'
|
15
|
+
# end
|
16
|
+
#
|
17
|
+
# New rules are added at the top. So in the example above, the irregular
|
18
|
+
# rule for octopus will now be the first of the pluralization and
|
19
|
+
# singularization rules that is runs. This guarantees that your rules run
|
20
|
+
# before any of the rules that may already have been loaded.
|
21
|
+
class Inflections
|
22
|
+
def self.instance
|
23
|
+
@__instance__ ||= new
|
24
|
+
end
|
25
|
+
|
26
|
+
# attr_reader :plurals, :singulars, :uncountables, :humans, :acronyms, :acronym_regex
|
27
|
+
|
28
|
+
def initialize
|
29
|
+
@plurals, @singulars, @uncountables, @humans, @acronyms, @acronym_regex = [], [], [], [], {}, /(?=a)b/
|
30
|
+
end
|
31
|
+
|
32
|
+
# Private, for the test suite.
|
33
|
+
def initialize_dup(orig) # :nodoc:
|
34
|
+
%w(plurals singulars uncountables humans acronyms acronym_regex).each do |scope|
|
35
|
+
instance_variable_set("@#{scope}", orig.send(scope).dup)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def plurals
|
40
|
+
@plurals
|
41
|
+
end
|
42
|
+
|
43
|
+
def singulars
|
44
|
+
@singulars
|
45
|
+
end
|
46
|
+
|
47
|
+
def uncountables
|
48
|
+
@uncountables
|
49
|
+
end
|
50
|
+
|
51
|
+
def humans
|
52
|
+
@humans
|
53
|
+
end
|
54
|
+
|
55
|
+
def acronyms
|
56
|
+
@acronyms
|
57
|
+
end
|
58
|
+
|
59
|
+
def acronym_regex
|
60
|
+
@acronym_regex
|
61
|
+
end
|
62
|
+
|
63
|
+
# Specifies a new acronym. An acronym must be specified as it will appear
|
64
|
+
# in a camelized string. An underscore string that contains the acronym
|
65
|
+
# will retain the acronym when passed to +camelize+, +humanize+, or
|
66
|
+
# +titleize+. A camelized string that contains the acronym will maintain
|
67
|
+
# the acronym when titleized or humanized, and will convert the acronym
|
68
|
+
# into a non-delimited single lowercase word when passed to +underscore+.
|
69
|
+
#
|
70
|
+
# acronym 'HTML'
|
71
|
+
# titleize 'html' #=> 'HTML'
|
72
|
+
# camelize 'html' #=> 'HTML'
|
73
|
+
# underscore 'MyHTML' #=> 'my_html'
|
74
|
+
#
|
75
|
+
# The acronym, however, must occur as a delimited unit and not be part of
|
76
|
+
# another word for conversions to recognize it:
|
77
|
+
#
|
78
|
+
# acronym 'HTTP'
|
79
|
+
# camelize 'my_http_delimited' #=> 'MyHTTPDelimited'
|
80
|
+
# camelize 'https' #=> 'Https', not 'HTTPs'
|
81
|
+
# underscore 'HTTPS' #=> 'http_s', not 'https'
|
82
|
+
#
|
83
|
+
# acronym 'HTTPS'
|
84
|
+
# camelize 'https' #=> 'HTTPS'
|
85
|
+
# underscore 'HTTPS' #=> 'https'
|
86
|
+
#
|
87
|
+
# Note: Acronyms that are passed to +pluralize+ will no longer be
|
88
|
+
# recognized, since the acronym will not occur as a delimited unit in the
|
89
|
+
# pluralized result. To work around this, you must specify the pluralized
|
90
|
+
# form as an acronym as well:
|
91
|
+
#
|
92
|
+
# acronym 'API'
|
93
|
+
# camelize(pluralize('api')) #=> 'Apis'
|
94
|
+
#
|
95
|
+
# acronym 'APIs'
|
96
|
+
# camelize(pluralize('api')) #=> 'APIs'
|
97
|
+
#
|
98
|
+
# +acronym+ may be used to specify any word that contains an acronym or
|
99
|
+
# otherwise needs to maintain a non-standard capitalization. The only
|
100
|
+
# restriction is that the word must begin with a capital letter.
|
101
|
+
#
|
102
|
+
# acronym 'RESTful'
|
103
|
+
# underscore 'RESTful' #=> 'restful'
|
104
|
+
# underscore 'RESTfulController' #=> 'restful_controller'
|
105
|
+
# titleize 'RESTfulController' #=> 'RESTful Controller'
|
106
|
+
# camelize 'restful' #=> 'RESTful'
|
107
|
+
# camelize 'restful_controller' #=> 'RESTfulController'
|
108
|
+
#
|
109
|
+
# acronym 'McDonald'
|
110
|
+
# underscore 'McDonald' #=> 'mcdonald'
|
111
|
+
# camelize 'mcdonald' #=> 'McDonald'
|
112
|
+
def acronym(word)
|
113
|
+
@acronyms[word.downcase] = word
|
114
|
+
@acronym_regex = /#{@acronyms.values.join("|")}/
|
115
|
+
end
|
116
|
+
|
117
|
+
# Specifies a new pluralization rule and its replacement. The rule can
|
118
|
+
# either be a string or a regular expression. The replacement should
|
119
|
+
# always be a string that may include references to the matched data from
|
120
|
+
# the rule.
|
121
|
+
def plural(rule, replacement)
|
122
|
+
@uncountables.delete(rule) if rule.is_a?(String)
|
123
|
+
@uncountables.delete(replacement)
|
124
|
+
@plurals.prepend([rule, replacement])
|
125
|
+
end
|
126
|
+
|
127
|
+
# Specifies a new singularization rule and its replacement. The rule can
|
128
|
+
# either be a string or a regular expression. The replacement should
|
129
|
+
# always be a string that may include references to the matched data from
|
130
|
+
# the rule.
|
131
|
+
def singular(rule, replacement)
|
132
|
+
@uncountables.delete(rule) if rule.is_a?(String)
|
133
|
+
@uncountables.delete(replacement)
|
134
|
+
@singulars.prepend([rule, replacement])
|
135
|
+
end
|
136
|
+
|
137
|
+
# Specifies a new irregular that applies to both pluralization and
|
138
|
+
# singularization at the same time. This can only be used for strings, not
|
139
|
+
# regular expressions. You simply pass the irregular in singular and
|
140
|
+
# plural form.
|
141
|
+
#
|
142
|
+
# irregular 'octopus', 'octopi'
|
143
|
+
# irregular 'person', 'people'
|
144
|
+
def irregular(singular, plural)
|
145
|
+
@uncountables.delete(singular)
|
146
|
+
@uncountables.delete(plural)
|
147
|
+
|
148
|
+
s0 = singular[0]
|
149
|
+
srest = singular[1..-1]
|
150
|
+
|
151
|
+
p0 = plural[0]
|
152
|
+
prest = plural[1..-1]
|
153
|
+
|
154
|
+
if s0.upcase == p0.upcase
|
155
|
+
plural(/(#{s0})#{srest}$/i, '\1' + prest)
|
156
|
+
plural(/(#{p0})#{prest}$/i, '\1' + prest)
|
157
|
+
|
158
|
+
singular(/(#{s0})#{srest}$/i, '\1' + srest)
|
159
|
+
singular(/(#{p0})#{prest}$/i, '\1' + srest)
|
160
|
+
else
|
161
|
+
plural(/#{s0.upcase}(?i)#{srest}$/, p0.upcase + prest)
|
162
|
+
plural(/#{s0.downcase}(?i)#{srest}$/, p0.downcase + prest)
|
163
|
+
plural(/#{p0.upcase}(?i)#{prest}$/, p0.upcase + prest)
|
164
|
+
plural(/#{p0.downcase}(?i)#{prest}$/, p0.downcase + prest)
|
165
|
+
|
166
|
+
singular(/#{s0.upcase}(?i)#{srest}$/, s0.upcase + srest)
|
167
|
+
singular(/#{s0.downcase}(?i)#{srest}$/, s0.downcase + srest)
|
168
|
+
singular(/#{p0.upcase}(?i)#{prest}$/, s0.upcase + srest)
|
169
|
+
singular(/#{p0.downcase}(?i)#{prest}$/, s0.downcase + srest)
|
170
|
+
end
|
171
|
+
end
|
172
|
+
|
173
|
+
# Add uncountable words that shouldn't be attempted inflected.
|
174
|
+
#
|
175
|
+
# uncountable 'money'
|
176
|
+
# uncountable 'money', 'information'
|
177
|
+
# uncountable %w( money information rice )
|
178
|
+
def uncountable(*words)
|
179
|
+
(@uncountables << words).flatten!
|
180
|
+
end
|
181
|
+
|
182
|
+
# Specifies a humanized form of a string by a regular expression rule or
|
183
|
+
# by a string mapping. When using a regular expression based replacement,
|
184
|
+
# the normal humanize formatting is called after the replacement. When a
|
185
|
+
# string is used, the human form should be specified as desired (example:
|
186
|
+
# 'The name', not 'the_name').
|
187
|
+
#
|
188
|
+
# human /_cnt$/i, '\1_count'
|
189
|
+
# human 'legacy_col_person_name', 'Name'
|
190
|
+
def human(rule, replacement)
|
191
|
+
@humans.prepend([rule, replacement])
|
192
|
+
end
|
193
|
+
|
194
|
+
# Clears the loaded inflections within a given scope (default is
|
195
|
+
# <tt>:all</tt>). Give the scope as a symbol of the inflection type, the
|
196
|
+
# options are: <tt>:plurals</tt>, <tt>:singulars</tt>, <tt>:uncountables</tt>,
|
197
|
+
# <tt>:humans</tt>.
|
198
|
+
#
|
199
|
+
# clear :all
|
200
|
+
# clear :plurals
|
201
|
+
def clear(scope = :all)
|
202
|
+
case scope
|
203
|
+
when :all
|
204
|
+
@plurals, @singulars, @uncountables, @humans = [], [], [], []
|
205
|
+
else
|
206
|
+
instance_variable_set "@#{scope}", []
|
207
|
+
end
|
208
|
+
end
|
209
|
+
end
|
210
|
+
|
211
|
+
# Yields a singleton instance of Inflector::Inflections so you can specify
|
212
|
+
# additional inflector rules.
|
213
|
+
#
|
214
|
+
# MotionSupport::Inflector.inflections do |inflect|
|
215
|
+
# inflect.uncountable 'rails'
|
216
|
+
# end
|
217
|
+
def inflections
|
218
|
+
if block_given?
|
219
|
+
yield Inflections.instance
|
220
|
+
else
|
221
|
+
Inflections.instance
|
222
|
+
end
|
223
|
+
end
|
224
|
+
|
225
|
+
end
|
226
|
+
end
|
227
|
+
end
|
@@ -0,0 +1,322 @@
|
|
1
|
+
module MotionSupport
|
2
|
+
# The Inflector transforms words from singular to plural, class names to table
|
3
|
+
# names, modularized class names to ones without, and class names to foreign
|
4
|
+
# keys. The default inflections for pluralization, singularization, and
|
5
|
+
# uncountable words are kept in inflections.rb.
|
6
|
+
module Inflector
|
7
|
+
class << self
|
8
|
+
|
9
|
+
# Returns the plural form of the word in the string.
|
10
|
+
#
|
11
|
+
# 'post'.pluralize # => "posts"
|
12
|
+
# 'octopus'.pluralize # => "octopi"
|
13
|
+
# 'sheep'.pluralize # => "sheep"
|
14
|
+
# 'words'.pluralize # => "words"
|
15
|
+
# 'CamelOctopus'.pluralize # => "CamelOctopi"
|
16
|
+
def pluralize(word)
|
17
|
+
apply_inflections(word, inflections.plurals)
|
18
|
+
end
|
19
|
+
|
20
|
+
# The reverse of +pluralize+, returns the singular form of a word in a
|
21
|
+
# string.
|
22
|
+
#
|
23
|
+
# 'posts'.singularize # => "post"
|
24
|
+
# 'octopi'.singularize # => "octopus"
|
25
|
+
# 'sheep'.singularize # => "sheep"
|
26
|
+
# 'word'.singularize # => "word"
|
27
|
+
# 'CamelOctopi'.singularize # => "CamelOctopus"
|
28
|
+
def singularize(word)
|
29
|
+
apply_inflections(word, inflections.singulars)
|
30
|
+
end
|
31
|
+
|
32
|
+
# By default, +camelize+ converts strings to UpperCamelCase. If the argument
|
33
|
+
# to +camelize+ is set to <tt>:lower</tt> then +camelize+ produces
|
34
|
+
# lowerCamelCase.
|
35
|
+
#
|
36
|
+
# +camelize+ will also convert '/' to '::' which is useful for converting
|
37
|
+
# paths to namespaces.
|
38
|
+
#
|
39
|
+
# 'active_model'.camelize # => "ActiveModel"
|
40
|
+
# 'active_model'.camelize(:lower) # => "activeModel"
|
41
|
+
# 'active_model/errors'.camelize # => "ActiveModel::Errors"
|
42
|
+
# 'active_model/errors'.camelize(:lower) # => "activeModel::Errors"
|
43
|
+
#
|
44
|
+
# As a rule of thumb you can think of +camelize+ as the inverse of
|
45
|
+
# +underscore+, though there are cases where that does not hold:
|
46
|
+
#
|
47
|
+
# 'SSLError'.underscore.camelize # => "SslError"
|
48
|
+
def camelize(term, uppercase_first_letter = true)
|
49
|
+
string = term.to_s
|
50
|
+
if uppercase_first_letter
|
51
|
+
string = string.sub(/^[a-z\d]*/) { |match| inflections.acronyms[match] || match.capitalize }
|
52
|
+
else
|
53
|
+
string = string.sub(/^(?:#{inflections.acronym_regex}(?=\b|[A-Z_])|\w)/) { |match| match.downcase }
|
54
|
+
end
|
55
|
+
string.gsub(/(?:_|(\/))([a-z\d]*)/i) { "#{$1}#{inflections.acronyms[$2] || $2.capitalize}" }.gsub('/', '::')
|
56
|
+
end
|
57
|
+
|
58
|
+
# Makes an underscored, lowercase form from the expression in the string.
|
59
|
+
#
|
60
|
+
# Changes '::' to '/' to convert namespaces to paths.
|
61
|
+
#
|
62
|
+
# 'ActiveModel'.underscore # => "active_model"
|
63
|
+
# 'ActiveModel::Errors'.underscore # => "active_model/errors"
|
64
|
+
#
|
65
|
+
# As a rule of thumb you can think of +underscore+ as the inverse of
|
66
|
+
# +camelize+, though there are cases where that does not hold:
|
67
|
+
#
|
68
|
+
# 'SSLError'.underscore.camelize # => "SslError"
|
69
|
+
def underscore(camel_cased_word)
|
70
|
+
word = camel_cased_word.to_s.dup
|
71
|
+
word.gsub!('::', '/')
|
72
|
+
word.gsub!(/(?:([A-Za-z\d])|^)(#{inflections.acronym_regex})(?=\b|[^a-z])/) { "#{$1}#{$1 && '_'}#{$2.downcase}" }
|
73
|
+
word.gsub!(/([A-Z\d]+)([A-Z][a-z])/,'\1_\2')
|
74
|
+
word.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
|
75
|
+
word.gsub!("-", "_")
|
76
|
+
word.downcase!
|
77
|
+
word
|
78
|
+
end
|
79
|
+
|
80
|
+
# Capitalizes the first word and turns underscores into spaces and strips a
|
81
|
+
# trailing "_id", if any. Like +titleize+, this is meant for creating pretty
|
82
|
+
# output.
|
83
|
+
#
|
84
|
+
# 'employee_salary'.humanize # => "Employee salary"
|
85
|
+
# 'author_id'.humanize # => "Author"
|
86
|
+
def humanize(lower_case_and_underscored_word)
|
87
|
+
result = lower_case_and_underscored_word.to_s.dup
|
88
|
+
inflections.humans.each { |(rule, replacement)| break if result.sub!(rule, replacement) }
|
89
|
+
result.gsub!(/_id$/, "")
|
90
|
+
result.gsub!('_', ' ')
|
91
|
+
result.gsub(/([a-z\d]*)/i) { |match|
|
92
|
+
"#{inflections.acronyms[match] || match.downcase}"
|
93
|
+
}.gsub(/^\w/) { |match| match.upcase }
|
94
|
+
end
|
95
|
+
|
96
|
+
# Capitalizes all the words and replaces some characters in the string to
|
97
|
+
# create a nicer looking title. +titleize+ is meant for creating pretty
|
98
|
+
# output. It is not used in the Rails internals.
|
99
|
+
#
|
100
|
+
# +titleize+ is also aliased as +titlecase+.
|
101
|
+
#
|
102
|
+
# 'man from the boondocks'.titleize # => "Man From The Boondocks"
|
103
|
+
# 'x-men: the last stand'.titleize # => "X Men: The Last Stand"
|
104
|
+
# 'TheManWithoutAPast'.titleize # => "The Man Without A Past"
|
105
|
+
# 'raiders_of_the_lost_ark'.titleize # => "Raiders Of The Lost Ark"
|
106
|
+
def titleize(word)
|
107
|
+
humanize(underscore(word)).gsub(/\b(?<!['’`])[a-z]/) { |match| match.capitalize }
|
108
|
+
end
|
109
|
+
|
110
|
+
# Create the name of a table like Rails does for models to table names. This
|
111
|
+
# method uses the +pluralize+ method on the last word in the string.
|
112
|
+
#
|
113
|
+
# 'RawScaledScorer'.tableize # => "raw_scaled_scorers"
|
114
|
+
# 'egg_and_ham'.tableize # => "egg_and_hams"
|
115
|
+
# 'fancyCategory'.tableize # => "fancy_categories"
|
116
|
+
def tableize(class_name)
|
117
|
+
pluralize(underscore(class_name))
|
118
|
+
end
|
119
|
+
|
120
|
+
# Create a class name from a plural table name like Rails does for table
|
121
|
+
# names to models. Note that this returns a string and not a Class (To
|
122
|
+
# convert to an actual class follow +classify+ with +constantize+).
|
123
|
+
#
|
124
|
+
# 'egg_and_hams'.classify # => "EggAndHam"
|
125
|
+
# 'posts'.classify # => "Post"
|
126
|
+
#
|
127
|
+
# Singular names are not handled correctly:
|
128
|
+
#
|
129
|
+
# 'business'.classify # => "Busines"
|
130
|
+
def classify(table_name)
|
131
|
+
# strip out any leading schema name
|
132
|
+
camelize(singularize(table_name.to_s.sub(/.*\./, '')))
|
133
|
+
end
|
134
|
+
|
135
|
+
# Replaces underscores with dashes in the string.
|
136
|
+
#
|
137
|
+
# 'puni_puni'.dasherize # => "puni-puni"
|
138
|
+
def dasherize(underscored_word)
|
139
|
+
underscored_word.gsub('_', '-')
|
140
|
+
end
|
141
|
+
|
142
|
+
# Removes the module part from the expression in the string.
|
143
|
+
#
|
144
|
+
# 'ActiveRecord::CoreExtensions::String::Inflections'.demodulize # => "Inflections"
|
145
|
+
# 'Inflections'.demodulize # => "Inflections"
|
146
|
+
#
|
147
|
+
# See also +deconstantize+.
|
148
|
+
def demodulize(path)
|
149
|
+
path = path.to_s
|
150
|
+
if i = path.rindex('::')
|
151
|
+
path[(i+2)..-1]
|
152
|
+
else
|
153
|
+
path
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
157
|
+
# Removes the rightmost segment from the constant expression in the string.
|
158
|
+
#
|
159
|
+
# 'Net::HTTP'.deconstantize # => "Net"
|
160
|
+
# '::Net::HTTP'.deconstantize # => "::Net"
|
161
|
+
# 'String'.deconstantize # => ""
|
162
|
+
# '::String'.deconstantize # => ""
|
163
|
+
# ''.deconstantize # => ""
|
164
|
+
#
|
165
|
+
# See also +demodulize+.
|
166
|
+
def deconstantize(path)
|
167
|
+
path.to_s[0...(path.rindex('::') || 0)] # implementation based on the one in facets' Module#spacename
|
168
|
+
end
|
169
|
+
|
170
|
+
# Creates a foreign key name from a class name.
|
171
|
+
# +separate_class_name_and_id_with_underscore+ sets whether
|
172
|
+
# the method should put '_' between the name and 'id'.
|
173
|
+
#
|
174
|
+
# 'Message'.foreign_key # => "message_id"
|
175
|
+
# 'Message'.foreign_key(false) # => "messageid"
|
176
|
+
# 'Admin::Post'.foreign_key # => "post_id"
|
177
|
+
def foreign_key(class_name, separate_class_name_and_id_with_underscore = true)
|
178
|
+
underscore(demodulize(class_name)) + (separate_class_name_and_id_with_underscore ? "_id" : "id")
|
179
|
+
end
|
180
|
+
|
181
|
+
# Tries to find a constant with the name specified in the argument string.
|
182
|
+
#
|
183
|
+
# 'Module'.constantize # => Module
|
184
|
+
# 'Test::Unit'.constantize # => Test::Unit
|
185
|
+
#
|
186
|
+
# The name is assumed to be the one of a top-level constant, no matter
|
187
|
+
# whether it starts with "::" or not. No lexical context is taken into
|
188
|
+
# account:
|
189
|
+
#
|
190
|
+
# C = 'outside'
|
191
|
+
# module M
|
192
|
+
# C = 'inside'
|
193
|
+
# C # => 'inside'
|
194
|
+
# 'C'.constantize # => 'outside', same as ::C
|
195
|
+
# end
|
196
|
+
#
|
197
|
+
# NameError is raised when the name is not in CamelCase or the constant is
|
198
|
+
# unknown.
|
199
|
+
def constantize(camel_cased_word)
|
200
|
+
names = camel_cased_word.split('::')
|
201
|
+
names.shift if names.empty? || names.first.empty?
|
202
|
+
|
203
|
+
names.inject(Object) do |constant, name|
|
204
|
+
if constant == Object
|
205
|
+
constant.const_get(name)
|
206
|
+
else
|
207
|
+
candidate = constant.const_get(name)
|
208
|
+
next candidate if constant.const_defined?(name, false)
|
209
|
+
next candidate unless Object.const_defined?(name)
|
210
|
+
|
211
|
+
# Go down the ancestors to check it it's owned
|
212
|
+
# directly before we reach Object or the end of ancestors.
|
213
|
+
constant = constant.ancestors.inject do |const, ancestor|
|
214
|
+
break const if ancestor == Object
|
215
|
+
break ancestor if ancestor.const_defined?(name, false)
|
216
|
+
const
|
217
|
+
end
|
218
|
+
|
219
|
+
# owner is in Object, so raise
|
220
|
+
constant.const_get(name, false)
|
221
|
+
end
|
222
|
+
end
|
223
|
+
end
|
224
|
+
|
225
|
+
# Tries to find a constant with the name specified in the argument string.
|
226
|
+
#
|
227
|
+
# 'Module'.safe_constantize # => Module
|
228
|
+
# 'Test::Unit'.safe_constantize # => Test::Unit
|
229
|
+
#
|
230
|
+
# The name is assumed to be the one of a top-level constant, no matter
|
231
|
+
# whether it starts with "::" or not. No lexical context is taken into
|
232
|
+
# account:
|
233
|
+
#
|
234
|
+
# C = 'outside'
|
235
|
+
# module M
|
236
|
+
# C = 'inside'
|
237
|
+
# C # => 'inside'
|
238
|
+
# 'C'.safe_constantize # => 'outside', same as ::C
|
239
|
+
# end
|
240
|
+
#
|
241
|
+
# +nil+ is returned when the name is not in CamelCase or the constant (or
|
242
|
+
# part of it) is unknown.
|
243
|
+
#
|
244
|
+
# 'blargle'.safe_constantize # => nil
|
245
|
+
# 'UnknownModule'.safe_constantize # => nil
|
246
|
+
# 'UnknownModule::Foo::Bar'.safe_constantize # => nil
|
247
|
+
def safe_constantize(camel_cased_word)
|
248
|
+
constantize(camel_cased_word)
|
249
|
+
rescue NameError => e
|
250
|
+
raise unless e.message =~ /(uninitialized constant|wrong constant name) #{const_regexp(camel_cased_word)}$/ ||
|
251
|
+
e.name.to_s == camel_cased_word.to_s
|
252
|
+
rescue ArgumentError => e
|
253
|
+
raise unless e.message =~ /not missing constant #{const_regexp(camel_cased_word)}\!$/
|
254
|
+
end
|
255
|
+
|
256
|
+
# Returns the suffix that should be added to a number to denote the position
|
257
|
+
# in an ordered sequence such as 1st, 2nd, 3rd, 4th.
|
258
|
+
#
|
259
|
+
# ordinal(1) # => "st"
|
260
|
+
# ordinal(2) # => "nd"
|
261
|
+
# ordinal(1002) # => "nd"
|
262
|
+
# ordinal(1003) # => "rd"
|
263
|
+
# ordinal(-11) # => "th"
|
264
|
+
# ordinal(-1021) # => "st"
|
265
|
+
def ordinal(number)
|
266
|
+
abs_number = number.to_i.abs
|
267
|
+
|
268
|
+
if (11..13).include?(abs_number % 100)
|
269
|
+
"th"
|
270
|
+
else
|
271
|
+
case abs_number % 10
|
272
|
+
when 1; "st"
|
273
|
+
when 2; "nd"
|
274
|
+
when 3; "rd"
|
275
|
+
else "th"
|
276
|
+
end
|
277
|
+
end
|
278
|
+
end
|
279
|
+
|
280
|
+
# Turns a number into an ordinal string used to denote the position in an
|
281
|
+
# ordered sequence such as 1st, 2nd, 3rd, 4th.
|
282
|
+
#
|
283
|
+
# ordinalize(1) # => "1st"
|
284
|
+
# ordinalize(2) # => "2nd"
|
285
|
+
# ordinalize(1002) # => "1002nd"
|
286
|
+
# ordinalize(1003) # => "1003rd"
|
287
|
+
# ordinalize(-11) # => "-11th"
|
288
|
+
# ordinalize(-1021) # => "-1021st"
|
289
|
+
def ordinalize(number)
|
290
|
+
"#{number}#{ordinal(number)}"
|
291
|
+
end
|
292
|
+
|
293
|
+
private
|
294
|
+
|
295
|
+
# Mount a regular expression that will match part by part of the constant.
|
296
|
+
# For instance, Foo::Bar::Baz will generate Foo(::Bar(::Baz)?)?
|
297
|
+
def const_regexp(camel_cased_word) #:nodoc:
|
298
|
+
parts = camel_cased_word.split("::")
|
299
|
+
last = parts.pop
|
300
|
+
|
301
|
+
parts.reverse.inject(last) do |acc, part|
|
302
|
+
part.empty? ? acc : "#{part}(::#{acc})?"
|
303
|
+
end
|
304
|
+
end
|
305
|
+
|
306
|
+
# Applies inflection rules for +singularize+ and +pluralize+.
|
307
|
+
#
|
308
|
+
# apply_inflections('post', inflections.plurals) # => "posts"
|
309
|
+
# apply_inflections('posts', inflections.singulars) # => "post"
|
310
|
+
def apply_inflections(word, rules)
|
311
|
+
result = word.to_s.dup
|
312
|
+
|
313
|
+
if word.empty? || inflections.uncountables.include?(result.downcase[/\b\w+\Z/])
|
314
|
+
result
|
315
|
+
else
|
316
|
+
rules.each { |(rule, replacement)| break if result.sub!(rule, replacement) }
|
317
|
+
result
|
318
|
+
end
|
319
|
+
end
|
320
|
+
end
|
321
|
+
end
|
322
|
+
end
|
@@ -0,0 +1,322 @@
|
|
1
|
+
module MotionSupport
|
2
|
+
# The Inflector transforms words from singular to plural, class names to table
|
3
|
+
# names, modularized class names to ones without, and class names to foreign
|
4
|
+
# keys. The default inflections for pluralization, singularization, and
|
5
|
+
# uncountable words are kept in inflections.rb.
|
6
|
+
module Inflector
|
7
|
+
class << self
|
8
|
+
|
9
|
+
# Returns the plural form of the word in the string.
|
10
|
+
#
|
11
|
+
# 'post'.pluralize # => "posts"
|
12
|
+
# 'octopus'.pluralize # => "octopi"
|
13
|
+
# 'sheep'.pluralize # => "sheep"
|
14
|
+
# 'words'.pluralize # => "words"
|
15
|
+
# 'CamelOctopus'.pluralize # => "CamelOctopi"
|
16
|
+
def pluralize(word)
|
17
|
+
apply_inflections(word, inflections.plurals)
|
18
|
+
end
|
19
|
+
|
20
|
+
# The reverse of +pluralize+, returns the singular form of a word in a
|
21
|
+
# string.
|
22
|
+
#
|
23
|
+
# 'posts'.singularize # => "post"
|
24
|
+
# 'octopi'.singularize # => "octopus"
|
25
|
+
# 'sheep'.singularize # => "sheep"
|
26
|
+
# 'word'.singularize # => "word"
|
27
|
+
# 'CamelOctopi'.singularize # => "CamelOctopus"
|
28
|
+
def singularize(word)
|
29
|
+
apply_inflections(word, inflections.singulars)
|
30
|
+
end
|
31
|
+
|
32
|
+
# By default, +camelize+ converts strings to UpperCamelCase. If the argument
|
33
|
+
# to +camelize+ is set to <tt>:lower</tt> then +camelize+ produces
|
34
|
+
# lowerCamelCase.
|
35
|
+
#
|
36
|
+
# +camelize+ will also convert '/' to '::' which is useful for converting
|
37
|
+
# paths to namespaces.
|
38
|
+
#
|
39
|
+
# 'active_model'.camelize # => "ActiveModel"
|
40
|
+
# 'active_model'.camelize(:lower) # => "activeModel"
|
41
|
+
# 'active_model/errors'.camelize # => "ActiveModel::Errors"
|
42
|
+
# 'active_model/errors'.camelize(:lower) # => "activeModel::Errors"
|
43
|
+
#
|
44
|
+
# As a rule of thumb you can think of +camelize+ as the inverse of
|
45
|
+
# +underscore+, though there are cases where that does not hold:
|
46
|
+
#
|
47
|
+
# 'SSLError'.underscore.camelize # => "SslError"
|
48
|
+
def camelize(term, uppercase_first_letter = true)
|
49
|
+
string = term.to_s
|
50
|
+
if uppercase_first_letter
|
51
|
+
string = string.sub(/^[a-z\d]*/) { |match| inflections.acronyms[match] || match.capitalize }
|
52
|
+
else
|
53
|
+
string = string.sub(/^(?:#{inflections.acronym_regex}(?=\b|[A-Z_])|\w)/) { |match| match.downcase }
|
54
|
+
end
|
55
|
+
string.gsub(/(?:_|(\/))([a-z\d]*)/i) { "#{$1}#{inflections.acronyms[$2] || $2.capitalize}" }.gsub('/', '::')
|
56
|
+
end
|
57
|
+
|
58
|
+
# Makes an underscored, lowercase form from the expression in the string.
|
59
|
+
#
|
60
|
+
# Changes '::' to '/' to convert namespaces to paths.
|
61
|
+
#
|
62
|
+
# 'ActiveModel'.underscore # => "active_model"
|
63
|
+
# 'ActiveModel::Errors'.underscore # => "active_model/errors"
|
64
|
+
#
|
65
|
+
# As a rule of thumb you can think of +underscore+ as the inverse of
|
66
|
+
# +camelize+, though there are cases where that does not hold:
|
67
|
+
#
|
68
|
+
# 'SSLError'.underscore.camelize # => "SslError"
|
69
|
+
def underscore(camel_cased_word)
|
70
|
+
word = camel_cased_word.to_s.dup
|
71
|
+
word.gsub!('::', '/')
|
72
|
+
word.gsub!(/(?:([A-Za-z\d])|^)(#{inflections.acronym_regex})(?=\b|[^a-z])/) { "#{$1}#{$1 && '_'}#{$2.downcase}" }
|
73
|
+
word.gsub!(/([A-Z\d]+)([A-Z][a-z])/,'\1_\2')
|
74
|
+
word.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
|
75
|
+
word.gsub!("-", "_")
|
76
|
+
word.downcase!
|
77
|
+
word
|
78
|
+
end
|
79
|
+
|
80
|
+
# Capitalizes the first word and turns underscores into spaces and strips a
|
81
|
+
# trailing "_id", if any. Like +titleize+, this is meant for creating pretty
|
82
|
+
# output.
|
83
|
+
#
|
84
|
+
# 'employee_salary'.humanize # => "Employee salary"
|
85
|
+
# 'author_id'.humanize # => "Author"
|
86
|
+
def humanize(lower_case_and_underscored_word)
|
87
|
+
result = lower_case_and_underscored_word.to_s.dup
|
88
|
+
inflections.humans.each { |(rule, replacement)| break if result.sub!(rule, replacement) }
|
89
|
+
result.gsub!(/_id$/, "")
|
90
|
+
result.gsub!('_', ' ')
|
91
|
+
result.gsub(/([a-z\d]*)/i) { |match|
|
92
|
+
"#{inflections.acronyms[match] || match.downcase}"
|
93
|
+
}.gsub(/^\w/) { |match| match.upcase }
|
94
|
+
end
|
95
|
+
|
96
|
+
# Capitalizes all the words and replaces some characters in the string to
|
97
|
+
# create a nicer looking title. +titleize+ is meant for creating pretty
|
98
|
+
# output. It is not used in the Rails internals.
|
99
|
+
#
|
100
|
+
# +titleize+ is also aliased as +titlecase+.
|
101
|
+
#
|
102
|
+
# 'man from the boondocks'.titleize # => "Man From The Boondocks"
|
103
|
+
# 'x-men: the last stand'.titleize # => "X Men: The Last Stand"
|
104
|
+
# 'TheManWithoutAPast'.titleize # => "The Man Without A Past"
|
105
|
+
# 'raiders_of_the_lost_ark'.titleize # => "Raiders Of The Lost Ark"
|
106
|
+
def titleize(word)
|
107
|
+
humanize(underscore(word)).gsub(/\b(?<!['’`])[a-z]/) { |match| match.capitalize }
|
108
|
+
end
|
109
|
+
|
110
|
+
# Create the name of a table like Rails does for models to table names. This
|
111
|
+
# method uses the +pluralize+ method on the last word in the string.
|
112
|
+
#
|
113
|
+
# 'RawScaledScorer'.tableize # => "raw_scaled_scorers"
|
114
|
+
# 'egg_and_ham'.tableize # => "egg_and_hams"
|
115
|
+
# 'fancyCategory'.tableize # => "fancy_categories"
|
116
|
+
def tableize(class_name)
|
117
|
+
pluralize(underscore(class_name))
|
118
|
+
end
|
119
|
+
|
120
|
+
# Create a class name from a plural table name like Rails does for table
|
121
|
+
# names to models. Note that this returns a string and not a Class (To
|
122
|
+
# convert to an actual class follow +classify+ with +constantize+).
|
123
|
+
#
|
124
|
+
# 'egg_and_hams'.classify # => "EggAndHam"
|
125
|
+
# 'posts'.classify # => "Post"
|
126
|
+
#
|
127
|
+
# Singular names are not handled correctly:
|
128
|
+
#
|
129
|
+
# 'business'.classify # => "Busines"
|
130
|
+
def classify(table_name)
|
131
|
+
# strip out any leading schema name
|
132
|
+
camelize(singularize(table_name.to_s.sub(/.*\./, '')))
|
133
|
+
end
|
134
|
+
|
135
|
+
# Replaces underscores with dashes in the string.
|
136
|
+
#
|
137
|
+
# 'puni_puni'.dasherize # => "puni-puni"
|
138
|
+
def dasherize(underscored_word)
|
139
|
+
underscored_word.gsub('_', '-')
|
140
|
+
end
|
141
|
+
|
142
|
+
# Removes the module part from the expression in the string.
|
143
|
+
#
|
144
|
+
# 'ActiveRecord::CoreExtensions::String::Inflections'.demodulize # => "Inflections"
|
145
|
+
# 'Inflections'.demodulize # => "Inflections"
|
146
|
+
#
|
147
|
+
# See also +deconstantize+.
|
148
|
+
def demodulize(path)
|
149
|
+
path = path.to_s
|
150
|
+
if i = path.rindex('::')
|
151
|
+
path[(i+2)..-1]
|
152
|
+
else
|
153
|
+
path
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
157
|
+
# Removes the rightmost segment from the constant expression in the string.
|
158
|
+
#
|
159
|
+
# 'Net::HTTP'.deconstantize # => "Net"
|
160
|
+
# '::Net::HTTP'.deconstantize # => "::Net"
|
161
|
+
# 'String'.deconstantize # => ""
|
162
|
+
# '::String'.deconstantize # => ""
|
163
|
+
# ''.deconstantize # => ""
|
164
|
+
#
|
165
|
+
# See also +demodulize+.
|
166
|
+
def deconstantize(path)
|
167
|
+
path.to_s[0...(path.rindex('::') || 0)] # implementation based on the one in facets' Module#spacename
|
168
|
+
end
|
169
|
+
|
170
|
+
# Creates a foreign key name from a class name.
|
171
|
+
# +separate_class_name_and_id_with_underscore+ sets whether
|
172
|
+
# the method should put '_' between the name and 'id'.
|
173
|
+
#
|
174
|
+
# 'Message'.foreign_key # => "message_id"
|
175
|
+
# 'Message'.foreign_key(false) # => "messageid"
|
176
|
+
# 'Admin::Post'.foreign_key # => "post_id"
|
177
|
+
def foreign_key(class_name, separate_class_name_and_id_with_underscore = true)
|
178
|
+
underscore(demodulize(class_name)) + (separate_class_name_and_id_with_underscore ? "_id" : "id")
|
179
|
+
end
|
180
|
+
|
181
|
+
# Tries to find a constant with the name specified in the argument string.
|
182
|
+
#
|
183
|
+
# 'Module'.constantize # => Module
|
184
|
+
# 'Test::Unit'.constantize # => Test::Unit
|
185
|
+
#
|
186
|
+
# The name is assumed to be the one of a top-level constant, no matter
|
187
|
+
# whether it starts with "::" or not. No lexical context is taken into
|
188
|
+
# account:
|
189
|
+
#
|
190
|
+
# C = 'outside'
|
191
|
+
# module M
|
192
|
+
# C = 'inside'
|
193
|
+
# C # => 'inside'
|
194
|
+
# 'C'.constantize # => 'outside', same as ::C
|
195
|
+
# end
|
196
|
+
#
|
197
|
+
# NameError is raised when the name is not in CamelCase or the constant is
|
198
|
+
# unknown.
|
199
|
+
def constantize(camel_cased_word)
|
200
|
+
names = camel_cased_word.split('::')
|
201
|
+
names.shift if names.empty? || names.first.empty?
|
202
|
+
|
203
|
+
names.inject(Object) do |constant, name|
|
204
|
+
if constant == Object
|
205
|
+
constant.const_get(name)
|
206
|
+
else
|
207
|
+
candidate = constant.const_get(name)
|
208
|
+
next candidate if constant.const_defined?(name, false)
|
209
|
+
next candidate unless Object.const_defined?(name)
|
210
|
+
|
211
|
+
# Go down the ancestors to check it it's owned
|
212
|
+
# directly before we reach Object or the end of ancestors.
|
213
|
+
constant = constant.ancestors.inject do |const, ancestor|
|
214
|
+
break const if ancestor == Object
|
215
|
+
break ancestor if ancestor.const_defined?(name, false)
|
216
|
+
const
|
217
|
+
end
|
218
|
+
|
219
|
+
# owner is in Object, so raise
|
220
|
+
constant.const_get(name, false)
|
221
|
+
end
|
222
|
+
end
|
223
|
+
end
|
224
|
+
|
225
|
+
# Tries to find a constant with the name specified in the argument string.
|
226
|
+
#
|
227
|
+
# 'Module'.safe_constantize # => Module
|
228
|
+
# 'Test::Unit'.safe_constantize # => Test::Unit
|
229
|
+
#
|
230
|
+
# The name is assumed to be the one of a top-level constant, no matter
|
231
|
+
# whether it starts with "::" or not. No lexical context is taken into
|
232
|
+
# account:
|
233
|
+
#
|
234
|
+
# C = 'outside'
|
235
|
+
# module M
|
236
|
+
# C = 'inside'
|
237
|
+
# C # => 'inside'
|
238
|
+
# 'C'.safe_constantize # => 'outside', same as ::C
|
239
|
+
# end
|
240
|
+
#
|
241
|
+
# +nil+ is returned when the name is not in CamelCase or the constant (or
|
242
|
+
# part of it) is unknown.
|
243
|
+
#
|
244
|
+
# 'blargle'.safe_constantize # => nil
|
245
|
+
# 'UnknownModule'.safe_constantize # => nil
|
246
|
+
# 'UnknownModule::Foo::Bar'.safe_constantize # => nil
|
247
|
+
def safe_constantize(camel_cased_word)
|
248
|
+
constantize(camel_cased_word)
|
249
|
+
rescue NameError => e
|
250
|
+
raise unless e.message =~ /(uninitialized constant|wrong constant name) #{const_regexp(camel_cased_word)}$/ ||
|
251
|
+
e.name.to_s == camel_cased_word.to_s
|
252
|
+
rescue ArgumentError => e
|
253
|
+
raise unless e.message =~ /not missing constant #{const_regexp(camel_cased_word)}\!$/
|
254
|
+
end
|
255
|
+
|
256
|
+
# Returns the suffix that should be added to a number to denote the position
|
257
|
+
# in an ordered sequence such as 1st, 2nd, 3rd, 4th.
|
258
|
+
#
|
259
|
+
# ordinal(1) # => "st"
|
260
|
+
# ordinal(2) # => "nd"
|
261
|
+
# ordinal(1002) # => "nd"
|
262
|
+
# ordinal(1003) # => "rd"
|
263
|
+
# ordinal(-11) # => "th"
|
264
|
+
# ordinal(-1021) # => "st"
|
265
|
+
def ordinal(number)
|
266
|
+
abs_number = number.to_i.abs
|
267
|
+
|
268
|
+
if (11..13).include?(abs_number % 100)
|
269
|
+
"th"
|
270
|
+
else
|
271
|
+
case abs_number % 10
|
272
|
+
when 1; "st"
|
273
|
+
when 2; "nd"
|
274
|
+
when 3; "rd"
|
275
|
+
else "th"
|
276
|
+
end
|
277
|
+
end
|
278
|
+
end
|
279
|
+
|
280
|
+
# Turns a number into an ordinal string used to denote the position in an
|
281
|
+
# ordered sequence such as 1st, 2nd, 3rd, 4th.
|
282
|
+
#
|
283
|
+
# ordinalize(1) # => "1st"
|
284
|
+
# ordinalize(2) # => "2nd"
|
285
|
+
# ordinalize(1002) # => "1002nd"
|
286
|
+
# ordinalize(1003) # => "1003rd"
|
287
|
+
# ordinalize(-11) # => "-11th"
|
288
|
+
# ordinalize(-1021) # => "-1021st"
|
289
|
+
def ordinalize(number)
|
290
|
+
"#{number}#{ordinal(number)}"
|
291
|
+
end
|
292
|
+
|
293
|
+
private
|
294
|
+
|
295
|
+
# Mount a regular expression that will match part by part of the constant.
|
296
|
+
# For instance, Foo::Bar::Baz will generate Foo(::Bar(::Baz)?)?
|
297
|
+
def const_regexp(camel_cased_word) #:nodoc:
|
298
|
+
parts = camel_cased_word.split("::")
|
299
|
+
last = parts.pop
|
300
|
+
|
301
|
+
parts.reverse.inject(last) do |acc, part|
|
302
|
+
part.empty? ? acc : "#{part}(::#{acc})?"
|
303
|
+
end
|
304
|
+
end
|
305
|
+
|
306
|
+
# Applies inflection rules for +singularize+ and +pluralize+.
|
307
|
+
#
|
308
|
+
# apply_inflections('post', inflections.plurals) # => "posts"
|
309
|
+
# apply_inflections('posts', inflections.singulars) # => "post"
|
310
|
+
def apply_inflections(word, rules)
|
311
|
+
result = word.to_s.dup
|
312
|
+
|
313
|
+
if word.empty? || inflections.uncountables.include?(result.downcase[/\b\w+\Z/])
|
314
|
+
result
|
315
|
+
else
|
316
|
+
rules.each { |(rule, replacement)| break if result.sub!(rule, replacement) }
|
317
|
+
result
|
318
|
+
end
|
319
|
+
end
|
320
|
+
end
|
321
|
+
end
|
322
|
+
end
|
@@ -0,0 +1,178 @@
|
|
1
|
+
# String inflections define new methods on the String class to transform names for different purposes.
|
2
|
+
# For instance, you can figure out the name of a table from the name of a class.
|
3
|
+
#
|
4
|
+
# 'ScaleScore'.tableize # => "scale_scores"
|
5
|
+
#
|
6
|
+
class String
|
7
|
+
# Returns the plural form of the word in the string.
|
8
|
+
#
|
9
|
+
# If the optional parameter +count+ is specified,
|
10
|
+
# the singular form will be returned if <tt>count == 1</tt>.
|
11
|
+
# For any other value of +count+ the plural will be returned.
|
12
|
+
#
|
13
|
+
# 'post'.pluralize # => "posts"
|
14
|
+
# 'octopus'.pluralize # => "octopi"
|
15
|
+
# 'sheep'.pluralize # => "sheep"
|
16
|
+
# 'words'.pluralize # => "words"
|
17
|
+
# 'the blue mailman'.pluralize # => "the blue mailmen"
|
18
|
+
# 'CamelOctopus'.pluralize # => "CamelOctopi"
|
19
|
+
# 'apple'.pluralize(1) # => "apple"
|
20
|
+
# 'apple'.pluralize(2) # => "apples"
|
21
|
+
def pluralize(count = nil)
|
22
|
+
if count == 1
|
23
|
+
self
|
24
|
+
else
|
25
|
+
MotionSupport::Inflector.pluralize(self)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
# The reverse of +pluralize+, returns the singular form of a word in a string.
|
30
|
+
#
|
31
|
+
# 'posts'.singularize # => "post"
|
32
|
+
# 'octopi'.singularize # => "octopus"
|
33
|
+
# 'sheep'.singularize # => "sheep"
|
34
|
+
# 'word'.singularize # => "word"
|
35
|
+
# 'the blue mailmen'.singularize # => "the blue mailman"
|
36
|
+
# 'CamelOctopi'.singularize # => "CamelOctopus"
|
37
|
+
def singularize
|
38
|
+
MotionSupport::Inflector.singularize(self)
|
39
|
+
end
|
40
|
+
|
41
|
+
# +constantize+ tries to find a declared constant with the name specified
|
42
|
+
# in the string. It raises a NameError when the name is not in CamelCase
|
43
|
+
# or is not initialized. See MotionSupport::Inflector.constantize
|
44
|
+
#
|
45
|
+
# 'Module'.constantize # => Module
|
46
|
+
# 'Class'.constantize # => Class
|
47
|
+
# 'blargle'.constantize # => NameError: wrong constant name blargle
|
48
|
+
def constantize
|
49
|
+
MotionSupport::Inflector.constantize(self)
|
50
|
+
end
|
51
|
+
|
52
|
+
# +safe_constantize+ tries to find a declared constant with the name specified
|
53
|
+
# in the string. It returns nil when the name is not in CamelCase
|
54
|
+
# or is not initialized. See MotionSupport::Inflector.safe_constantize
|
55
|
+
#
|
56
|
+
# 'Module'.safe_constantize # => Module
|
57
|
+
# 'Class'.safe_constantize # => Class
|
58
|
+
# 'blargle'.safe_constantize # => nil
|
59
|
+
def safe_constantize
|
60
|
+
MotionSupport::Inflector.safe_constantize(self)
|
61
|
+
end
|
62
|
+
|
63
|
+
# By default, +camelize+ converts strings to UpperCamelCase. If the argument to camelize
|
64
|
+
# is set to <tt>:lower</tt> then camelize produces lowerCamelCase.
|
65
|
+
#
|
66
|
+
# +camelize+ will also convert '/' to '::' which is useful for converting paths to namespaces.
|
67
|
+
#
|
68
|
+
# 'active_record'.camelize # => "ActiveRecord"
|
69
|
+
# 'active_record'.camelize(:lower) # => "activeRecord"
|
70
|
+
# 'active_record/errors'.camelize # => "ActiveRecord::Errors"
|
71
|
+
# 'active_record/errors'.camelize(:lower) # => "activeRecord::Errors"
|
72
|
+
def camelize(first_letter = :upper)
|
73
|
+
case first_letter
|
74
|
+
when :upper
|
75
|
+
MotionSupport::Inflector.camelize(self, true)
|
76
|
+
when :lower
|
77
|
+
MotionSupport::Inflector.camelize(self, false)
|
78
|
+
end
|
79
|
+
end
|
80
|
+
alias_method :camelcase, :camelize
|
81
|
+
|
82
|
+
# Capitalizes all the words and replaces some characters in the string to create
|
83
|
+
# a nicer looking title. +titleize+ is meant for creating pretty output. It is not
|
84
|
+
# used in the Rails internals.
|
85
|
+
#
|
86
|
+
# +titleize+ is also aliased as +titlecase+.
|
87
|
+
#
|
88
|
+
# 'man from the boondocks'.titleize # => "Man From The Boondocks"
|
89
|
+
# 'x-men: the last stand'.titleize # => "X Men: The Last Stand"
|
90
|
+
def titleize
|
91
|
+
MotionSupport::Inflector.titleize(self)
|
92
|
+
end
|
93
|
+
alias_method :titlecase, :titleize
|
94
|
+
|
95
|
+
# The reverse of +camelize+. Makes an underscored, lowercase form from the expression in the string.
|
96
|
+
#
|
97
|
+
# +underscore+ will also change '::' to '/' to convert namespaces to paths.
|
98
|
+
#
|
99
|
+
# 'ActiveModel'.underscore # => "active_model"
|
100
|
+
# 'ActiveModel::Errors'.underscore # => "active_model/errors"
|
101
|
+
def underscore
|
102
|
+
MotionSupport::Inflector.underscore(self)
|
103
|
+
end
|
104
|
+
|
105
|
+
# Replaces underscores with dashes in the string.
|
106
|
+
#
|
107
|
+
# 'puni_puni'.dasherize # => "puni-puni"
|
108
|
+
def dasherize
|
109
|
+
MotionSupport::Inflector.dasherize(self)
|
110
|
+
end
|
111
|
+
|
112
|
+
# Removes the module part from the constant expression in the string.
|
113
|
+
#
|
114
|
+
# 'ActiveRecord::CoreExtensions::String::Inflections'.demodulize # => "Inflections"
|
115
|
+
# 'Inflections'.demodulize # => "Inflections"
|
116
|
+
#
|
117
|
+
# See also +deconstantize+.
|
118
|
+
def demodulize
|
119
|
+
MotionSupport::Inflector.demodulize(self)
|
120
|
+
end
|
121
|
+
|
122
|
+
# Removes the rightmost segment from the constant expression in the string.
|
123
|
+
#
|
124
|
+
# 'Net::HTTP'.deconstantize # => "Net"
|
125
|
+
# '::Net::HTTP'.deconstantize # => "::Net"
|
126
|
+
# 'String'.deconstantize # => ""
|
127
|
+
# '::String'.deconstantize # => ""
|
128
|
+
# ''.deconstantize # => ""
|
129
|
+
#
|
130
|
+
# See also +demodulize+.
|
131
|
+
def deconstantize
|
132
|
+
MotionSupport::Inflector.deconstantize(self)
|
133
|
+
end
|
134
|
+
|
135
|
+
# Creates the name of a table like Rails does for models to table names. This method
|
136
|
+
# uses the +pluralize+ method on the last word in the string.
|
137
|
+
#
|
138
|
+
# 'RawScaledScorer'.tableize # => "raw_scaled_scorers"
|
139
|
+
# 'egg_and_ham'.tableize # => "egg_and_hams"
|
140
|
+
# 'fancyCategory'.tableize # => "fancy_categories"
|
141
|
+
def tableize
|
142
|
+
MotionSupport::Inflector.tableize(self)
|
143
|
+
end
|
144
|
+
|
145
|
+
# Create a class name from a plural table name like Rails does for table names to models.
|
146
|
+
# Note that this returns a string and not a class. (To convert to an actual class
|
147
|
+
# follow +classify+ with +constantize+.)
|
148
|
+
#
|
149
|
+
# 'egg_and_hams'.classify # => "EggAndHam"
|
150
|
+
# 'posts'.classify # => "Post"
|
151
|
+
#
|
152
|
+
# Singular names are not handled correctly.
|
153
|
+
#
|
154
|
+
# 'business'.classify # => "Busines"
|
155
|
+
def classify
|
156
|
+
MotionSupport::Inflector.classify(self)
|
157
|
+
end
|
158
|
+
|
159
|
+
# Capitalizes the first word, turns underscores into spaces, and strips '_id'.
|
160
|
+
# Like +titleize+, this is meant for creating pretty output.
|
161
|
+
#
|
162
|
+
# 'employee_salary'.humanize # => "Employee salary"
|
163
|
+
# 'author_id'.humanize # => "Author"
|
164
|
+
def humanize
|
165
|
+
MotionSupport::Inflector.humanize(self)
|
166
|
+
end
|
167
|
+
|
168
|
+
# Creates a foreign key name from a class name.
|
169
|
+
# +separate_class_name_and_id_with_underscore+ sets whether
|
170
|
+
# the method should put '_' between the name and 'id'.
|
171
|
+
#
|
172
|
+
# 'Message'.foreign_key # => "message_id"
|
173
|
+
# 'Message'.foreign_key(false) # => "messageid"
|
174
|
+
# 'Admin::Post'.foreign_key # => "post_id"
|
175
|
+
def foreign_key(separate_class_name_and_id_with_underscore = true)
|
176
|
+
MotionSupport::Inflector.foreign_key(self, separate_class_name_and_id_with_underscore)
|
177
|
+
end
|
178
|
+
end
|
data/lib/swiss_db/swiss_model.rb
CHANGED
@@ -16,6 +16,10 @@ class SwissModel
|
|
16
16
|
@store
|
17
17
|
end
|
18
18
|
|
19
|
+
def self.set_class_name(class_name) # hack, class.name not functioning in RM Android...
|
20
|
+
set_table_name(class_name.tableize)
|
21
|
+
end
|
22
|
+
|
19
23
|
def self.set_table_name(table_name)
|
20
24
|
@table_name = table_name
|
21
25
|
end
|
@@ -29,7 +33,7 @@ class SwissModel
|
|
29
33
|
end
|
30
34
|
|
31
35
|
def self.primary_key
|
32
|
-
@primary_key
|
36
|
+
@primary_key.nil? ? "id" : @primary_key
|
33
37
|
end
|
34
38
|
|
35
39
|
def self.all
|
@@ -60,7 +64,7 @@ class SwissModel
|
|
60
64
|
# create a row
|
61
65
|
result = store.insert(@table_name, obj)
|
62
66
|
if result == -1
|
63
|
-
puts "An error occured inserting values into #{
|
67
|
+
puts "An error occured inserting values into #{@table_name}"
|
64
68
|
else
|
65
69
|
return result
|
66
70
|
end
|
@@ -75,6 +79,18 @@ class SwissModel
|
|
75
79
|
store.destroy_all(@table_name)
|
76
80
|
end
|
77
81
|
|
78
|
-
#
|
82
|
+
# borrowed from rails
|
83
|
+
private
|
84
|
+
|
85
|
+
def underscore(camel_cased_word)
|
86
|
+
return camel_cased_word unless camel_cased_word =~ /[A-Z-]|::/
|
87
|
+
word = camel_cased_word.to_s.gsub(/::/, '/')
|
88
|
+
word.gsub!(/(?:(?<=([A-Za-z\d]))|\b)(#{inflections.acronym_regex})(?=\b|[^a-z])/) { "#{$1 && '_'}#{$2.downcase}" }
|
89
|
+
word.gsub!(/([A-Z\d]+)([A-Z][a-z])/,'\1_\2')
|
90
|
+
word.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
|
91
|
+
word.gsub!("-", "_")
|
92
|
+
word.downcase!
|
93
|
+
word
|
94
|
+
end
|
79
95
|
|
80
96
|
end
|
data/lib/swiss_db.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
# -*- coding: utf-8 -*-
|
2
2
|
# SwissDB by jsilverMDX
|
3
|
+
|
3
4
|
if defined?(Motion) && defined?(Motion::Project::Config)
|
4
|
-
# puts "MOTION PROJECT CONFIG IS DEFINED"
|
5
5
|
lib_dir_path = File.dirname(File.expand_path(__FILE__))
|
6
6
|
Motion::Project::App.setup do |app|
|
7
7
|
# unless platform_name == "android"
|
@@ -13,10 +13,14 @@ if defined?(Motion) && defined?(Motion::Project::Config)
|
|
13
13
|
# the end of the list
|
14
14
|
insert_point = app.files.find_index { |file| file =~ /^(?:\.\/)?app\// } || 0
|
15
15
|
|
16
|
+
# change to "swiss_db" for just swiss_db
|
16
17
|
Dir.glob(File.join(lib_dir_path, "**/*.rb")).reverse.each do |file|
|
17
18
|
app.files.insert(insert_point, file)
|
18
19
|
end
|
19
20
|
|
21
|
+
# load their schemas folder
|
22
|
+
app.files += Dir.glob("schemas/*.rb")
|
23
|
+
|
20
24
|
# puts "APP FILES: #{app.files.inspect}"
|
21
25
|
|
22
26
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: swiss_db
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jonathan Silverman
|
@@ -46,6 +46,11 @@ extensions: []
|
|
46
46
|
extra_rdoc_files: []
|
47
47
|
files:
|
48
48
|
- README.md
|
49
|
+
- lib/motion-support/array.rb
|
50
|
+
- lib/motion-support/inflections.rb
|
51
|
+
- lib/motion-support/inflector.rb
|
52
|
+
- lib/motion-support/methods.rb
|
53
|
+
- lib/motion-support/string.rb
|
49
54
|
- lib/swiss_db.rb
|
50
55
|
- lib/swiss_db/cursor.rb
|
51
56
|
- lib/swiss_db/data_store.java
|