motion-support 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +3 -0
- data/Gemfile +1 -1
- data/README.md +231 -2
- data/Rakefile +3 -3
- data/app/app_delegate.rb +0 -2
- data/examples/Inflector/.gitignore +16 -0
- data/examples/Inflector/Gemfile +5 -0
- data/examples/Inflector/Rakefile +13 -0
- data/examples/Inflector/app/app_delegate.rb +8 -0
- data/examples/Inflector/app/inflections.rb +5 -0
- data/examples/Inflector/app/inflector_view_controller.rb +120 -0
- data/examples/Inflector/app/words_view_controller.rb +101 -0
- data/examples/Inflector/resources/Default-568h@2x.png +0 -0
- data/examples/Inflector/spec/main_spec.rb +9 -0
- data/lib/motion-support/core_ext/array.rb +13 -0
- data/lib/motion-support/core_ext/class.rb +8 -0
- data/lib/motion-support/core_ext/hash.rb +16 -0
- data/lib/motion-support/core_ext/integer.rb +9 -0
- data/lib/motion-support/core_ext/module.rb +14 -0
- data/lib/motion-support/core_ext/numeric.rb +8 -0
- data/lib/motion-support/core_ext/object.rb +14 -0
- data/lib/motion-support/core_ext/range.rb +8 -0
- data/lib/motion-support/core_ext/string.rb +14 -0
- data/lib/motion-support/core_ext/time.rb +19 -0
- data/lib/motion-support/core_ext.rb +3 -0
- data/lib/motion-support/inflector.rb +12 -156
- data/lib/motion-support.rb +5 -5
- data/motion/_stdlib/cgi.rb +22 -0
- data/motion/_stdlib/date.rb +77 -0
- data/motion/_stdlib/time.rb +19 -0
- data/motion/core_ext/array/access.rb +28 -0
- data/motion/core_ext/array/conversions.rb +86 -0
- data/motion/core_ext/array/extract_options.rb +11 -0
- data/motion/core_ext/array/grouping.rb +99 -0
- data/motion/core_ext/array/prepend_and_append.rb +7 -0
- data/motion/core_ext/array/wrap.rb +45 -0
- data/{lib/motion-support → motion/core_ext}/array.rb +0 -4
- data/motion/core_ext/class/attribute.rb +119 -0
- data/motion/core_ext/class/attribute_accessors.rb +168 -0
- data/motion/core_ext/date/acts_like.rb +8 -0
- data/motion/core_ext/date/calculations.rb +117 -0
- data/motion/core_ext/date/conversions.rb +56 -0
- data/motion/core_ext/date_and_time/calculations.rb +232 -0
- data/motion/core_ext/enumerable.rb +90 -0
- data/motion/core_ext/hash/deep_merge.rb +27 -0
- data/motion/core_ext/hash/except.rb +15 -0
- data/motion/core_ext/hash/indifferent_access.rb +19 -0
- data/motion/core_ext/hash/keys.rb +138 -0
- data/motion/core_ext/hash/reverse_merge.rb +22 -0
- data/motion/core_ext/hash/slice.rb +40 -0
- data/motion/core_ext/integer/inflections.rb +27 -0
- data/motion/core_ext/integer/multiple.rb +10 -0
- data/motion/core_ext/integer/time.rb +41 -0
- data/{lib/motion-support → motion/core_ext}/metaclass.rb +0 -0
- data/motion/core_ext/module/aliasing.rb +69 -0
- data/motion/core_ext/module/anonymous.rb +19 -0
- data/motion/core_ext/module/attr_internal.rb +38 -0
- data/motion/core_ext/module/attribute_accessors.rb +64 -0
- data/motion/core_ext/module/delegation.rb +175 -0
- data/motion/core_ext/module/introspection.rb +60 -0
- data/motion/core_ext/module/reachable.rb +5 -0
- data/motion/core_ext/module/remove_method.rb +12 -0
- data/motion/core_ext/ns_dictionary.rb +11 -0
- data/motion/core_ext/numeric/bytes.rb +44 -0
- data/motion/core_ext/numeric/conversions.rb +7 -0
- data/motion/core_ext/numeric/time.rb +75 -0
- data/motion/core_ext/object/acts_like.rb +10 -0
- data/motion/core_ext/object/blank.rb +105 -0
- data/motion/core_ext/object/deep_dup.rb +44 -0
- data/motion/core_ext/object/duplicable.rb +83 -0
- data/motion/core_ext/object/instance_variables.rb +28 -0
- data/motion/core_ext/object/to_param.rb +58 -0
- data/motion/core_ext/object/to_query.rb +26 -0
- data/motion/core_ext/object/try.rb +78 -0
- data/motion/core_ext/range/include_range.rb +23 -0
- data/motion/core_ext/range/overlaps.rb +8 -0
- data/motion/core_ext/regexp.rb +5 -0
- data/motion/core_ext/string/access.rb +104 -0
- data/motion/core_ext/string/behavior.rb +6 -0
- data/motion/core_ext/string/exclude.rb +11 -0
- data/motion/core_ext/string/filters.rb +55 -0
- data/motion/core_ext/string/indent.rb +43 -0
- data/motion/core_ext/string/inflections.rb +195 -0
- data/motion/core_ext/string/starts_ends_with.rb +4 -0
- data/motion/core_ext/string/strip.rb +24 -0
- data/motion/core_ext/time/acts_like.rb +8 -0
- data/motion/core_ext/time/calculations.rb +215 -0
- data/motion/core_ext/time/conversions.rb +52 -0
- data/motion/duration.rb +104 -0
- data/motion/hash_with_indifferent_access.rb +251 -0
- data/motion/inflections.rb +67 -0
- data/motion/inflector/inflections.rb +203 -0
- data/motion/inflector/methods.rb +321 -0
- data/{lib/motion-support → motion}/logger.rb +0 -0
- data/{lib/motion-support → motion}/version.rb +1 -1
- data/motion-support.gemspec +2 -2
- data/spec/motion-support/_helpers/constantize_test_cases.rb +75 -0
- data/spec/motion-support/_helpers/inflector_test_cases.rb +313 -0
- data/spec/motion-support/core_ext/array/access_spec.rb +29 -0
- data/spec/motion-support/core_ext/array/conversion_spec.rb +60 -0
- data/spec/motion-support/core_ext/array/extract_options_spec.rb +15 -0
- data/spec/motion-support/core_ext/array/grouping_spec.rb +85 -0
- data/spec/motion-support/core_ext/array/prepend_and_append_spec.rb +25 -0
- data/spec/motion-support/core_ext/array/wrap_spec.rb +19 -0
- data/spec/motion-support/{array_spec.rb → core_ext/array_spec.rb} +0 -5
- data/spec/motion-support/core_ext/class/attribute_accessor_spec.rb +127 -0
- data/spec/motion-support/core_ext/class/attribute_spec.rb +92 -0
- data/spec/motion-support/core_ext/date/acts_like_spec.rb +11 -0
- data/spec/motion-support/core_ext/date/calculation_spec.rb +186 -0
- data/spec/motion-support/core_ext/date/conversion_spec.rb +18 -0
- data/spec/motion-support/core_ext/date_and_time/calculation_spec.rb +336 -0
- data/spec/motion-support/core_ext/enumerable_spec.rb +130 -0
- data/spec/motion-support/core_ext/hash/deep_merge_spec.rb +32 -0
- data/spec/motion-support/core_ext/hash/except_spec.rb +43 -0
- data/spec/motion-support/core_ext/hash/key_spec.rb +230 -0
- data/spec/motion-support/core_ext/hash/reverse_merge_spec.rb +26 -0
- data/spec/motion-support/core_ext/hash/slice_spec.rb +61 -0
- data/spec/motion-support/core_ext/integer/inflection_spec.rb +23 -0
- data/spec/motion-support/core_ext/integer/multiple_spec.rb +19 -0
- data/spec/motion-support/{metaclass_spec.rb → core_ext/metaclass_spec.rb} +0 -0
- data/spec/motion-support/core_ext/module/aliasing_spec.rb +143 -0
- data/spec/motion-support/core_ext/module/anonymous_spec.rb +29 -0
- data/spec/motion-support/core_ext/module/attr_internal_spec.rb +104 -0
- data/spec/motion-support/core_ext/module/attribute_accessor_spec.rb +86 -0
- data/spec/motion-support/core_ext/module/delegation_spec.rb +136 -0
- data/spec/motion-support/core_ext/module/introspection_spec.rb +70 -0
- data/spec/motion-support/core_ext/module/reachable_spec.rb +61 -0
- data/spec/motion-support/core_ext/module/remove_method_spec.rb +25 -0
- data/spec/motion-support/core_ext/numeric/bytes_spec.rb +43 -0
- data/spec/motion-support/core_ext/object/acts_like_spec.rb +21 -0
- data/spec/motion-support/core_ext/object/blank_spec.rb +54 -0
- data/spec/motion-support/core_ext/object/deep_dup_spec.rb +54 -0
- data/spec/motion-support/core_ext/object/duplicable_spec.rb +31 -0
- data/spec/motion-support/core_ext/object/instance_variable_spec.rb +19 -0
- data/spec/motion-support/core_ext/object/to_param_spec.rb +75 -0
- data/spec/motion-support/core_ext/object/to_query_spec.rb +37 -0
- data/spec/motion-support/core_ext/object/try_spec.rb +92 -0
- data/spec/motion-support/core_ext/range/include_range_spec.rb +31 -0
- data/spec/motion-support/core_ext/range/overlap_spec.rb +43 -0
- data/spec/motion-support/core_ext/regexp_spec.rb +7 -0
- data/spec/motion-support/core_ext/string/access_spec.rb +53 -0
- data/spec/motion-support/core_ext/string/behavior_spec.rb +7 -0
- data/spec/motion-support/core_ext/string/exclude_spec.rb +8 -0
- data/spec/motion-support/core_ext/string/filter_spec.rb +48 -0
- data/spec/motion-support/core_ext/string/indent_spec.rb +56 -0
- data/spec/motion-support/core_ext/string/inflection_spec.rb +142 -0
- data/spec/motion-support/core_ext/string/starts_end_with_spec.rb +14 -0
- data/spec/motion-support/core_ext/string/strip_spec.rb +34 -0
- data/spec/motion-support/core_ext/string_spec.rb +88 -0
- data/spec/motion-support/core_ext/time/acts_like_spec.rb +11 -0
- data/spec/motion-support/core_ext/time/calculation_spec.rb +201 -0
- data/spec/motion-support/core_ext/time/conversion_spec.rb +54 -0
- data/spec/motion-support/duration_spec.rb +107 -0
- data/spec/motion-support/hash_with_indifferent_access_spec.rb +605 -0
- data/spec/motion-support/inflector_spec.rb +474 -35
- data/spec/motion-support/ns_dictionary_spec.rb +29 -0
- metadata +212 -35
- data/lib/motion-support/cattr_accessor.rb +0 -19
- data/lib/motion-support/class_inheritable_accessor.rb +0 -23
- data/lib/motion-support/class_inheritable_array.rb +0 -29
- data/lib/motion-support/hash.rb +0 -31
- data/lib/motion-support/nilclass.rb +0 -5
- data/lib/motion-support/object.rb +0 -17
- data/lib/motion-support/string.rb +0 -71
- data/spec/motion-support/cattr_accessor_spec.rb +0 -49
- data/spec/motion-support/class_inheritable_accessor_spec.rb +0 -49
- data/spec/motion-support/class_inheritable_array_spec.rb +0 -61
- data/spec/motion-support/hash_spec.rb +0 -31
- data/spec/motion-support/nilclass_spec.rb +0 -5
- data/spec/motion-support/object_spec.rb +0 -43
- data/spec/motion-support/string_spec.rb +0 -145
@@ -1,65 +1,504 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
1
|
+
module InflectorHelper
|
2
|
+
# Dups the singleton and yields, restoring the original inflections later.
|
3
|
+
# Use this in tests what modify the state of the singleton.
|
4
|
+
#
|
5
|
+
# This helper is implemented by setting @__instance__ because in some tests
|
6
|
+
# there are module functions that access MotionSupport::Inflector.inflections,
|
7
|
+
# so we need to replace the singleton itself.
|
8
|
+
def with_dup
|
9
|
+
original = MotionSupport::Inflector::Inflections.instance_variable_get(:@__instance__)
|
10
|
+
MotionSupport::Inflector::Inflections.instance_variable_set(:@__instance__, original.dup)
|
11
|
+
yield
|
12
|
+
ensure
|
13
|
+
MotionSupport::Inflector::Inflections.instance_variable_set(:@__instance__, original)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
describe "Inflector" do
|
18
|
+
describe "singularize/pluralize" do
|
19
|
+
extend InflectorHelper
|
20
|
+
|
21
|
+
it "should pluralize plurals" do
|
22
|
+
MotionSupport::Inflector.pluralize("plurals").should == "plurals"
|
23
|
+
MotionSupport::Inflector.pluralize("Plurals").should == "Plurals"
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should pluralize empty string" do
|
27
|
+
MotionSupport::Inflector.pluralize("").should == ""
|
28
|
+
end
|
29
|
+
|
30
|
+
MotionSupport::Inflector.inflections.uncountable.each do |word|
|
31
|
+
it "should treat #{word} as uncountable" do
|
32
|
+
MotionSupport::Inflector.singularize(word).should == word
|
33
|
+
MotionSupport::Inflector.pluralize(word).should == word
|
34
|
+
MotionSupport::Inflector.singularize(word).should == MotionSupport::Inflector.pluralize(word)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
InflectorTestCases::SingularToPlural.each do |singular, plural|
|
39
|
+
it "should pluralize singular #{singular}" do
|
40
|
+
MotionSupport::Inflector.pluralize(singular).should == plural
|
41
|
+
MotionSupport::Inflector.pluralize(singular.capitalize).should == plural.capitalize
|
42
|
+
end
|
43
|
+
|
44
|
+
it "should singularize plural #{plural}" do
|
45
|
+
MotionSupport::Inflector.singularize(plural).should == singular
|
46
|
+
MotionSupport::Inflector.singularize(plural.capitalize).should == singular.capitalize
|
47
|
+
end
|
48
|
+
|
49
|
+
it "should pluralize plural #{plural}" do
|
50
|
+
MotionSupport::Inflector.pluralize(plural).should == plural
|
51
|
+
MotionSupport::Inflector.pluralize(plural.capitalize).should == plural.capitalize
|
52
|
+
end
|
53
|
+
|
54
|
+
it "should singularize singular #{singular}" do
|
55
|
+
MotionSupport::Inflector.singularize(singular).should == singular
|
56
|
+
MotionSupport::Inflector.singularize(singular.capitalize).should == singular.capitalize
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
it "should handle uncountable words non-greedily" do
|
61
|
+
with_dup do
|
62
|
+
uncountable_word = "ors"
|
63
|
+
countable_word = "sponsor"
|
64
|
+
|
65
|
+
MotionSupport::Inflector.inflections.uncountable << uncountable_word
|
66
|
+
|
67
|
+
MotionSupport::Inflector.singularize(uncountable_word).should == uncountable_word
|
68
|
+
MotionSupport::Inflector.pluralize(uncountable_word).should == uncountable_word
|
69
|
+
MotionSupport::Inflector.pluralize(uncountable_word).should == MotionSupport::Inflector.singularize(uncountable_word)
|
70
|
+
|
71
|
+
MotionSupport::Inflector.singularize(countable_word).should == "sponsor"
|
72
|
+
MotionSupport::Inflector.pluralize(countable_word).should == "sponsors"
|
73
|
+
MotionSupport::Inflector.singularize(MotionSupport::Inflector.pluralize(countable_word)).should == "sponsor"
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
describe "titleize" do
|
79
|
+
InflectorTestCases::MixtureToTitleCase.each do |before, titleized|
|
80
|
+
it "should titleize #{before}" do
|
81
|
+
MotionSupport::Inflector.titleize(before).should == titleized
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
describe "underscore" do
|
87
|
+
InflectorTestCases::CamelToUnderscore.each do |camel, underscore|
|
88
|
+
it "should underscore #{camel}" do
|
89
|
+
MotionSupport::Inflector.underscore(camel).should == underscore
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
InflectorTestCases::CamelToUnderscoreWithoutReverse.each do |camel, underscore|
|
94
|
+
it "should underscore #{camel}" do
|
95
|
+
MotionSupport::Inflector.underscore(camel).should == underscore
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
InflectorTestCases::CamelWithModuleToUnderscoreWithSlash.each do |camel, underscore|
|
100
|
+
it "should underscore #{camel}" do
|
101
|
+
MotionSupport::Inflector.underscore(camel).should == underscore
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
describe "camelize" do
|
107
|
+
InflectorTestCases::CamelToUnderscore.each do |camel, underscore|
|
108
|
+
it "should camelize #{underscore}" do
|
109
|
+
MotionSupport::Inflector.camelize(underscore).should == camel
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
InflectorTestCases::CamelWithModuleToUnderscoreWithSlash.each do |camel, underscore|
|
114
|
+
it "should camelize #{underscore}" do
|
115
|
+
MotionSupport::Inflector.camelize(underscore).should == camel
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
it "should downcase first letter if called with lower" do
|
120
|
+
MotionSupport::Inflector.camelize('Capital', false).should == 'capital'
|
121
|
+
end
|
122
|
+
|
123
|
+
it "should remove underscores" do
|
124
|
+
MotionSupport::Inflector.camelize('Camel_Case').should == "CamelCase"
|
125
|
+
end
|
126
|
+
|
127
|
+
InflectorTestCases::UnderscoreToLowerCamel.each do |underscored, lower_camel|
|
128
|
+
it "should lower-camelize #{underscored}" do
|
129
|
+
MotionSupport::Inflector.camelize(underscored, false).should == lower_camel
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
InflectorTestCases::SymbolToLowerCamel.each do |symbol, lower_camel|
|
134
|
+
it "should lower-camelize symbol :#{symbol}" do
|
135
|
+
MotionSupport::Inflector.camelize(symbol, false).should == lower_camel
|
136
|
+
end
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
describe "irregularities" do
|
141
|
+
InflectorTestCases::Irregularities.each do |irregularity|
|
142
|
+
singular, plural = *irregularity
|
143
|
+
MotionSupport::Inflector.inflections do |inflect|
|
144
|
+
it "should singularize #{plural} as #{singular}" do
|
145
|
+
inflect.irregular(singular, plural)
|
146
|
+
MotionSupport::Inflector.singularize(plural).should == singular
|
147
|
+
end
|
148
|
+
|
149
|
+
it "should pluralize #{singular} as #{plural}" do
|
150
|
+
inflect.irregular(singular, plural)
|
151
|
+
MotionSupport::Inflector.pluralize(singular).should == plural
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
MotionSupport::Inflector.inflections do |inflect|
|
156
|
+
it "should return same string when pluralizing irregular plural #{plural}" do
|
157
|
+
inflect.irregular(singular, plural)
|
158
|
+
MotionSupport::Inflector.pluralize(plural).should == plural
|
159
|
+
end
|
160
|
+
end
|
161
|
+
|
162
|
+
MotionSupport::Inflector.inflections do |inflect|
|
163
|
+
it "should return same string when singularizing irregular singular #{singular}" do
|
164
|
+
inflect.irregular(singular, plural)
|
165
|
+
MotionSupport::Inflector.singularize(singular).should == singular
|
166
|
+
end
|
167
|
+
end
|
168
|
+
end
|
169
|
+
end
|
170
|
+
|
171
|
+
it "should overwrite previous inflectors" do
|
172
|
+
MotionSupport::Inflector.singularize("series").should == "series"
|
173
|
+
MotionSupport::Inflector.inflections.singular "series", "serie"
|
174
|
+
MotionSupport::Inflector.singularize("series").should == "serie"
|
175
|
+
MotionSupport::Inflector.inflections.uncountable "series" # Return to normal
|
176
|
+
end
|
177
|
+
|
178
|
+
describe "acronym" do
|
179
|
+
before do
|
180
|
+
MotionSupport::Inflector.inflections do |inflect|
|
181
|
+
inflect.acronym("API")
|
182
|
+
inflect.acronym("HTML")
|
183
|
+
inflect.acronym("HTTP")
|
184
|
+
inflect.acronym("RESTful")
|
185
|
+
inflect.acronym("W3C")
|
186
|
+
inflect.acronym("PhD")
|
187
|
+
inflect.acronym("RoR")
|
188
|
+
inflect.acronym("SSL")
|
189
|
+
end
|
5
190
|
end
|
6
191
|
|
7
|
-
|
8
|
-
|
192
|
+
# camelize underscore humanize titleize
|
193
|
+
[
|
194
|
+
["API", "api", "API", "API"],
|
195
|
+
["APIController", "api_controller", "API controller", "API Controller"],
|
196
|
+
["Nokogiri::HTML", "nokogiri/html", "Nokogiri/HTML", "Nokogiri/HTML"],
|
197
|
+
["HTTPAPI", "http_api", "HTTP API", "HTTP API"],
|
198
|
+
["HTTP::Get", "http/get", "HTTP/get", "HTTP/Get"],
|
199
|
+
["SSLError", "ssl_error", "SSL error", "SSL Error"],
|
200
|
+
["RESTful", "restful", "RESTful", "RESTful"],
|
201
|
+
["RESTfulController", "restful_controller", "RESTful controller", "RESTful Controller"],
|
202
|
+
["IHeartW3C", "i_heart_w3c", "I heart W3C", "I Heart W3C"],
|
203
|
+
["PhDRequired", "phd_required", "PhD required", "PhD Required"],
|
204
|
+
["IRoRU", "i_ror_u", "I RoR u", "I RoR U"],
|
205
|
+
["RESTfulHTTPAPI", "restful_http_api", "RESTful HTTP API", "RESTful HTTP API"],
|
206
|
+
["UIImage", "ui_image", "UI image", "UI Image"],
|
207
|
+
|
208
|
+
# misdirection
|
209
|
+
["Capistrano", "capistrano", "Capistrano", "Capistrano"],
|
210
|
+
["CapiController", "capi_controller", "Capi controller", "Capi Controller"],
|
211
|
+
["HttpsApis", "https_apis", "Https apis", "Https Apis"],
|
212
|
+
["Html5", "html5", "Html5", "Html5"],
|
213
|
+
["Restfully", "restfully", "Restfully", "Restfully"],
|
214
|
+
["RoRails", "ro_rails", "Ro rails", "Ro Rails"]
|
215
|
+
].each do |camel, under, human, title|
|
216
|
+
it "should camelize #{under} as #{camel}" do
|
217
|
+
MotionSupport::Inflector.camelize(under).should == camel
|
218
|
+
end
|
219
|
+
|
220
|
+
it "should keep #{camel} camelized" do
|
221
|
+
MotionSupport::Inflector.camelize(camel).should == camel
|
222
|
+
end
|
223
|
+
|
224
|
+
it "should keep #{under} underscored" do
|
225
|
+
MotionSupport::Inflector.underscore(under).should == under
|
226
|
+
end
|
227
|
+
|
228
|
+
it "should underscore #{camel} as #{under}" do
|
229
|
+
MotionSupport::Inflector.underscore(camel).should == under
|
230
|
+
end
|
231
|
+
|
232
|
+
it "should titleize #{under} as #{title}" do
|
233
|
+
MotionSupport::Inflector.titleize(under).should == title
|
234
|
+
end
|
235
|
+
|
236
|
+
it "should titleize #{camel} as #{title}" do
|
237
|
+
MotionSupport::Inflector.titleize(camel).should == title
|
238
|
+
end
|
239
|
+
|
240
|
+
it "should humanize #{under} as #{human}" do
|
241
|
+
MotionSupport::Inflector.humanize(under).should == human
|
242
|
+
end
|
9
243
|
end
|
10
244
|
|
11
|
-
|
12
|
-
|
245
|
+
describe "override acronyms" do
|
246
|
+
before do
|
247
|
+
MotionSupport::Inflector.inflections do |inflect|
|
248
|
+
inflect.acronym("API")
|
249
|
+
inflect.acronym("LegacyApi")
|
250
|
+
end
|
251
|
+
end
|
252
|
+
|
253
|
+
{
|
254
|
+
"legacyapi" => "LegacyApi",
|
255
|
+
"legacy_api" => "LegacyAPI",
|
256
|
+
"some_legacyapi" => "SomeLegacyApi",
|
257
|
+
"nonlegacyapi" => "Nonlegacyapi"
|
258
|
+
}.each do |from, to|
|
259
|
+
it "should camelize #{from} as #{to}" do
|
260
|
+
MotionSupport::Inflector.camelize(from).should == to
|
261
|
+
end
|
262
|
+
end
|
13
263
|
end
|
14
264
|
|
15
|
-
|
16
|
-
|
265
|
+
describe "lower-camelize with acronym parts" do
|
266
|
+
before do
|
267
|
+
MotionSupport::Inflector.inflections do |inflect|
|
268
|
+
inflect.acronym("API")
|
269
|
+
inflect.acronym("HTML")
|
270
|
+
end
|
271
|
+
end
|
272
|
+
|
273
|
+
{
|
274
|
+
"html_api" => "htmlAPI",
|
275
|
+
"htmlAPI" => "htmlAPI",
|
276
|
+
"HTMLAPI" => "htmlAPI"
|
277
|
+
}.each do |from, to|
|
278
|
+
it "should lower-camelize #{from} as #{to}" do
|
279
|
+
MotionSupport::Inflector.camelize(from, false).should == to
|
280
|
+
end
|
281
|
+
end
|
17
282
|
end
|
18
283
|
|
19
|
-
it "
|
20
|
-
Inflector.inflections
|
284
|
+
it "should underscore acronym sequence" do
|
285
|
+
MotionSupport::Inflector.inflections do |inflect|
|
286
|
+
inflect.acronym("API")
|
287
|
+
inflect.acronym("JSON")
|
288
|
+
inflect.acronym("HTML")
|
289
|
+
end
|
290
|
+
|
291
|
+
MotionSupport::Inflector.underscore("JSONHTMLAPI").should == "json_html_api"
|
292
|
+
end
|
293
|
+
end
|
294
|
+
|
295
|
+
describe "demodulize" do
|
296
|
+
{
|
297
|
+
"MyApplication::Billing::Account" => "Account",
|
298
|
+
"Account" => "Account",
|
299
|
+
"" => ""
|
300
|
+
}.each do |from, to|
|
301
|
+
it "should transform #{from} to #{to}" do
|
302
|
+
MotionSupport::Inflector.demodulize(from).should == to
|
303
|
+
end
|
21
304
|
end
|
22
305
|
end
|
23
306
|
|
24
|
-
describe
|
25
|
-
|
26
|
-
|
307
|
+
describe "deconstantize" do
|
308
|
+
{
|
309
|
+
"MyApplication::Billing::Account" => "MyApplication::Billing",
|
310
|
+
"::MyApplication::Billing::Account" => "::MyApplication::Billing",
|
311
|
+
"MyApplication::Billing" => "MyApplication",
|
312
|
+
"::MyApplication::Billing" => "::MyApplication",
|
313
|
+
"Account" => "",
|
314
|
+
"::Account" => "",
|
315
|
+
"" => ""
|
316
|
+
}.each do |from, to|
|
317
|
+
it "should deconstantize #{from} as #{to}" do
|
318
|
+
MotionSupport::Inflector.deconstantize(from).should == to
|
319
|
+
end
|
320
|
+
end
|
321
|
+
end
|
322
|
+
|
323
|
+
describe "foreign_key" do
|
324
|
+
InflectorTestCases::ClassNameToForeignKeyWithUnderscore.each do |klass, foreign_key|
|
325
|
+
it "should create foreign key for class name #{klass}" do
|
326
|
+
MotionSupport::Inflector.foreign_key(klass).should == foreign_key
|
327
|
+
end
|
27
328
|
end
|
28
329
|
|
29
|
-
|
30
|
-
|
330
|
+
InflectorTestCases::ClassNameToForeignKeyWithoutUnderscore.each do |klass, foreign_key|
|
331
|
+
it "should create foreign key for class name #{klass} without underscore" do
|
332
|
+
MotionSupport::Inflector.foreign_key(klass, false).should == foreign_key
|
333
|
+
end
|
31
334
|
end
|
335
|
+
end
|
336
|
+
|
337
|
+
describe "tableize" do
|
338
|
+
InflectorTestCases::ClassNameToTableName.each do |class_name, table_name|
|
339
|
+
it "should create table name from class name #{class_name}" do
|
340
|
+
MotionSupport::Inflector.tableize(class_name).should == table_name
|
341
|
+
end
|
342
|
+
end
|
343
|
+
end
|
32
344
|
|
33
|
-
|
34
|
-
|
345
|
+
describe "classify" do
|
346
|
+
InflectorTestCases::ClassNameToTableName.each do |class_name, table_name|
|
347
|
+
it "should classify #{table_name}" do
|
348
|
+
MotionSupport::Inflector.classify(table_name).should == class_name
|
349
|
+
end
|
350
|
+
|
351
|
+
it "should classify #{table_name} with table prefix" do
|
352
|
+
MotionSupport::Inflector.classify("table_prefix." + table_name).should == class_name
|
353
|
+
end
|
35
354
|
end
|
36
355
|
|
37
|
-
it "
|
38
|
-
|
356
|
+
it "should classify with symbol" do
|
357
|
+
lambda do
|
358
|
+
MotionSupport::Inflector.classify(:foo_bars).should == 'FooBar'
|
359
|
+
end.should.not.raise
|
39
360
|
end
|
40
361
|
|
41
|
-
it "
|
42
|
-
Inflector.
|
362
|
+
it "should classify with leading schema name" do
|
363
|
+
MotionSupport::Inflector.classify('schema.foo_bar').should == 'FooBar'
|
43
364
|
end
|
44
365
|
end
|
366
|
+
|
367
|
+
describe "humanize" do
|
368
|
+
InflectorTestCases::UnderscoreToHuman.each do |underscore, human|
|
369
|
+
it "should humanize #{underscore}" do
|
370
|
+
MotionSupport::Inflector.humanize(underscore).should == human
|
371
|
+
end
|
372
|
+
end
|
45
373
|
|
46
|
-
|
47
|
-
|
48
|
-
|
374
|
+
it "should humanize by rule" do
|
375
|
+
MotionSupport::Inflector.inflections do |inflect|
|
376
|
+
inflect.human(/_cnt$/i, '\1_count')
|
377
|
+
inflect.human(/^prefx_/i, '\1')
|
378
|
+
end
|
379
|
+
MotionSupport::Inflector.humanize("jargon_cnt").should == "Jargon count"
|
380
|
+
MotionSupport::Inflector.humanize("prefx_request").should == "Request"
|
49
381
|
end
|
50
382
|
|
51
|
-
it "
|
52
|
-
Inflector.inflections
|
383
|
+
it "should humanize by string" do
|
384
|
+
MotionSupport::Inflector.inflections do |inflect|
|
385
|
+
inflect.human("col_rpted_bugs", "Reported bugs")
|
386
|
+
end
|
387
|
+
MotionSupport::Inflector.humanize("col_rpted_bugs").should == "Reported bugs"
|
388
|
+
MotionSupport::Inflector.humanize("COL_rpted_bugs").should == "Col rpted bugs"
|
53
389
|
end
|
54
390
|
end
|
391
|
+
|
392
|
+
describe "constantize" do
|
393
|
+
extend ConstantizeTestCases
|
394
|
+
|
395
|
+
it "should constantize" do
|
396
|
+
run_constantize_tests_on do |string|
|
397
|
+
MotionSupport::Inflector.constantize(string)
|
398
|
+
end
|
399
|
+
end
|
400
|
+
end
|
401
|
+
|
402
|
+
describe "safe_constantize" do
|
403
|
+
extend ConstantizeTestCases
|
404
|
+
|
405
|
+
it "should safe_constantize" do
|
406
|
+
run_safe_constantize_tests_on do |string|
|
407
|
+
MotionSupport::Inflector.safe_constantize(string)
|
408
|
+
end
|
409
|
+
end
|
410
|
+
end
|
411
|
+
|
412
|
+
describe "ordinal" do
|
413
|
+
InflectorTestCases::OrdinalNumbers.each do |number, ordinalized|
|
414
|
+
it "should return ordinal of number #{number}" do
|
415
|
+
(number + MotionSupport::Inflector.ordinal(number)).should == ordinalized
|
416
|
+
end
|
417
|
+
end
|
418
|
+
end
|
419
|
+
|
420
|
+
describe "ordinalize" do
|
421
|
+
InflectorTestCases::OrdinalNumbers.each do |number, ordinalized|
|
422
|
+
it "should ordinalize number #{number}" do
|
423
|
+
MotionSupport::Inflector.ordinalize(number).should == ordinalized
|
424
|
+
end
|
425
|
+
end
|
426
|
+
end
|
427
|
+
|
428
|
+
describe "dasherize" do
|
429
|
+
InflectorTestCases::UnderscoresToDashes.each do |underscored, dasherized|
|
430
|
+
it "should dasherize #{underscored}" do
|
431
|
+
MotionSupport::Inflector.dasherize(underscored).should == dasherized
|
432
|
+
end
|
433
|
+
end
|
55
434
|
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
Inflector.inflections.pluralize('foot').should == 'feet'
|
61
|
-
Inflector.inflections.singularize('feet').should == 'foot'
|
435
|
+
InflectorTestCases::UnderscoresToDashes.each_key do |underscored|
|
436
|
+
it "should dasherize and then underscore #{underscored}, returning #{underscored}" do
|
437
|
+
MotionSupport::Inflector.underscore(MotionSupport::Inflector.dasherize(underscored)).should == underscored
|
438
|
+
end
|
62
439
|
end
|
63
440
|
end
|
64
|
-
|
441
|
+
|
442
|
+
describe "clear" do
|
443
|
+
extend InflectorHelper
|
444
|
+
|
445
|
+
%w{plurals singulars uncountables humans acronyms}.each do |inflection_type|
|
446
|
+
it "should clear #{inflection_type}" do
|
447
|
+
with_dup do
|
448
|
+
MotionSupport::Inflector.inflections.clear inflection_type.to_sym
|
449
|
+
MotionSupport::Inflector.inflections.send(inflection_type).should.be.empty
|
450
|
+
end
|
451
|
+
end
|
452
|
+
end
|
65
453
|
|
454
|
+
it "should clear all" do
|
455
|
+
with_dup do
|
456
|
+
MotionSupport::Inflector.inflections do |inflect|
|
457
|
+
# ensure any data is present
|
458
|
+
inflect.plural(/(quiz)$/i, '\1zes')
|
459
|
+
inflect.singular(/(database)s$/i, '\1')
|
460
|
+
inflect.uncountable('series')
|
461
|
+
inflect.human("col_rpted_bugs", "Reported bugs")
|
462
|
+
|
463
|
+
inflect.clear :all
|
464
|
+
|
465
|
+
inflect.plurals.should.be.empty
|
466
|
+
inflect.singulars.should.be.empty
|
467
|
+
inflect.uncountables.should.be.empty
|
468
|
+
inflect.humans.should.be.empty
|
469
|
+
end
|
470
|
+
end
|
471
|
+
end
|
472
|
+
|
473
|
+
it "should clear with default" do
|
474
|
+
with_dup do
|
475
|
+
MotionSupport::Inflector.inflections do |inflect|
|
476
|
+
# ensure any data is present
|
477
|
+
inflect.plural(/(quiz)$/i, '\1zes')
|
478
|
+
inflect.singular(/(database)s$/i, '\1')
|
479
|
+
inflect.uncountable('series')
|
480
|
+
inflect.human("col_rpted_bugs", "Reported bugs")
|
481
|
+
|
482
|
+
inflect.clear
|
483
|
+
|
484
|
+
inflect.plurals.should.be.empty
|
485
|
+
inflect.singulars.should.be.empty
|
486
|
+
inflect.uncountables.should.be.empty
|
487
|
+
inflect.humans.should.be.empty
|
488
|
+
end
|
489
|
+
end
|
490
|
+
end
|
491
|
+
|
492
|
+
%w(plurals singulars uncountables humans acronyms).each do |scope|
|
493
|
+
MotionSupport::Inflector.inflections do |inflect|
|
494
|
+
it "should clear inflections with #{scope}" do
|
495
|
+
with_dup do
|
496
|
+
# clear the inflections
|
497
|
+
inflect.clear(scope)
|
498
|
+
inflect.send(scope).should == []
|
499
|
+
end
|
500
|
+
end
|
501
|
+
end
|
502
|
+
end
|
503
|
+
end
|
504
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
describe "NSDictionary" do
|
2
|
+
describe "to_hash" do
|
3
|
+
before do
|
4
|
+
dict = NSMutableDictionary.alloc.init
|
5
|
+
dict.setValue('bar', forKey:'foo')
|
6
|
+
@hash = dict.to_hash
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should convert NSDictionary to Hash" do
|
10
|
+
@hash.class.should == Hash
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should preserve all keys" do
|
14
|
+
@hash.keys.should == ['foo']
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should preserve all values" do
|
18
|
+
@hash.values.should == ['bar']
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
describe "symbolize_keys" do
|
23
|
+
it "should work for NSDictionary instances" do
|
24
|
+
dict = NSMutableDictionary.alloc.init
|
25
|
+
dict.setValue('bar', forKey:'foo')
|
26
|
+
dict.symbolize_keys.should == { :foo => 'bar' }
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|