dm-core 1.1.0.rc2 → 1.1.0.rc3
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.
- data/Gemfile +1 -8
- data/VERSION +1 -1
- data/dm-core.gemspec +24 -15
- data/lib/dm-core.rb +9 -48
- data/lib/dm-core/adapters.rb +1 -1
- data/lib/dm-core/adapters/abstract_adapter.rb +2 -1
- data/lib/dm-core/associations/many_to_many.rb +3 -3
- data/lib/dm-core/associations/many_to_one.rb +15 -4
- data/lib/dm-core/associations/one_to_many.rb +1 -1
- data/lib/dm-core/associations/relationship.rb +7 -6
- data/lib/dm-core/collection.rb +7 -2
- data/lib/dm-core/core_ext/pathname.rb +0 -14
- data/lib/dm-core/ext/array.rb +41 -0
- data/lib/dm-core/ext/blank.rb +24 -0
- data/lib/dm-core/ext/hash.rb +67 -0
- data/lib/dm-core/ext/module.rb +49 -0
- data/lib/dm-core/ext/object.rb +57 -0
- data/lib/dm-core/ext/singleton_class.rb +8 -0
- data/lib/dm-core/ext/string.rb +24 -0
- data/lib/dm-core/ext/try_dup.rb +12 -0
- data/lib/dm-core/model.rb +2 -1
- data/lib/dm-core/model/property.rb +3 -2
- data/lib/dm-core/property.rb +1 -1
- data/lib/dm-core/property/class.rb +1 -1
- data/lib/dm-core/property/date.rb +3 -3
- data/lib/dm-core/property/date_time.rb +3 -3
- data/lib/dm-core/property/time.rb +3 -3
- data/lib/dm-core/property/typecast/time.rb +2 -2
- data/lib/dm-core/property_set.rb +1 -1
- data/lib/dm-core/query.rb +9 -12
- data/lib/dm-core/resource.rb +2 -2
- data/lib/dm-core/spec/lib/counter_adapter.rb +1 -1
- data/lib/dm-core/spec/lib/spec_helper.rb +1 -1
- data/lib/dm-core/spec/shared/resource_spec.rb +2 -1
- data/lib/dm-core/support/equalizer.rb +1 -1
- data/lib/dm-core/support/hook.rb +0 -15
- data/lib/dm-core/support/inflections.rb +60 -0
- data/lib/dm-core/support/inflector.rb +3 -0
- data/lib/dm-core/support/inflector/inflections.rb +211 -0
- data/lib/dm-core/support/inflector/methods.rb +151 -0
- data/lib/dm-core/support/lazy_array.rb +4 -4
- data/lib/dm-core/support/mash.rb +176 -0
- data/lib/dm-core/support/subject.rb +1 -1
- data/lib/dm-core/version.rb +1 -1
- data/spec/public/model/relationship_spec.rb +2 -1
- data/spec/public/shared/collection_shared_spec.rb +5 -2
- data/spec/public/shared/finder_shared_spec.rb +10 -6
- data/spec/semipublic/adapters/in_memory_adapter_spec.rb +1 -1
- data/spec/semipublic/query_spec.rb +2 -2
- data/spec/semipublic/resource/state/immutable_spec.rb +2 -1
- data/spec/semipublic/resource/state_spec.rb +1 -3
- data/spec/semipublic/shared/resource_state_shared_spec.rb +2 -1
- data/spec/semipublic/shared/subject_shared_spec.rb +1 -1
- data/spec/support/core_ext/hash.rb +10 -0
- data/spec/support/core_ext/inheritable_attributes.rb +46 -0
- data/spec/unit/array_spec.rb +8 -20
- data/spec/unit/blank_spec.rb +62 -0
- data/spec/unit/data_mapper/ordered_set/hash_spec.rb +1 -1
- data/spec/unit/hash_spec.rb +8 -16
- data/spec/unit/lazy_array_spec.rb +3 -14
- data/spec/unit/mash_spec.rb +312 -0
- data/spec/unit/module_spec.rb +15 -15
- data/spec/unit/object_spec.rb +9 -9
- data/spec/unit/try_dup_spec.rb +8 -8
- metadata +32 -39
- data/lib/dm-core/core_ext/array.rb +0 -36
- data/lib/dm-core/core_ext/hash.rb +0 -30
- data/lib/dm-core/core_ext/module.rb +0 -46
- data/lib/dm-core/core_ext/object.rb +0 -31
- data/lib/dm-core/core_ext/string.rb +0 -22
- data/lib/dm-core/core_ext/try_dup.rb +0 -44
data/lib/dm-core/resource.rb
CHANGED
@@ -519,7 +519,7 @@ module DataMapper
|
|
519
519
|
#
|
520
520
|
# @api private
|
521
521
|
def hash
|
522
|
-
key.hash
|
522
|
+
model.hash ^ key.hash
|
523
523
|
end
|
524
524
|
|
525
525
|
# Get a Human-readable representation of this Resource instance
|
@@ -752,7 +752,7 @@ module DataMapper
|
|
752
752
|
# @api private
|
753
753
|
def initialize_copy(original)
|
754
754
|
instance_variables.each do |ivar|
|
755
|
-
instance_variable_set(ivar, instance_variable_get(ivar)
|
755
|
+
instance_variable_set(ivar, DataMapper::Ext.try_dup(instance_variable_get(ivar)))
|
756
756
|
end
|
757
757
|
|
758
758
|
self.persisted_state = persisted_state.class.new(self)
|
@@ -1,7 +1,7 @@
|
|
1
1
|
class CounterAdapter < DataMapper::Adapters::AbstractAdapter
|
2
2
|
instance_methods.each do |method|
|
3
3
|
next if method =~ /\A__/ ||
|
4
|
-
%w[ send class dup object_id kind_of? instance_of? respond_to? equal? freeze frozen? should should_not instance_variables instance_variable_set instance_variable_get instance_variable_defined? remove_instance_variable extend
|
4
|
+
%w[ send class dup object_id kind_of? instance_of? respond_to? equal? freeze frozen? should should_not instance_variables instance_variable_set instance_variable_get instance_variable_defined? remove_instance_variable extend inspect copy_object ].include?(method.to_s)
|
5
5
|
undef_method method
|
6
6
|
end
|
7
7
|
|
@@ -21,7 +21,7 @@ module DataMapper
|
|
21
21
|
unless model_name.empty? || model_name[0] == ?#
|
22
22
|
parts = model_name.split('::')
|
23
23
|
constant_name = parts.pop.to_sym
|
24
|
-
base = parts.empty? ? Object : Object.full_const_get(parts.join('::'))
|
24
|
+
base = parts.empty? ? Object : DataMapper::Ext::Object.full_const_get(parts.join('::'))
|
25
25
|
|
26
26
|
base.class_eval { remove_const(constant_name) if const_defined?(constant_name) }
|
27
27
|
end
|
@@ -214,7 +214,8 @@ share_examples_for 'A public Resource' do
|
|
214
214
|
|
215
215
|
describe 'with a saved resource' do
|
216
216
|
it 'should return the expected values' do
|
217
|
-
@user.attributes
|
217
|
+
DataMapper::Ext::Hash.only(@user.attributes, :name, :description, :age).should ==
|
218
|
+
{ :name => 'dbussink', :description => 'Test', :age => 25 }
|
218
219
|
end
|
219
220
|
end
|
220
221
|
end
|
@@ -40,7 +40,7 @@ module DataMapper
|
|
40
40
|
def define_hash_method(methods)
|
41
41
|
class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
42
42
|
def hash
|
43
|
-
#{methods.map { |method| "#{method}.hash" }.join(' ^ ')}
|
43
|
+
self.class.hash ^ #{methods.map { |method| "#{method}.hash" }.join(' ^ ')}
|
44
44
|
end
|
45
45
|
RUBY
|
46
46
|
end
|
data/lib/dm-core/support/hook.rb
CHANGED
@@ -1,21 +1,6 @@
|
|
1
1
|
require 'dm-core/support/assertions'
|
2
2
|
require 'dm-core/support/local_object_space'
|
3
3
|
|
4
|
-
# Mainly done here to support spec/unit/hook_spec without
|
5
|
-
# needing to require dm-core to setup either extlib or AS
|
6
|
-
|
7
|
-
begin
|
8
|
-
require 'active_support/core_ext/object/singleton_class'
|
9
|
-
rescue LoadError
|
10
|
-
class Object
|
11
|
-
unless respond_to?(:singleton_class)
|
12
|
-
def singleton_class
|
13
|
-
class << self; self end
|
14
|
-
end
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
4
|
module DataMapper
|
20
5
|
#
|
21
6
|
# TODO: Write more documentation!
|
@@ -0,0 +1,60 @@
|
|
1
|
+
module DataMapper
|
2
|
+
Inflector.inflections do |inflect|
|
3
|
+
inflect.plural(/$/, 's')
|
4
|
+
inflect.plural(/s$/i, 's')
|
5
|
+
inflect.plural(/(ax|test)is$/i, '\1es')
|
6
|
+
inflect.plural(/(octop|vir)us$/i, '\1i')
|
7
|
+
inflect.plural(/(octop|vir)i$/i, '\1i')
|
8
|
+
inflect.plural(/(alias|status)$/i, '\1es')
|
9
|
+
inflect.plural(/(bu)s$/i, '\1ses')
|
10
|
+
inflect.plural(/(buffal|tomat)o$/i, '\1oes')
|
11
|
+
inflect.plural(/([ti])um$/i, '\1a')
|
12
|
+
inflect.plural(/([ti])a$/i, '\1a')
|
13
|
+
inflect.plural(/sis$/i, 'ses')
|
14
|
+
inflect.plural(/(?:([^f])fe|([lr])f)$/i, '\1\2ves')
|
15
|
+
inflect.plural(/(hive)$/i, '\1s')
|
16
|
+
inflect.plural(/([^aeiouy]|qu)y$/i, '\1ies')
|
17
|
+
inflect.plural(/(x|ch|ss|sh)$/i, '\1es')
|
18
|
+
inflect.plural(/(matr|vert|ind)(?:ix|ex)$/i, '\1ices')
|
19
|
+
inflect.plural(/([m|l])ouse$/i, '\1ice')
|
20
|
+
inflect.plural(/([m|l])ice$/i, '\1ice')
|
21
|
+
inflect.plural(/^(ox)$/i, '\1en')
|
22
|
+
inflect.plural(/^(oxen)$/i, '\1')
|
23
|
+
inflect.plural(/(quiz)$/i, '\1zes')
|
24
|
+
|
25
|
+
inflect.singular(/s$/i, '')
|
26
|
+
inflect.singular(/(n)ews$/i, '\1ews')
|
27
|
+
inflect.singular(/([ti])a$/i, '\1um')
|
28
|
+
inflect.singular(/((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/i, '\1\2sis')
|
29
|
+
inflect.singular(/(^analy)ses$/i, '\1sis')
|
30
|
+
inflect.singular(/([^f])ves$/i, '\1fe')
|
31
|
+
inflect.singular(/(hive)s$/i, '\1')
|
32
|
+
inflect.singular(/(tive)s$/i, '\1')
|
33
|
+
inflect.singular(/([lr])ves$/i, '\1f')
|
34
|
+
inflect.singular(/([^aeiouy]|qu)ies$/i, '\1y')
|
35
|
+
inflect.singular(/(s)eries$/i, '\1eries')
|
36
|
+
inflect.singular(/(m)ovies$/i, '\1ovie')
|
37
|
+
inflect.singular(/(x|ch|ss|sh)es$/i, '\1')
|
38
|
+
inflect.singular(/([m|l])ice$/i, '\1ouse')
|
39
|
+
inflect.singular(/(bus)es$/i, '\1')
|
40
|
+
inflect.singular(/(o)es$/i, '\1')
|
41
|
+
inflect.singular(/(shoe)s$/i, '\1')
|
42
|
+
inflect.singular(/(cris|ax|test)es$/i, '\1is')
|
43
|
+
inflect.singular(/(octop|vir)i$/i, '\1us')
|
44
|
+
inflect.singular(/(alias|status)es$/i, '\1')
|
45
|
+
inflect.singular(/^(ox)en/i, '\1')
|
46
|
+
inflect.singular(/(vert|ind)ices$/i, '\1ex')
|
47
|
+
inflect.singular(/(matr)ices$/i, '\1ix')
|
48
|
+
inflect.singular(/(quiz)zes$/i, '\1')
|
49
|
+
inflect.singular(/(database)s$/i, '\1')
|
50
|
+
|
51
|
+
inflect.irregular('person', 'people')
|
52
|
+
inflect.irregular('man', 'men')
|
53
|
+
inflect.irregular('child', 'children')
|
54
|
+
inflect.irregular('sex', 'sexes')
|
55
|
+
inflect.irregular('move', 'moves')
|
56
|
+
inflect.irregular('cow', 'kine')
|
57
|
+
|
58
|
+
inflect.uncountable(%w(equipment information rice money species series fish sheep jeans))
|
59
|
+
end
|
60
|
+
end
|
@@ -0,0 +1,211 @@
|
|
1
|
+
module DataMapper
|
2
|
+
module Inflector
|
3
|
+
# A singleton instance of this class is yielded by Inflector.inflections, which can then be used to specify additional
|
4
|
+
# inflection rules. Examples:
|
5
|
+
#
|
6
|
+
# DataMapper::Inflector.inflections do |inflect|
|
7
|
+
# inflect.plural /^(ox)$/i, '\1\2en'
|
8
|
+
# inflect.singular /^(ox)en/i, '\1'
|
9
|
+
#
|
10
|
+
# inflect.irregular 'octopus', 'octopi'
|
11
|
+
#
|
12
|
+
# inflect.uncountable "equipment"
|
13
|
+
# end
|
14
|
+
#
|
15
|
+
# New rules are added at the top. So in the example above, the irregular rule for octopus will now be the first of the
|
16
|
+
# pluralization and singularization rules that is runs. This guarantees that your rules run before any of the rules that may
|
17
|
+
# already have been loaded.
|
18
|
+
class Inflections
|
19
|
+
def self.instance
|
20
|
+
@__instance__ ||= new
|
21
|
+
end
|
22
|
+
|
23
|
+
attr_reader :plurals, :singulars, :uncountables, :humans
|
24
|
+
|
25
|
+
def initialize
|
26
|
+
@plurals, @singulars, @uncountables, @humans = [], [], [], []
|
27
|
+
end
|
28
|
+
|
29
|
+
# Specifies a new pluralization rule and its replacement. The rule can either be a string or a regular expression.
|
30
|
+
# The replacement should always be a string that may include references to the matched data from the rule.
|
31
|
+
def plural(rule, replacement)
|
32
|
+
@uncountables.delete(rule) if rule.is_a?(String)
|
33
|
+
@uncountables.delete(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
|
+
@uncountables.delete(rule) if rule.is_a?(String)
|
41
|
+
@uncountables.delete(replacement)
|
42
|
+
@singulars.insert(0, [rule, replacement])
|
43
|
+
end
|
44
|
+
|
45
|
+
# Specifies a new irregular that applies to both pluralization and singularization at the same time. This can only be used
|
46
|
+
# for strings, not regular expressions. You simply pass the irregular in singular and plural form.
|
47
|
+
#
|
48
|
+
# Examples:
|
49
|
+
# irregular 'octopus', 'octopi'
|
50
|
+
# irregular 'person', 'people'
|
51
|
+
def irregular(singular, plural)
|
52
|
+
@uncountables.delete(singular)
|
53
|
+
@uncountables.delete(plural)
|
54
|
+
if singular[0,1].upcase == plural[0,1].upcase
|
55
|
+
plural(Regexp.new("(#{singular[0,1]})#{singular[1..-1]}$", "i"), '\1' + plural[1..-1])
|
56
|
+
plural(Regexp.new("(#{plural[0,1]})#{plural[1..-1]}$", "i"), '\1' + plural[1..-1])
|
57
|
+
singular(Regexp.new("(#{plural[0,1]})#{plural[1..-1]}$", "i"), '\1' + singular[1..-1])
|
58
|
+
else
|
59
|
+
plural(Regexp.new("#{singular[0,1].upcase}(?i)#{singular[1..-1]}$"), plural[0,1].upcase + plural[1..-1])
|
60
|
+
plural(Regexp.new("#{singular[0,1].downcase}(?i)#{singular[1..-1]}$"), plural[0,1].downcase + plural[1..-1])
|
61
|
+
plural(Regexp.new("#{plural[0,1].upcase}(?i)#{plural[1..-1]}$"), plural[0,1].upcase + plural[1..-1])
|
62
|
+
plural(Regexp.new("#{plural[0,1].downcase}(?i)#{plural[1..-1]}$"), plural[0,1].downcase + plural[1..-1])
|
63
|
+
singular(Regexp.new("#{plural[0,1].upcase}(?i)#{plural[1..-1]}$"), singular[0,1].upcase + singular[1..-1])
|
64
|
+
singular(Regexp.new("#{plural[0,1].downcase}(?i)#{plural[1..-1]}$"), singular[0,1].downcase + singular[1..-1])
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
# Add uncountable words that shouldn't be attempted inflected.
|
69
|
+
#
|
70
|
+
# Examples:
|
71
|
+
# uncountable "money"
|
72
|
+
# uncountable "money", "information"
|
73
|
+
# uncountable %w( money information rice )
|
74
|
+
def uncountable(*words)
|
75
|
+
(@uncountables << words).flatten!
|
76
|
+
end
|
77
|
+
|
78
|
+
# Specifies a humanized form of a string by a regular expression rule or by a string mapping.
|
79
|
+
# When using a regular expression based replacement, the normal humanize formatting is called after the replacement.
|
80
|
+
# When a string is used, the human form should be specified as desired (example: 'The name', not 'the_name')
|
81
|
+
#
|
82
|
+
# Examples:
|
83
|
+
# human /_cnt$/i, '\1_count'
|
84
|
+
# human "legacy_col_person_name", "Name"
|
85
|
+
def human(rule, replacement)
|
86
|
+
@humans.insert(0, [rule, replacement])
|
87
|
+
end
|
88
|
+
|
89
|
+
# Clears the loaded inflections within a given scope (default is <tt>:all</tt>).
|
90
|
+
# Give the scope as a symbol of the inflection type, the options are: <tt>:plurals</tt>,
|
91
|
+
# <tt>:singulars</tt>, <tt>:uncountables</tt>, <tt>:humans</tt>.
|
92
|
+
#
|
93
|
+
# Examples:
|
94
|
+
# clear :all
|
95
|
+
# clear :plurals
|
96
|
+
def clear(scope = :all)
|
97
|
+
case scope
|
98
|
+
when :all
|
99
|
+
@plurals, @singulars, @uncountables = [], [], []
|
100
|
+
else
|
101
|
+
instance_variable_set "@#{scope}", []
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
# Yields a singleton instance of Inflector::Inflections so you can specify additional
|
107
|
+
# inflector rules.
|
108
|
+
#
|
109
|
+
# Example:
|
110
|
+
# DataMapper::Inflector.inflections do |inflect|
|
111
|
+
# inflect.uncountable "rails"
|
112
|
+
# end
|
113
|
+
def inflections
|
114
|
+
if block_given?
|
115
|
+
yield Inflections.instance
|
116
|
+
else
|
117
|
+
Inflections.instance
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
# Returns the plural form of the word in the string.
|
122
|
+
#
|
123
|
+
# Examples:
|
124
|
+
# "post".pluralize # => "posts"
|
125
|
+
# "octopus".pluralize # => "octopi"
|
126
|
+
# "sheep".pluralize # => "sheep"
|
127
|
+
# "words".pluralize # => "words"
|
128
|
+
# "CamelOctopus".pluralize # => "CamelOctopi"
|
129
|
+
def pluralize(word)
|
130
|
+
result = word.to_s.dup
|
131
|
+
|
132
|
+
if word.empty? || inflections.uncountables.include?(result.downcase)
|
133
|
+
result
|
134
|
+
else
|
135
|
+
inflections.plurals.each { |(rule, replacement)| break if result.gsub!(rule, replacement) }
|
136
|
+
result
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
# The reverse of +pluralize+, returns the singular form of a word in a string.
|
141
|
+
#
|
142
|
+
# Examples:
|
143
|
+
# "posts".singularize # => "post"
|
144
|
+
# "octopi".singularize # => "octopus"
|
145
|
+
# "sheep".singularize # => "sheep"
|
146
|
+
# "word".singularize # => "word"
|
147
|
+
# "CamelOctopi".singularize # => "CamelOctopus"
|
148
|
+
def singularize(word)
|
149
|
+
result = word.to_s.dup
|
150
|
+
|
151
|
+
if inflections.uncountables.any? { |inflection| result =~ /\b(#{inflection})\Z/i }
|
152
|
+
result
|
153
|
+
else
|
154
|
+
inflections.singulars.each { |(rule, replacement)| break if result.gsub!(rule, replacement) }
|
155
|
+
result
|
156
|
+
end
|
157
|
+
end
|
158
|
+
|
159
|
+
# Capitalizes the first word and turns underscores into spaces and strips a
|
160
|
+
# trailing "_id", if any. Like +titleize+, this is meant for creating pretty output.
|
161
|
+
#
|
162
|
+
# Examples:
|
163
|
+
# "employee_salary" # => "Employee salary"
|
164
|
+
# "author_id" # => "Author"
|
165
|
+
def humanize(lower_case_and_underscored_word)
|
166
|
+
result = lower_case_and_underscored_word.to_s.dup
|
167
|
+
|
168
|
+
inflections.humans.each { |(rule, replacement)| break if result.gsub!(rule, replacement) }
|
169
|
+
result.gsub(/_id$/, "").gsub(/_/, " ").capitalize
|
170
|
+
end
|
171
|
+
|
172
|
+
# Capitalizes all the words and replaces some characters in the string to create
|
173
|
+
# a nicer looking title. +titleize+ is meant for creating pretty output. It is not
|
174
|
+
# used in the Rails internals.
|
175
|
+
#
|
176
|
+
# +titleize+ is also aliased as as +titlecase+.
|
177
|
+
#
|
178
|
+
# Examples:
|
179
|
+
# "man from the boondocks".titleize # => "Man From The Boondocks"
|
180
|
+
# "x-men: the last stand".titleize # => "X Men: The Last Stand"
|
181
|
+
def titleize(word)
|
182
|
+
humanize(underscore(word)).gsub(/\b('?[a-z])/) { $1.capitalize }
|
183
|
+
end
|
184
|
+
|
185
|
+
# Create the name of a table like Rails does for models to table names. This method
|
186
|
+
# uses the +pluralize+ method on the last word in the string.
|
187
|
+
#
|
188
|
+
# Examples
|
189
|
+
# "RawScaledScorer".tableize # => "raw_scaled_scorers"
|
190
|
+
# "egg_and_ham".tableize # => "egg_and_hams"
|
191
|
+
# "fancyCategory".tableize # => "fancy_categories"
|
192
|
+
def tableize(class_name)
|
193
|
+
pluralize(underscore(class_name))
|
194
|
+
end
|
195
|
+
|
196
|
+
# Create a class name from a plural table name like Rails does for table names to models.
|
197
|
+
# Note that this returns a string and not a Class. (To convert to an actual class
|
198
|
+
# follow +classify+ with +constantize+.)
|
199
|
+
#
|
200
|
+
# Examples:
|
201
|
+
# "egg_and_hams".classify # => "EggAndHam"
|
202
|
+
# "posts".classify # => "Post"
|
203
|
+
#
|
204
|
+
# Singular names are not handled correctly:
|
205
|
+
# "business".classify # => "Busines"
|
206
|
+
def classify(table_name)
|
207
|
+
# strip out any leading schema name
|
208
|
+
camelize(singularize(table_name.to_s.sub(/.*\./, '')))
|
209
|
+
end
|
210
|
+
end
|
211
|
+
end
|
@@ -0,0 +1,151 @@
|
|
1
|
+
module DataMapper
|
2
|
+
# The Inflector transforms words from singular to plural, class names to table names, modularized class names to ones without,
|
3
|
+
# and class names to foreign keys. The default inflections for pluralization, singularization, and uncountable words are kept
|
4
|
+
# in inflections.rb.
|
5
|
+
#
|
6
|
+
# The Rails core team has stated patches for the inflections library will not be accepted
|
7
|
+
# in order to avoid breaking legacy applications which may be relying on errant inflections.
|
8
|
+
# If you discover an incorrect inflection and require it for your application, you'll need
|
9
|
+
# to correct it yourself (explained below).
|
10
|
+
module Inflector
|
11
|
+
extend self
|
12
|
+
|
13
|
+
# By default, +camelize+ converts strings to UpperCamelCase. If the argument to +camelize+
|
14
|
+
# is set to <tt>:lower</tt> then +camelize+ produces lowerCamelCase.
|
15
|
+
#
|
16
|
+
# +camelize+ will also convert '/' to '::' which is useful for converting paths to namespaces.
|
17
|
+
#
|
18
|
+
# Examples:
|
19
|
+
# "active_record".camelize # => "ActiveRecord"
|
20
|
+
# "active_record".camelize(:lower) # => "activeRecord"
|
21
|
+
# "active_record/errors".camelize # => "ActiveRecord::Errors"
|
22
|
+
# "active_record/errors".camelize(:lower) # => "activeRecord::Errors"
|
23
|
+
#
|
24
|
+
# As a rule of thumb you can think of +camelize+ as the inverse of +underscore+,
|
25
|
+
# though there are cases where that does not hold:
|
26
|
+
#
|
27
|
+
# "SSLError".underscore.camelize # => "SslError"
|
28
|
+
def camelize(lower_case_and_underscored_word, first_letter_in_uppercase = true)
|
29
|
+
if first_letter_in_uppercase
|
30
|
+
lower_case_and_underscored_word.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
|
31
|
+
else
|
32
|
+
lower_case_and_underscored_word.to_s[0].chr.downcase + camelize(lower_case_and_underscored_word)[1..-1]
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
# Makes an underscored, lowercase form from the expression in the string.
|
37
|
+
#
|
38
|
+
# Changes '::' to '/' to convert namespaces to paths.
|
39
|
+
#
|
40
|
+
# Examples:
|
41
|
+
# "ActiveRecord".underscore # => "active_record"
|
42
|
+
# "ActiveRecord::Errors".underscore # => active_record/errors
|
43
|
+
#
|
44
|
+
# As a rule of thumb you can think of +underscore+ as the inverse of +camelize+,
|
45
|
+
# though there are cases where that does not hold:
|
46
|
+
#
|
47
|
+
# "SSLError".underscore.camelize # => "SslError"
|
48
|
+
def underscore(camel_cased_word)
|
49
|
+
word = camel_cased_word.to_s.dup
|
50
|
+
word.gsub!(/::/, '/')
|
51
|
+
word.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
|
52
|
+
word.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
|
53
|
+
word.tr!("-", "_")
|
54
|
+
word.downcase!
|
55
|
+
word
|
56
|
+
end
|
57
|
+
|
58
|
+
# Replaces underscores with dashes in the string.
|
59
|
+
#
|
60
|
+
# Example:
|
61
|
+
# "puni_puni" # => "puni-puni"
|
62
|
+
def dasherize(underscored_word)
|
63
|
+
underscored_word.gsub(/_/, '-')
|
64
|
+
end
|
65
|
+
|
66
|
+
# Removes the module part from the expression in the string.
|
67
|
+
#
|
68
|
+
# Examples:
|
69
|
+
# "ActiveRecord::CoreExtensions::String::Inflections".demodulize # => "Inflections"
|
70
|
+
# "Inflections".demodulize # => "Inflections"
|
71
|
+
def demodulize(class_name_in_module)
|
72
|
+
class_name_in_module.to_s.gsub(/^.*::/, '')
|
73
|
+
end
|
74
|
+
|
75
|
+
# Creates a foreign key name from a class name.
|
76
|
+
# +separate_class_name_and_id_with_underscore+ sets whether
|
77
|
+
# the method should put '_' between the name and 'id'.
|
78
|
+
#
|
79
|
+
# Examples:
|
80
|
+
# "Message".foreign_key # => "message_id"
|
81
|
+
# "Message".foreign_key(false) # => "messageid"
|
82
|
+
# "Admin::Post".foreign_key # => "post_id"
|
83
|
+
def foreign_key(class_name, separate_class_name_and_id_with_underscore = true)
|
84
|
+
underscore(demodulize(class_name)) + (separate_class_name_and_id_with_underscore ? "_id" : "id")
|
85
|
+
end
|
86
|
+
|
87
|
+
# Ruby 1.9 introduces an inherit argument for Module#const_get and
|
88
|
+
# #const_defined? and changes their default behavior.
|
89
|
+
if Module.method(:const_get).arity == 1
|
90
|
+
# Tries to find a constant with the name specified in the argument string:
|
91
|
+
#
|
92
|
+
# "Module".constantize # => Module
|
93
|
+
# "Test::Unit".constantize # => Test::Unit
|
94
|
+
#
|
95
|
+
# The name is assumed to be the one of a top-level constant, no matter whether
|
96
|
+
# it starts with "::" or not. No lexical context is taken into account:
|
97
|
+
#
|
98
|
+
# C = 'outside'
|
99
|
+
# module M
|
100
|
+
# C = 'inside'
|
101
|
+
# C # => 'inside'
|
102
|
+
# "C".constantize # => 'outside', same as ::C
|
103
|
+
# end
|
104
|
+
#
|
105
|
+
# NameError is raised when the name is not in CamelCase or the constant is
|
106
|
+
# unknown.
|
107
|
+
def constantize(camel_cased_word)
|
108
|
+
names = camel_cased_word.split('::')
|
109
|
+
names.shift if names.empty? || names.first.empty?
|
110
|
+
|
111
|
+
constant = Object
|
112
|
+
names.each do |name|
|
113
|
+
constant = constant.const_defined?(name) ? constant.const_get(name) : constant.const_missing(name)
|
114
|
+
end
|
115
|
+
constant
|
116
|
+
end
|
117
|
+
else
|
118
|
+
def constantize(camel_cased_word) #:nodoc:
|
119
|
+
names = camel_cased_word.split('::')
|
120
|
+
names.shift if names.empty? || names.first.empty?
|
121
|
+
|
122
|
+
constant = Object
|
123
|
+
names.each do |name|
|
124
|
+
constant = constant.const_defined?(name, false) ? constant.const_get(name) : constant.const_missing(name)
|
125
|
+
end
|
126
|
+
constant
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
# Turns a number into an ordinal string used to denote the position in an
|
131
|
+
# ordered sequence such as 1st, 2nd, 3rd, 4th.
|
132
|
+
#
|
133
|
+
# Examples:
|
134
|
+
# ordinalize(1) # => "1st"
|
135
|
+
# ordinalize(2) # => "2nd"
|
136
|
+
# ordinalize(1002) # => "1002nd"
|
137
|
+
# ordinalize(1003) # => "1003rd"
|
138
|
+
def ordinalize(number)
|
139
|
+
if (11..13).include?(number.to_i % 100)
|
140
|
+
"#{number}th"
|
141
|
+
else
|
142
|
+
case number.to_i % 10
|
143
|
+
when 1; "#{number}st"
|
144
|
+
when 2; "#{number}nd"
|
145
|
+
when 3; "#{number}rd"
|
146
|
+
else "#{number}th"
|
147
|
+
end
|
148
|
+
end
|
149
|
+
end
|
150
|
+
end
|
151
|
+
end
|