datamapper 0.2.5 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +5 -1
- data/FAQ +96 -0
- data/QUICKLINKS +12 -0
- data/README +57 -155
- data/environment.rb +61 -43
- data/example.rb +30 -12
- data/lib/data_mapper.rb +6 -1
- data/lib/data_mapper/adapters/abstract_adapter.rb +0 -57
- data/lib/data_mapper/adapters/data_object_adapter.rb +203 -97
- data/lib/data_mapper/adapters/mysql_adapter.rb +4 -0
- data/lib/data_mapper/adapters/postgresql_adapter.rb +7 -1
- data/lib/data_mapper/adapters/sql/coersion.rb +3 -2
- data/lib/data_mapper/adapters/sql/commands/load_command.rb +29 -10
- data/lib/data_mapper/adapters/sql/mappings/associations_set.rb +4 -0
- data/lib/data_mapper/adapters/sql/mappings/column.rb +13 -9
- data/lib/data_mapper/adapters/sql/mappings/conditions.rb +172 -0
- data/lib/data_mapper/adapters/sql/mappings/table.rb +43 -17
- data/lib/data_mapper/adapters/sqlite3_adapter.rb +9 -2
- data/lib/data_mapper/associations.rb +75 -3
- data/lib/data_mapper/associations/belongs_to_association.rb +70 -36
- data/lib/data_mapper/associations/has_and_belongs_to_many_association.rb +195 -86
- data/lib/data_mapper/associations/has_many_association.rb +168 -61
- data/lib/data_mapper/associations/has_n_association.rb +23 -3
- data/lib/data_mapper/attributes.rb +73 -0
- data/lib/data_mapper/auto_migrations.rb +2 -6
- data/lib/data_mapper/base.rb +5 -9
- data/lib/data_mapper/database.rb +4 -3
- data/lib/data_mapper/embedded_value.rb +66 -30
- data/lib/data_mapper/identity_map.rb +1 -3
- data/lib/data_mapper/is/tree.rb +121 -0
- data/lib/data_mapper/migration.rb +155 -0
- data/lib/data_mapper/persistence.rb +532 -218
- data/lib/data_mapper/property.rb +306 -0
- data/lib/data_mapper/query.rb +164 -0
- data/lib/data_mapper/support/blank.rb +2 -2
- data/lib/data_mapper/support/connection_pool.rb +5 -6
- data/lib/data_mapper/support/enumerable.rb +3 -3
- data/lib/data_mapper/support/errors.rb +10 -1
- data/lib/data_mapper/support/inflector.rb +174 -238
- data/lib/data_mapper/support/object.rb +54 -0
- data/lib/data_mapper/support/serialization.rb +19 -1
- data/lib/data_mapper/support/string.rb +7 -16
- data/lib/data_mapper/support/symbol.rb +3 -15
- data/lib/data_mapper/support/typed_set.rb +68 -0
- data/lib/data_mapper/types/base.rb +44 -0
- data/lib/data_mapper/types/string.rb +34 -0
- data/lib/data_mapper/validations/number_validator.rb +40 -0
- data/lib/data_mapper/validations/string_validator.rb +20 -0
- data/lib/data_mapper/validations/validator.rb +13 -0
- data/performance.rb +26 -1
- data/profile_data_mapper.rb +1 -1
- data/rakefile.rb +42 -2
- data/spec/acts_as_tree_spec.rb +11 -3
- data/spec/adapters/data_object_adapter_spec.rb +31 -0
- data/spec/associations/belongs_to_association_spec.rb +98 -0
- data/spec/associations/has_and_belongs_to_many_association_spec.rb +377 -0
- data/spec/associations/has_many_association_spec.rb +337 -0
- data/spec/attributes_spec.rb +23 -1
- data/spec/auto_migrations_spec.rb +86 -29
- data/spec/callbacks_spec.rb +107 -0
- data/spec/column_spec.rb +5 -2
- data/spec/count_command_spec.rb +33 -1
- data/spec/database_spec.rb +18 -0
- data/spec/dependency_spec.rb +4 -2
- data/spec/embedded_value_spec.rb +8 -8
- data/spec/fixtures/people.yaml +1 -1
- data/spec/fixtures/projects.yaml +10 -1
- data/spec/fixtures/tasks.yaml +6 -0
- data/spec/fixtures/tasks_tasks.yaml +2 -0
- data/spec/fixtures/tomatoes.yaml +1 -0
- data/spec/is_a_tree_spec.rb +149 -0
- data/spec/load_command_spec.rb +71 -9
- data/spec/magic_columns_spec.rb +17 -2
- data/spec/migration_spec.rb +267 -0
- data/spec/models/animal.rb +1 -1
- data/spec/models/candidate.rb +8 -0
- data/spec/models/career.rb +1 -1
- data/spec/models/chain.rb +8 -0
- data/spec/models/comment.rb +1 -1
- data/spec/models/exhibit.rb +1 -1
- data/spec/models/fence.rb +7 -0
- data/spec/models/fruit.rb +2 -2
- data/spec/models/job.rb +8 -0
- data/spec/models/person.rb +2 -3
- data/spec/models/post.rb +1 -1
- data/spec/models/project.rb +21 -1
- data/spec/models/section.rb +1 -1
- data/spec/models/serializer.rb +1 -1
- data/spec/models/task.rb +9 -0
- data/spec/models/tomato.rb +27 -0
- data/spec/models/user.rb +8 -2
- data/spec/models/zoo.rb +2 -7
- data/spec/paranoia_spec.rb +1 -1
- data/spec/{base_spec.rb → persistence_spec.rb} +207 -18
- data/spec/postgres_spec.rb +48 -6
- data/spec/property_spec.rb +90 -9
- data/spec/query_spec.rb +71 -5
- data/spec/save_command_spec.rb +11 -0
- data/spec/spec_helper.rb +14 -11
- data/spec/support/blank_spec.rb +8 -0
- data/spec/support/inflector_spec.rb +41 -0
- data/spec/support/object_spec.rb +9 -0
- data/spec/{serialization_spec.rb → support/serialization_spec.rb} +1 -1
- data/spec/support/silence_spec.rb +15 -0
- data/spec/{support_spec.rb → support/string_spec.rb} +3 -3
- data/spec/support/struct_spec.rb +12 -0
- data/spec/support/typed_set_spec.rb +66 -0
- data/spec/table_spec.rb +3 -3
- data/spec/types/string.rb +81 -0
- data/spec/validates_uniqueness_of_spec.rb +17 -0
- data/spec/validations/number_validator.rb +59 -0
- data/spec/validations/string_validator.rb +14 -0
- metadata +59 -17
- data/do_performance.rb +0 -153
- data/lib/data_mapper/support/active_record_impersonation.rb +0 -103
- data/lib/data_mapper/support/weak_hash.rb +0 -46
- data/spec/active_record_impersonation_spec.rb +0 -129
- data/spec/associations_spec.rb +0 -232
- data/spec/conditions_spec.rb +0 -49
- data/spec/has_many_association_spec.rb +0 -173
- data/spec/models/animals_exhibit.rb +0 -8
@@ -15,11 +15,15 @@ module DataMapper
|
|
15
15
|
|
16
16
|
# The maximum number of connections.
|
17
17
|
attr_reader :max_size
|
18
|
+
|
19
|
+
# Returns the number of created connections.
|
20
|
+
attr_reader :created_count
|
21
|
+
alias :size :created_count
|
18
22
|
|
19
23
|
# The proc used to create a new connection.
|
20
24
|
attr_accessor :connection_proc
|
21
25
|
|
22
|
-
attr_reader :available_connections, :allocated
|
26
|
+
attr_reader :available_connections, :allocated
|
23
27
|
|
24
28
|
# Constructs a new pool with a maximum size. If a block is supplied, it
|
25
29
|
# is used to create new connections as they are needed.
|
@@ -41,11 +45,6 @@ module DataMapper
|
|
41
45
|
@created_count = 0
|
42
46
|
end
|
43
47
|
|
44
|
-
# Returns the number of created connections.
|
45
|
-
def size
|
46
|
-
@created_count
|
47
|
-
end
|
48
|
-
|
49
48
|
# Assigns a connection to the current thread, yielding the connection
|
50
49
|
# to the supplied block.
|
51
50
|
#
|
@@ -8,7 +8,7 @@ module DataMapper
|
|
8
8
|
module Support
|
9
9
|
|
10
10
|
# Extends Array to include an instance method for grouping objects
|
11
|
-
module
|
11
|
+
module EnumerableExtensions
|
12
12
|
|
13
13
|
# Group a collection of elements into groups within a
|
14
14
|
# Hash. The value returned by the block passed to group_by
|
@@ -29,7 +29,7 @@ module DataMapper
|
|
29
29
|
end # module Support
|
30
30
|
end # module DataMapper
|
31
31
|
|
32
|
-
# Extend Array with DataMapper::Support::
|
32
|
+
# Extend Array with DataMapper::Support::EnumerableExtensions
|
33
33
|
class Array #:nodoc:
|
34
|
-
include DataMapper::Support::
|
34
|
+
include DataMapper::Support::EnumerableExtensions
|
35
35
|
end
|
@@ -1,7 +1,16 @@
|
|
1
1
|
module DataMapper
|
2
2
|
|
3
|
-
class
|
3
|
+
class ValidationError < StandardError; end
|
4
|
+
|
5
|
+
class ObjectNotFoundError < StandardError; end
|
4
6
|
|
5
7
|
class MaterializationError < StandardError; end
|
6
8
|
|
9
|
+
end
|
10
|
+
|
11
|
+
class StandardError
|
12
|
+
|
13
|
+
def display
|
14
|
+
"#{message}\n\t#{backtrace.join("\n\t")}"
|
15
|
+
end
|
7
16
|
end
|
@@ -1,276 +1,212 @@
|
|
1
|
-
# This file
|
1
|
+
# This file was copied from the ActiveSupport project, which
|
2
2
|
# is a part of the Ruby On Rails web-framework (http://rubyonrails.org).
|
3
|
+
# Some methods have been modified or removed.
|
3
4
|
|
4
5
|
require 'singleton'
|
5
6
|
|
6
7
|
# The Inflector transforms words from singular to plural, class names to table names, modularized class names to ones without,
|
7
8
|
# and class names to foreign keys. The default inflections for pluralization, singularization, and uncountable words are kept
|
8
9
|
# in inflections.rb.
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
attr_reader :plurals, :singulars, :uncountables
|
10
|
+
unless defined?(Inflector)
|
11
|
+
module Inflector
|
12
|
+
# A singleton instance of this class is yielded by Inflector.inflections, which can then be used to specify additional
|
13
|
+
# inflection rules. Examples:
|
14
|
+
#
|
15
|
+
# Inflector.inflections do |inflect|
|
16
|
+
# inflect.plural /^(ox)$/i, '\1\2en'
|
17
|
+
# inflect.singular /^(ox)en/i, '\1'
|
18
|
+
#
|
19
|
+
# inflect.irregular 'octopus', 'octopi'
|
20
|
+
#
|
21
|
+
# inflect.uncountable "equipment"
|
22
|
+
# end
|
23
|
+
#
|
24
|
+
# New rules are added at the top. So in the example above, the irregular rule for octopus will now be the first of the
|
25
|
+
# pluralization and singularization rules that is runs. This guarantees that your rules run before any of the rules that may
|
26
|
+
# already have been loaded.
|
27
|
+
class Inflections
|
28
|
+
include Singleton
|
29
29
|
|
30
|
-
|
31
|
-
@plurals, @singulars, @uncountables = [], [], []
|
32
|
-
end
|
30
|
+
attr_reader :plurals, :singulars, :uncountables
|
33
31
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
@plurals.insert(0, [rule, replacement])
|
38
|
-
end
|
32
|
+
def initialize
|
33
|
+
@plurals, @singulars, @uncountables = [], [], []
|
34
|
+
end
|
39
35
|
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
36
|
+
# Specifies a new pluralization rule and its replacement. The rule can either be a string or a regular expression.
|
37
|
+
# The replacement should always be a string that may include references to the matched data from the rule.
|
38
|
+
def plural(rule, replacement)
|
39
|
+
@plurals.insert(0, [rule, replacement])
|
40
|
+
end
|
45
41
|
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
# irregular 'person', 'people'
|
52
|
-
def irregular(singular, plural)
|
53
|
-
plural(Regexp.new("(#{singular[0,1]})#{singular[1..-1]}$", "i"), '\1' + plural[1..-1])
|
54
|
-
singular(Regexp.new("(#{plural[0,1]})#{plural[1..-1]}$", "i"), '\1' + singular[1..-1])
|
55
|
-
end
|
42
|
+
# Specifies a new singularization rule and its replacement. The rule can either be a string or a regular expression.
|
43
|
+
# The replacement should always be a string that may include references to the matched data from the rule.
|
44
|
+
def singular(rule, replacement)
|
45
|
+
@singulars.insert(0, [rule, replacement])
|
46
|
+
end
|
56
47
|
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
48
|
+
# Specifies a new irregular that applies to both pluralization and singularization at the same time. This can only be used
|
49
|
+
# for strings, not regular expressions. You simply pass the irregular in singular and plural form.
|
50
|
+
#
|
51
|
+
# Examples:
|
52
|
+
# irregular 'octopus', 'octopi'
|
53
|
+
# irregular 'person', 'people'
|
54
|
+
def irregular(singular, plural)
|
55
|
+
plural(Regexp.new("(#{singular[0,1]})#{singular[1..-1]}$", "i"), '\1' + plural[1..-1])
|
56
|
+
singular(Regexp.new("(#{plural[0,1]})#{plural[1..-1]}$", "i"), '\1' + singular[1..-1])
|
57
|
+
end
|
66
58
|
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
when :all
|
76
|
-
@plurals, @singulars, @uncountables = [], [], []
|
77
|
-
else
|
78
|
-
instance_variable_set "@#{scope}", []
|
59
|
+
# Add uncountable words that shouldn't be attempted inflected.
|
60
|
+
#
|
61
|
+
# Examples:
|
62
|
+
# uncountable "money"
|
63
|
+
# uncountable "money", "information"
|
64
|
+
# uncountable %w( money information rice )
|
65
|
+
def uncountable(*words)
|
66
|
+
(@uncountables << words).flatten!
|
79
67
|
end
|
80
68
|
end
|
81
|
-
end
|
82
69
|
|
83
|
-
|
70
|
+
extend self
|
84
71
|
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
72
|
+
def inflections
|
73
|
+
if block_given?
|
74
|
+
yield Inflections.instance
|
75
|
+
else
|
76
|
+
Inflections.instance
|
77
|
+
end
|
90
78
|
end
|
91
|
-
end
|
92
79
|
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
80
|
+
# Returns the plural form of the word in the string.
|
81
|
+
#
|
82
|
+
# Examples
|
83
|
+
# "post".pluralize #=> "posts"
|
84
|
+
# "octopus".pluralize #=> "octopi"
|
85
|
+
# "sheep".pluralize #=> "sheep"
|
86
|
+
# "words".pluralize #=> "words"
|
87
|
+
# "the blue mailman".pluralize #=> "the blue mailmen"
|
88
|
+
# "CamelOctopus".pluralize #=> "CamelOctopi"
|
89
|
+
def pluralize(word)
|
90
|
+
result = word.to_s.dup
|
104
91
|
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
92
|
+
if inflections.uncountables.include?(result.downcase)
|
93
|
+
result
|
94
|
+
else
|
95
|
+
inflections.plurals.each { |(rule, replacement)| break if result.gsub!(rule, replacement) }
|
96
|
+
result
|
97
|
+
end
|
110
98
|
end
|
111
|
-
end
|
112
99
|
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
100
|
+
# The reverse of pluralize, returns the singular form of a word in a string.
|
101
|
+
#
|
102
|
+
# Examples
|
103
|
+
# "posts".singularize #=> "post"
|
104
|
+
# "octopi".singularize #=> "octopus"
|
105
|
+
# "sheep".singluarize #=> "sheep"
|
106
|
+
# "word".singluarize #=> "word"
|
107
|
+
# "the blue mailmen".singularize #=> "the blue mailman"
|
108
|
+
# "CamelOctopi".singularize #=> "CamelOctopus"
|
109
|
+
def singularize(word)
|
110
|
+
result = word.to_s.dup
|
124
111
|
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
112
|
+
if inflections.uncountables.include?(result.downcase)
|
113
|
+
result
|
114
|
+
else
|
115
|
+
inflections.singulars.each { |(rule, replacement)| break if result.gsub!(rule, replacement) }
|
116
|
+
result
|
117
|
+
end
|
130
118
|
end
|
131
|
-
end
|
132
119
|
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
# "active_record/errors".camelize #=> "ActiveRecord::Errors"
|
142
|
-
# "active_record/errors".camelize(:lower) #=> "activeRecord::Errors"
|
143
|
-
def camelize(lower_case_and_underscored_word, first_letter_in_uppercase = true)
|
144
|
-
if first_letter_in_uppercase
|
120
|
+
# By default, camelize converts strings to UpperCamelCase.
|
121
|
+
#
|
122
|
+
# camelize will also convert '/' to '::' which is useful for converting paths to namespaces
|
123
|
+
#
|
124
|
+
# Examples
|
125
|
+
# "active_record".camelize #=> "ActiveRecord"
|
126
|
+
# "active_record/errors".camelize #=> "ActiveRecord::Errors"
|
127
|
+
def camelize(lower_case_and_underscored_word, *args)
|
145
128
|
lower_case_and_underscored_word.to_s.gsub(/\/(.?)/) { "::" + $1.upcase }.gsub(/(^|_)(.)/) { $2.upcase }
|
146
|
-
else
|
147
|
-
lower_case_and_underscored_word.first + camelize(lower_case_and_underscored_word)[1..-1]
|
148
129
|
end
|
149
|
-
end
|
150
130
|
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
#
|
166
|
-
# Changes '::' to '/' to convert namespaces to paths.
|
167
|
-
#
|
168
|
-
# Examples
|
169
|
-
# "ActiveRecord".underscore #=> "active_record"
|
170
|
-
# "ActiveRecord::Errors".underscore #=> active_record/errors
|
171
|
-
def underscore(camel_cased_word)
|
172
|
-
camel_cased_word.to_s.gsub(/::/, '/').
|
173
|
-
gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
|
174
|
-
gsub(/([a-z\d])([A-Z])/,'\1_\2').
|
175
|
-
tr("-", "_").
|
176
|
-
downcase
|
177
|
-
end
|
178
|
-
|
179
|
-
# Replaces underscores with dashes in the string.
|
180
|
-
#
|
181
|
-
# Example
|
182
|
-
# "puni_puni" #=> "puni-puni"
|
183
|
-
def dasherize(underscored_word)
|
184
|
-
underscored_word.gsub(/_/, '-')
|
185
|
-
end
|
186
|
-
|
187
|
-
# Capitalizes the first word and turns underscores into spaces and strips _id.
|
188
|
-
# Like titleize, this is meant for creating pretty output.
|
189
|
-
#
|
190
|
-
# Examples
|
191
|
-
# "employee_salary" #=> "Employee salary"
|
192
|
-
# "author_id" #=> "Author"
|
193
|
-
def humanize(lower_case_and_underscored_word)
|
194
|
-
lower_case_and_underscored_word.to_s.gsub(/_id$/, "").gsub(/_/, " ").capitalize
|
195
|
-
end
|
196
|
-
|
197
|
-
# Removes the module part from the expression in the string
|
198
|
-
#
|
199
|
-
# Examples
|
200
|
-
# "ActiveRecord::CoreExtensions::String::Inflections".demodulize #=> "Inflections"
|
201
|
-
# "Inflections".demodulize #=> "Inflections"
|
202
|
-
def demodulize(class_name_in_module)
|
203
|
-
class_name_in_module.to_s.gsub(/^.*::/, '')
|
204
|
-
end
|
131
|
+
# The reverse of +camelize+. Makes an underscored form from the expression in the string.
|
132
|
+
#
|
133
|
+
# Changes '::' to '/' to convert namespaces to paths.
|
134
|
+
#
|
135
|
+
# Examples
|
136
|
+
# "ActiveRecord".underscore #=> "active_record"
|
137
|
+
# "ActiveRecord::Errors".underscore #=> active_record/errors
|
138
|
+
def underscore(camel_cased_word)
|
139
|
+
camel_cased_word.to_s.gsub(/::/, '/').
|
140
|
+
gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
|
141
|
+
gsub(/([a-z\d])([A-Z])/,'\1_\2').
|
142
|
+
tr("-", "_").
|
143
|
+
downcase
|
144
|
+
end
|
205
145
|
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
end
|
146
|
+
# Capitalizes the first word and turns underscores into spaces and strips _id.
|
147
|
+
# Like titleize, this is meant for creating pretty output.
|
148
|
+
#
|
149
|
+
# Examples
|
150
|
+
# "employee_salary" #=> "Employee salary"
|
151
|
+
# "author_id" #=> "Author"
|
152
|
+
def humanize(lower_case_and_underscored_word)
|
153
|
+
lower_case_and_underscored_word.to_s.gsub(/_id$/, "").gsub(/_/, " ").capitalize
|
154
|
+
end
|
216
155
|
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
# strip out any leading schema name
|
226
|
-
camelize(singularize(table_name.to_s.sub(/.*\./, '')))
|
227
|
-
end
|
156
|
+
# Removes the module part from the expression in the string
|
157
|
+
#
|
158
|
+
# Examples
|
159
|
+
# "ActiveRecord::CoreExtensions::String::Inflections".demodulize #=> "Inflections"
|
160
|
+
# "Inflections".demodulize #=> "Inflections"
|
161
|
+
def demodulize(class_name_in_module)
|
162
|
+
class_name_in_module.to_s.gsub(/^.*::/, '')
|
163
|
+
end
|
228
164
|
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
end
|
165
|
+
# Create the name of a table like Rails does for models to table names. This method
|
166
|
+
# uses the pluralize method on the last word in the string.
|
167
|
+
#
|
168
|
+
# Examples
|
169
|
+
# "RawScaledScorer".tableize #=> "raw_scaled_scorers"
|
170
|
+
# "egg_and_ham".tableize #=> "egg_and_hams"
|
171
|
+
# "fancyCategory".tableize #=> "fancy_categories"
|
172
|
+
def tableize(class_name)
|
173
|
+
pluralize(underscore(class_name))
|
174
|
+
end
|
240
175
|
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
176
|
+
# Create a class name from a table name like Rails does for table names to models.
|
177
|
+
# Note that this returns a string and not a Class. (To convert to an actual class
|
178
|
+
# follow classify with constantize.)
|
179
|
+
#
|
180
|
+
# Examples
|
181
|
+
# "egg_and_hams".classify #=> "EggAndHam"
|
182
|
+
# "post".classify #=> "Post"
|
183
|
+
def classify(table_name)
|
184
|
+
# strip out any leading schema name
|
185
|
+
camelize(singularize(table_name.to_s.sub(/.*\./, '')))
|
251
186
|
end
|
252
187
|
|
253
|
-
|
254
|
-
|
188
|
+
# Creates a foreign key name from a class name.
|
189
|
+
#
|
190
|
+
# Examples
|
191
|
+
# "Message".foreign_key #=> "message_id"
|
192
|
+
# "Admin::Post".foreign_key #=> "post_id"
|
193
|
+
def foreign_key(class_name, key = "id")
|
194
|
+
underscore(demodulize(class_name.to_s)) << "_" << key.to_s
|
195
|
+
end
|
255
196
|
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
"#{number}th"
|
267
|
-
else
|
268
|
-
case number.to_i % 10
|
269
|
-
when 1: "#{number}st"
|
270
|
-
when 2: "#{number}nd"
|
271
|
-
when 3: "#{number}rd"
|
272
|
-
else "#{number}th"
|
197
|
+
# Constantize tries to find a declared constant with the name specified
|
198
|
+
# in the string. It raises a NameError when the name is not in CamelCase
|
199
|
+
# or is not initialized.
|
200
|
+
#
|
201
|
+
# Examples
|
202
|
+
# "Module".constantize #=> Module
|
203
|
+
# "Class".constantize #=> Class
|
204
|
+
def constantize(camel_cased_word)
|
205
|
+
unless /\A(?:::)?([A-Z]\w*(?:::[A-Z]\w*)*)\z/ =~ camel_cased_word
|
206
|
+
raise NameError, "#{camel_cased_word.inspect} is not a valid constant name!"
|
273
207
|
end
|
208
|
+
|
209
|
+
Object.module_eval("::#{$1}", __FILE__, __LINE__)
|
274
210
|
end
|
275
211
|
end
|
276
212
|
end
|
@@ -326,4 +262,4 @@ Inflector.inflections do |inflect|
|
|
326
262
|
inflect.irregular('move', 'moves')
|
327
263
|
|
328
264
|
inflect.uncountable(%w(equipment information rice money species series fish sheep))
|
329
|
-
end
|
265
|
+
end
|