merb-core 0.9.3 → 0.9.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (178) hide show
  1. data/LICENSE +1 -1
  2. data/README +3 -3
  3. data/Rakefile +144 -33
  4. data/bin/merb +0 -0
  5. data/bin/merb-specs +0 -0
  6. data/docs/bootloading.dox +1 -0
  7. data/docs/merb-core-call-stack-diagram.mmap +0 -0
  8. data/docs/merb-core-call-stack-diagram.pdf +0 -0
  9. data/docs/merb-core-call-stack-diagram.png +0 -0
  10. data/lib/merb-core.rb +159 -37
  11. data/lib/merb-core/autoload.rb +1 -0
  12. data/lib/merb-core/bootloader.rb +208 -92
  13. data/lib/merb-core/config.rb +20 -6
  14. data/lib/merb-core/controller/abstract_controller.rb +113 -61
  15. data/lib/merb-core/controller/exceptions.rb +28 -13
  16. data/lib/merb-core/controller/merb_controller.rb +73 -44
  17. data/lib/merb-core/controller/mime.rb +25 -7
  18. data/lib/merb-core/controller/mixins/authentication.rb +1 -1
  19. data/lib/merb-core/controller/mixins/controller.rb +44 -8
  20. data/lib/merb-core/controller/mixins/render.rb +191 -128
  21. data/lib/merb-core/controller/mixins/responder.rb +65 -63
  22. data/lib/merb-core/controller/template.rb +103 -54
  23. data/lib/merb-core/core_ext.rb +7 -12
  24. data/lib/merb-core/core_ext/kernel.rb +128 -136
  25. data/lib/merb-core/dispatch/cookies.rb +26 -4
  26. data/lib/merb-core/dispatch/default_exception/default_exception.rb +93 -0
  27. data/lib/merb-core/dispatch/default_exception/views/_css.html.erb +198 -0
  28. data/lib/merb-core/dispatch/default_exception/views/_javascript.html.erb +73 -0
  29. data/lib/merb-core/dispatch/default_exception/views/index.html.erb +92 -0
  30. data/lib/merb-core/dispatch/dispatcher.rb +156 -224
  31. data/lib/merb-core/dispatch/request.rb +126 -25
  32. data/lib/merb-core/dispatch/router.rb +61 -6
  33. data/lib/merb-core/dispatch/router/behavior.rb +122 -41
  34. data/lib/merb-core/dispatch/router/route.rb +147 -22
  35. data/lib/merb-core/dispatch/session.rb +52 -2
  36. data/lib/merb-core/dispatch/session/cookie.rb +4 -2
  37. data/lib/merb-core/dispatch/session/memcached.rb +38 -27
  38. data/lib/merb-core/dispatch/session/memory.rb +18 -11
  39. data/lib/merb-core/dispatch/worker.rb +28 -0
  40. data/lib/merb-core/gem_ext/erubis.rb +58 -0
  41. data/lib/merb-core/logger.rb +3 -31
  42. data/lib/merb-core/plugins.rb +25 -3
  43. data/lib/merb-core/rack.rb +18 -12
  44. data/lib/merb-core/rack/adapter.rb +10 -8
  45. data/lib/merb-core/rack/adapter/ebb.rb +2 -2
  46. data/lib/merb-core/rack/adapter/irb.rb +31 -21
  47. data/lib/merb-core/rack/adapter/swiftiplied_mongrel.rb +26 -0
  48. data/lib/merb-core/rack/adapter/thin.rb +19 -9
  49. data/lib/merb-core/rack/adapter/thin_turbo.rb +24 -0
  50. data/lib/merb-core/rack/application.rb +9 -84
  51. data/lib/merb-core/rack/middleware.rb +26 -0
  52. data/lib/merb-core/rack/middleware/path_prefix.rb +31 -0
  53. data/lib/merb-core/rack/middleware/profiler.rb +19 -0
  54. data/lib/merb-core/rack/middleware/static.rb +45 -0
  55. data/lib/merb-core/server.rb +27 -9
  56. data/lib/merb-core/tasks/audit.rake +68 -0
  57. data/lib/merb-core/tasks/merb.rb +1 -0
  58. data/lib/merb-core/tasks/merb_rake_helper.rb +12 -0
  59. data/lib/merb-core/tasks/stats.rake +71 -0
  60. data/lib/merb-core/test.rb +2 -1
  61. data/lib/merb-core/test/helpers/multipart_request_helper.rb +3 -3
  62. data/lib/merb-core/test/helpers/request_helper.rb +66 -24
  63. data/lib/merb-core/test/matchers/controller_matchers.rb +36 -4
  64. data/lib/merb-core/test/matchers/route_matchers.rb +12 -3
  65. data/lib/merb-core/test/matchers/view_matchers.rb +3 -3
  66. data/lib/merb-core/test/run_specs.rb +1 -0
  67. data/lib/merb-core/test/tasks/spectasks.rb +13 -5
  68. data/lib/merb-core/test/test_ext/string.rb +14 -0
  69. data/lib/merb-core/vendor/facets/dictionary.rb +3 -3
  70. data/lib/merb-core/vendor/facets/inflect.rb +82 -37
  71. data/lib/merb-core/version.rb +2 -2
  72. data/spec/private/config/config_spec.rb +39 -4
  73. data/spec/private/core_ext/kernel_spec.rb +3 -14
  74. data/spec/private/dispatch/bootloader_spec.rb +1 -1
  75. data/spec/private/dispatch/cookies_spec.rb +181 -69
  76. data/spec/private/dispatch/fixture/app/controllers/exceptions.rb +0 -2
  77. data/spec/private/dispatch/fixture/app/controllers/foo.rb +0 -2
  78. data/spec/private/dispatch/fixture/config/rack.rb +10 -0
  79. data/spec/private/dispatch/fixture/log/merb_test.log +7054 -1802
  80. data/spec/private/dispatch/route_params_spec.rb +2 -3
  81. data/spec/private/dispatch/session_mixin_spec.rb +47 -0
  82. data/spec/private/plugins/plugin_spec.rb +73 -59
  83. data/spec/private/router/behavior_spec.rb +60 -0
  84. data/spec/private/router/fixture/log/merb_test.log +1693 -0
  85. data/spec/private/router/route_spec.rb +414 -0
  86. data/spec/private/router/router_spec.rb +175 -0
  87. data/spec/private/vendor/facets/plural_spec.rb +564 -0
  88. data/spec/private/vendor/facets/singular_spec.rb +489 -0
  89. data/spec/public/abstract_controller/controllers/cousins.rb +41 -0
  90. data/spec/public/abstract_controller/controllers/helpers.rb +12 -2
  91. data/spec/public/abstract_controller/controllers/partial.rb +17 -2
  92. data/spec/public/abstract_controller/controllers/render.rb +16 -1
  93. data/spec/public/abstract_controller/controllers/views/helpers/capture_eq/index.erb +1 -0
  94. data/spec/public/abstract_controller/controllers/views/helpers/capture_with_args/index.erb +1 -0
  95. data/spec/public/abstract_controller/controllers/views/merb/test/fixtures/abstract/render_two_throw_contents/index.erb +1 -0
  96. data/spec/public/abstract_controller/controllers/views/partial/partial_with_collections_and_counter/_collection.erb +1 -0
  97. data/spec/public/abstract_controller/controllers/views/partial/partial_with_collections_and_counter/index.erb +1 -0
  98. data/spec/public/abstract_controller/controllers/views/partial/with_absolute_partial/_partial.erb +1 -0
  99. data/spec/public/abstract_controller/controllers/views/partial/with_absolute_partial/index.erb +1 -0
  100. data/spec/public/abstract_controller/filter_spec.rb +20 -1
  101. data/spec/public/abstract_controller/helper_spec.rb +10 -2
  102. data/spec/public/abstract_controller/partial_spec.rb +8 -0
  103. data/spec/public/abstract_controller/render_spec.rb +8 -0
  104. data/spec/public/abstract_controller/spec_helper.rb +7 -3
  105. data/spec/public/boot_loader/boot_loader_spec.rb +2 -2
  106. data/spec/public/controller/base_spec.rb +10 -2
  107. data/spec/public/controller/config/init.rb +6 -0
  108. data/spec/public/controller/controllers/authentication.rb +9 -11
  109. data/spec/public/controller/controllers/base.rb +2 -8
  110. data/spec/public/controller/controllers/cookies.rb +16 -0
  111. data/spec/public/controller/controllers/dispatcher.rb +35 -0
  112. data/spec/public/controller/controllers/display.rb +62 -14
  113. data/spec/public/controller/controllers/redirect.rb +36 -0
  114. data/spec/public/controller/controllers/responder.rb +37 -11
  115. data/spec/public/controller/controllers/views/layout/custom_arg.json.erb +1 -0
  116. data/spec/public/controller/controllers/views/merb/test/fixtures/controllers/class_and_local_provides/index.html.erb +1 -0
  117. data/spec/public/controller/controllers/views/merb/test/fixtures/controllers/class_and_local_provides/index.xml.erb +1 -0
  118. data/spec/public/controller/controllers/views/merb/test/fixtures/controllers/display_with_template/no_layout.html.erb +1 -0
  119. data/spec/public/controller/cookies_spec.rb +23 -0
  120. data/spec/public/controller/dispatcher_spec.rb +411 -0
  121. data/spec/public/controller/display_spec.rb +43 -10
  122. data/spec/public/controller/redirect_spec.rb +33 -0
  123. data/spec/public/controller/responder_spec.rb +79 -11
  124. data/spec/public/controller/spec_helper.rb +3 -1
  125. data/spec/public/controller/url_spec.rb +10 -0
  126. data/spec/public/core/merb_core_spec.rb +11 -0
  127. data/spec/public/core_ext/fixtures/core_ext_dependency.rb +2 -0
  128. data/spec/public/core_ext/kernel_spec.rb +9 -0
  129. data/spec/public/core_ext/spec_helper.rb +1 -0
  130. data/spec/public/directory_structure/directory/log/merb_test.log +3729 -272
  131. data/spec/public/directory_structure/directory_spec.rb +3 -4
  132. data/spec/public/logger/logger_spec.rb +4 -4
  133. data/spec/public/reloading/directory/log/merb_test.log +288066 -15
  134. data/spec/public/reloading/reload_spec.rb +49 -27
  135. data/spec/public/request/multipart_spec.rb +26 -0
  136. data/spec/public/request/request_spec.rb +21 -2
  137. data/spec/public/router/fixation_spec.rb +27 -0
  138. data/spec/public/router/fixture/log/merb_test.log +30050 -0
  139. data/spec/public/router/nested_matches_spec.rb +97 -0
  140. data/spec/public/router/resource_spec.rb +1 -9
  141. data/spec/public/router/resources_spec.rb +0 -20
  142. data/spec/public/router/spec_helper.rb +27 -9
  143. data/spec/public/router/special_spec.rb +21 -8
  144. data/spec/public/template/template_spec.rb +17 -5
  145. data/spec/public/test/controller_matchers_spec.rb +10 -0
  146. data/spec/public/test/request_helper_spec.rb +29 -0
  147. data/spec/public/test/route_helper_spec.rb +18 -1
  148. data/spec/public/test/route_matchers_spec.rb +28 -1
  149. data/spec/public/test/view_matchers_spec.rb +3 -3
  150. data/spec/spec_helper.rb +56 -12
  151. metadata +89 -47
  152. data/lib/merb-core/core_ext/class.rb +0 -299
  153. data/lib/merb-core/core_ext/hash.rb +0 -426
  154. data/lib/merb-core/core_ext/mash.rb +0 -154
  155. data/lib/merb-core/core_ext/object.rb +0 -147
  156. data/lib/merb-core/core_ext/object_space.rb +0 -14
  157. data/lib/merb-core/core_ext/rubygems.rb +0 -28
  158. data/lib/merb-core/core_ext/set.rb +0 -46
  159. data/lib/merb-core/core_ext/string.rb +0 -89
  160. data/lib/merb-core/core_ext/time.rb +0 -13
  161. data/lib/merb-core/dispatch/exceptions.html.erb +0 -297
  162. data/spec/private/core_ext/class_spec.rb +0 -22
  163. data/spec/private/core_ext/hash_spec.rb +0 -522
  164. data/spec/private/core_ext/object_spec.rb +0 -121
  165. data/spec/private/core_ext/set_spec.rb +0 -26
  166. data/spec/private/core_ext/string_spec.rb +0 -167
  167. data/spec/private/core_ext/time_spec.rb +0 -16
  168. data/spec/private/dispatch/dispatch_spec.rb +0 -26
  169. data/spec/private/dispatch/fixture/log/development.log +0 -1
  170. data/spec/private/dispatch/fixture/log/merb.4000.pid +0 -1
  171. data/spec/private/dispatch/fixture/log/production.log +0 -1
  172. data/spec/private/dispatch/fixture/merb.4000.pid +0 -1
  173. data/spec/private/rack/application_spec.rb +0 -43
  174. data/spec/public/controller/log/merb.4000.pid +0 -1
  175. data/spec/public/directory_structure/directory/log/merb.4000.pid +0 -1
  176. data/spec/public/directory_structure/directory/merb.4000.pid +0 -1
  177. data/spec/public/reloading/directory/log/merb.4000.pid +0 -1
  178. data/spec/public/reloading/directory/merb.4000.pid +0 -1
@@ -1,7 +1,6 @@
1
1
  module Merb::Test::Rspec::ControllerMatchers
2
2
 
3
3
  class BeRedirect
4
-
5
4
  # ==== Parameters
6
5
  # target<Fixnum, ~status>::
7
6
  # Either the status code or a controller with a status code.
@@ -36,8 +35,10 @@ module Merb::Test::Rspec::ControllerMatchers
36
35
 
37
36
  # === Parameters
38
37
  # String:: The expected location
39
- def initialize(expected)
38
+ # Hash:: Optional hash of options (currently only :message)
39
+ def initialize(expected, options = {})
40
40
  @expected = expected
41
+ @options = options
41
42
  end
42
43
 
43
44
  # ==== Parameters
@@ -49,6 +50,12 @@ module Merb::Test::Rspec::ControllerMatchers
49
50
  def matches?(target)
50
51
  @target, @location = target, target.headers['Location']
51
52
  @redirected = BeRedirect.new.matches?(target.status)
53
+
54
+ if @options[:message]
55
+ msg = Merb::Request.escape([Marshal.dump(@options[:message])].pack("m"))
56
+ @expected << "?_message=#{msg}"
57
+ end
58
+
52
59
  @location == @expected && @redirected
53
60
  end
54
61
 
@@ -158,6 +165,27 @@ module Merb::Test::Rspec::ControllerMatchers
158
165
  end
159
166
  end
160
167
 
168
+ class BeError
169
+ def initialize(expected)
170
+ @expected = expected
171
+ end
172
+
173
+ def matches?(target)
174
+ @target = target
175
+ @target.request.exceptions &&
176
+ @target.request.exceptions.first.is_a?(@expected)
177
+ end
178
+
179
+ def failure_message
180
+ "expected #{@target} to be a #{@expected} error, but it was " <<
181
+ @target.request.exceptions.first.inspect
182
+ end
183
+
184
+ def negative_failure_message
185
+ "expected #{@target} not to be a #{@expected} error, but it was"
186
+ end
187
+ end
188
+
161
189
  class Provide
162
190
 
163
191
  # === Parameters
@@ -232,8 +260,8 @@ module Merb::Test::Rspec::ControllerMatchers
232
260
  # ==== Examples
233
261
  # # Passes if the controller was redirected to http://example.com/
234
262
  # controller.should redirect_to('http://example.com/')
235
- def redirect_to(expected)
236
- RedirectTo.new(expected)
263
+ def redirect_to(expected, options = {})
264
+ RedirectTo.new(expected, options)
237
265
  end
238
266
 
239
267
  alias_method :be_redirection_to, :redirect_to
@@ -302,6 +330,10 @@ module Merb::Test::Rspec::ControllerMatchers
302
330
  def be_missing
303
331
  BeMissing.new
304
332
  end
333
+
334
+ def be_error(expected)
335
+ BeError.new(expected)
336
+ end
305
337
 
306
338
  alias_method :be_client_error, :be_missing
307
339
 
@@ -48,7 +48,7 @@ module Merb::Test::Rspec::RouteMatchers
48
48
  # If parameters is an object, then a new expected hash will be constructed
49
49
  # with the key :id set to parameters.to_param.
50
50
  def with(parameters)
51
- @paramter_matcher = ParameterMatcher.new(parameters)
51
+ @parameter_matcher = ParameterMatcher.new(parameters)
52
52
 
53
53
  self
54
54
  end
@@ -56,17 +56,26 @@ module Merb::Test::Rspec::RouteMatchers
56
56
  # ==== Returns
57
57
  # String:: The failure message.
58
58
  def failure_message
59
- "expected the request to route to #{@expected_controller.camel_case}##{@expected_action}, but was #{@target_controller.camel_case}##{@target_action}"
59
+ "expected the request to route to #{@expected_controller.camel_case}##{@expected_action}#{expected_parameters_message}, but was #{@target_controller.camel_case}##{@target_action}#{actual_parameters_message}"
60
60
  end
61
61
 
62
62
  # ==== Returns
63
63
  # String:: The failure message to be displayed in negative matches.
64
64
  def negative_failure_message
65
- "expected the request not to route to #{@expected_controller.camel_case}##{@expected_action}, but it did"
65
+ "expected the request not to route to #{@expected_controller.camel_case}##{@expected_action}#{expected_parameters_message}, but it did"
66
+ end
67
+
68
+ def expected_parameters_message
69
+ " with #{@parameter_matcher.expected.inspect}" if @parameter_matcher
70
+ end
71
+
72
+ def actual_parameters_message
73
+ " with #{(@parameter_matcher.actual || {}).inspect}" if @parameter_matcher
66
74
  end
67
75
  end
68
76
 
69
77
  class ParameterMatcher
78
+ attr_accessor :expected, :actual
70
79
 
71
80
  # ==== Parameters
72
81
  # hash_or_object<Hash, ~to_param>:: The parameters to match.
@@ -251,13 +251,13 @@ module Merb::Test::Rspec::ViewMatchers
251
251
  # ==== Returns
252
252
  # String:: The failure message.
253
253
  def failure_message
254
- "expected the following element's content to #{content_message}:\n#{@element.inner_content}"
254
+ "expected the following element's content to #{content_message}:\n#{@element.inner_text}"
255
255
  end
256
256
 
257
257
  # ==== Returns
258
258
  # String:: The failure message to be displayed in negative matches.
259
259
  def negative_failure_message
260
- "expected the following element's content to not #{content_message}:\n#{@element.inner_content}"
260
+ "expected the following element's content to not #{content_message}:\n#{@element.inner_text}"
261
261
  end
262
262
 
263
263
  def content_message
@@ -332,4 +332,4 @@ module Merb::Test::Rspec::ViewMatchers
332
332
  def contain(content)
333
333
  HasContent.new(content)
334
334
  end
335
- end
335
+ end
@@ -1,3 +1,4 @@
1
+ require 'rubygems'
1
2
  require 'open3'
2
3
  require 'benchmark'
3
4
 
@@ -2,8 +2,15 @@ desc "Run specs, run a specific spec with TASK=spec/path_to_spec.rb"
2
2
  task :spec => [ "spec:default" ]
3
3
 
4
4
  namespace :spec do
5
+ OPTS_FILENAME = "./spec/spec.opts"
6
+ if File.exist?(OPTS_FILENAME)
7
+ SPEC_OPTS = ["--options", OPTS_FILENAME]
8
+ else
9
+ SPEC_OPTS = ["--color", "--format", "specdoc"]
10
+ end
11
+
5
12
  Spec::Rake::SpecTask.new('default') do |t|
6
- t.spec_opts = ["--format", "specdoc", "--colour"]
13
+ t.spec_opts = SPEC_OPTS
7
14
  if(ENV['TASK'])
8
15
  t.spec_files = [ENV['TASK']]
9
16
  else
@@ -13,7 +20,7 @@ namespace :spec do
13
20
 
14
21
  desc "Run all model specs, run a spec for a specific Model with MODEL=MyModel"
15
22
  Spec::Rake::SpecTask.new('model') do |t|
16
- t.spec_opts = ["--format", "specdoc", "--colour"]
23
+ t.spec_opts = SPEC_OPTS
17
24
  if(ENV['MODEL'])
18
25
  t.spec_files = Dir["spec/models/**/#{ENV['MODEL']}_spec.rb"].sort
19
26
  else
@@ -23,7 +30,7 @@ namespace :spec do
23
30
 
24
31
  desc "Run all controller specs, run a spec for a specific Controller with CONTROLLER=MyController"
25
32
  Spec::Rake::SpecTask.new('controller') do |t|
26
- t.spec_opts = ["--format", "specdoc", "--colour"]
33
+ t.spec_opts = SPEC_OPTS
27
34
  if(ENV['CONTROLLER'])
28
35
  t.spec_files = Dir["spec/controllers/**/#{ENV['CONTROLLER']}_spec.rb"].sort
29
36
  else
@@ -33,7 +40,7 @@ namespace :spec do
33
40
 
34
41
  desc "Run all view specs, run specs for a specific controller (and view) with CONTROLLER=MyController (VIEW=MyView)"
35
42
  Spec::Rake::SpecTask.new('view') do |t|
36
- t.spec_opts = ["--format", "specdoc", "--colour"]
43
+ t.spec_opts = SPEC_OPTS
37
44
  if(ENV['CONTROLLER'] and ENV['VIEW'])
38
45
  t.spec_files = Dir["spec/views/**/#{ENV['CONTROLLER']}/#{ENV['VIEW']}*_spec.rb"].sort
39
46
  elsif(ENV['CONTROLLER'])
@@ -52,9 +59,10 @@ namespace :spec do
52
59
 
53
60
  desc "Run specs and check coverage with rcov"
54
61
  Spec::Rake::SpecTask.new('coverage') do |t|
55
- t.spec_opts = ["--format", "specdoc", "--colour"]
62
+ t.spec_opts = SPEC_OPTS
56
63
  t.spec_files = Dir['spec/**/*_spec.rb'].sort
57
64
  t.libs = ['lib', 'server/lib' ]
58
65
  t.rcov = true
66
+ t.rcov_opts = ["--exclude 'config,spec,#{Gem::path.join(',')}'"]
59
67
  end
60
68
  end
@@ -0,0 +1,14 @@
1
+ class String
2
+ def contain?(value)
3
+ self.include?(value)
4
+ end
5
+
6
+ alias_method :contains?, :contain?
7
+
8
+ def match?(regex)
9
+ self.match(regex)
10
+ end
11
+
12
+ alias_method :matches?, :match?
13
+
14
+ end
@@ -292,11 +292,11 @@ class Dictionary
292
292
  end
293
293
 
294
294
  def reject( &block )
295
- self.dup.delete_if &block
295
+ self.dup.delete_if(&block)
296
296
  end
297
297
 
298
298
  def reject!( &block )
299
- hsh2 = reject &block
299
+ hsh2 = reject(&block)
300
300
  self == hsh2 ? nil : hsh2
301
301
  end
302
302
 
@@ -321,7 +321,7 @@ class Dictionary
321
321
  end
322
322
 
323
323
  def <<(kv)
324
- push *kv
324
+ push( *kv )
325
325
  end
326
326
 
327
327
  def push( k,v )
@@ -38,6 +38,20 @@ module English
38
38
  plural_word(singular, plural)
39
39
  end
40
40
 
41
+ def clear(type = :all)
42
+ if type == :singular || type == :all
43
+ @singular_of = {}
44
+ @singular_rules = []
45
+ @singularization_rules, @singularization_regex = nil, nil
46
+ end
47
+ if type == :plural || type == :all
48
+ @singular_of = {}
49
+ @singular_rules = []
50
+ @singularization_rules, @singularization_regex = nil, nil
51
+ end
52
+ end
53
+
54
+
41
55
  # Define a singularization exception.
42
56
  #
43
57
  # ==== Parameters
@@ -47,6 +61,7 @@ module English
47
61
  # plural form of the word
48
62
  def singular_word(singular, plural)
49
63
  @singular_of[plural] = singular
64
+ @singular_of[plural.capitalize] = singular.capitalize
50
65
  end
51
66
 
52
67
  # Define a pluralization exception.
@@ -58,6 +73,7 @@ module English
58
73
  # plural form of the word
59
74
  def plural_word(singular, plural)
60
75
  @plural_of[singular] = plural
76
+ @plural_of[singular.capitalize] = plural.capitalize
61
77
  end
62
78
 
63
79
  # Define a general rule.
@@ -67,7 +83,9 @@ module English
67
83
  # ending of the word in singular form
68
84
  # plural<String>::
69
85
  # ending of the word in plural form
70
- #
86
+ # whole_word<Boolean>::
87
+ # for capitalization, since words can be
88
+ # capitalized (Man => Men) #
71
89
  # ==== Examples
72
90
  # Once the following rule is defined:
73
91
  # Language::English::Inflector.rule 'y', 'ies'
@@ -77,9 +95,12 @@ module English
77
95
  # => flies
78
96
  # irb> "cry".plural
79
97
  # => cries
80
- def rule(singular, plural)
98
+ # Define a general rule.
99
+
100
+ def rule(singular, plural, whole_word = false)
81
101
  singular_rule(singular, plural)
82
102
  plural_rule(singular, plural)
103
+ word(singular, plural) if whole_word
83
104
  end
84
105
 
85
106
  # Define a singularization rule.
@@ -122,31 +143,26 @@ module English
122
143
 
123
144
  # Read prepared singularization rules.
124
145
  def singularization_rules
125
- return @singularization_rules if @singularization_rules
126
- sorted = @singular_rules.sort_by{ |s, p| "#{p}".size }.reverse
127
- @singularization_rules = sorted.collect do |s, p|
128
- [ /#{p}$/, "#{s}" ]
146
+ if defined?(@singularization_regex) && @singularization_regex
147
+ return [@singularization_regex, @singularization_hash]
129
148
  end
149
+ # No sorting needed: Regexen match on longest string
150
+ @singularization_regex = Regexp.new("(" + @singular_rules.map {|s,p| p}.join("|") + ")$", "i")
151
+ @singularization_hash = Hash[*@singular_rules.flatten].invert
152
+ [@singularization_regex, @singularization_hash]
130
153
  end
131
154
 
132
155
  # Read prepared pluralization rules.
133
156
  def pluralization_rules
134
- return @pluralization_rules if @pluralization_rules
135
- sorted = @plural_rules.sort_by{ |s, p| "#{s}".size }.reverse
136
- @pluralization_rules = sorted.collect do |s, p|
137
- [ /#{s}$/, "#{p}" ]
157
+ if defined?(@pluralization_regex) && @pluralization_regex
158
+ return [@pluralization_regex, @pluralization_hash]
138
159
  end
160
+ @pluralization_regex = Regexp.new("(" + @plural_rules.map {|s,p| s}.join("|") + ")$", "i")
161
+ @pluralization_hash = Hash[*@plural_rules.flatten]
162
+ [@pluralization_regex, @pluralization_hash]
139
163
  end
140
164
 
141
- #
142
- def plural_of
143
- @plural_of
144
- end
145
-
146
- #
147
- def singular_of
148
- @singular_of
149
- end
165
+ attr_reader :singular_of, :plural_of
150
166
 
151
167
  # Convert an English word from plurel to singular.
152
168
  #
@@ -166,9 +182,9 @@ module English
166
182
  return result.dup
167
183
  end
168
184
  result = word.dup
169
- singularization_rules.each do |(match, replacement)|
170
- break if result.gsub!(match, replacement)
171
- end
185
+ regex, hash = singularization_rules
186
+ result.sub!(regex) {|m| hash[m]}
187
+ singular_of[word] = result
172
188
  return result
173
189
  end
174
190
 
@@ -190,14 +206,15 @@ module English
190
206
  # ==== Notes
191
207
  # Aliased as pluralize (a Railism)
192
208
  def plural(word)
209
+ # special exceptions
210
+ return "" if word == ""
193
211
  if result = plural_of[word]
194
212
  return result.dup
195
213
  end
196
- #return self.dup if /s$/ =~ self # ???
197
214
  result = word.dup
198
- pluralization_rules.each do |(match, replacement)|
199
- break if result.gsub!(match, replacement)
200
- end
215
+ regex, hash = pluralization_rules
216
+ result.sub!(regex) {|m| hash[m]}
217
+ plural_of[word] = result
201
218
  return result
202
219
  end
203
220
 
@@ -216,6 +233,11 @@ module English
216
233
  word 'sheep'
217
234
  word 'moose'
218
235
  word 'hovercraft'
236
+ word 'grass'
237
+ word 'rain'
238
+ word 'milk'
239
+ word 'rice'
240
+ word 'plurals'
219
241
 
220
242
  # Two arguments defines a singular and plural exception.
221
243
 
@@ -229,15 +251,13 @@ module English
229
251
  word 'axis' , 'axes'
230
252
  word 'crisis' , 'crises'
231
253
  word 'testis' , 'testes'
232
- word 'child' , 'children'
233
- word 'person' , 'people'
234
254
  word 'potato' , 'potatoes'
235
255
  word 'tomato' , 'tomatoes'
236
256
  word 'buffalo' , 'buffaloes'
237
257
  word 'torpedo' , 'torpedoes'
238
- word 'quiz' , 'quizes'
258
+ word 'quiz' , 'quizzes'
239
259
  word 'matrix' , 'matrices'
240
- word 'vertex' , 'vetices'
260
+ word 'vertex' , 'vertices'
241
261
  word 'index' , 'indices'
242
262
  word 'ox' , 'oxen'
243
263
  word 'mouse' , 'mice'
@@ -245,14 +265,29 @@ module English
245
265
  word 'thesis' , 'theses'
246
266
  word 'thief' , 'thieves'
247
267
  word 'analysis' , 'analyses'
248
-
268
+ word 'erratum' , 'errata'
269
+ word 'phenomenon', 'phenomena'
270
+ word 'octopus' , 'octopi'
271
+ word 'thesaurus' , 'thesauri'
272
+ word 'movie' , 'movies'
273
+ word 'cactus' , 'cacti'
274
+ word 'plus' , 'plusses'
275
+ word 'cross' , 'crosses'
276
+ word 'medium' , 'media'
277
+ word 'cow' , 'kine'
278
+ word 'datum' , 'data'
279
+ word 'basis' , 'bases'
280
+ word 'diagnosis' , 'diagnoses'
281
+
249
282
  # One-way singularization exception (convert plural to singular).
250
283
 
251
- singular_word 'cactus', 'cacti'
252
-
253
284
  # General rules.
254
-
255
- rule 'hive' , 'hives'
285
+ rule 'person' , 'people', true
286
+ rule 'shoe' , 'shoes', true
287
+ rule 'hive' , 'hives', true
288
+ rule 'man' , 'men', true
289
+ rule 'child' , 'children', true
290
+ rule 'news' , 'news', true
256
291
  rule 'rf' , 'rves'
257
292
  rule 'af' , 'aves'
258
293
  rule 'ero' , 'eroes'
@@ -270,6 +305,13 @@ module English
270
305
  rule 'y' , 'ies'
271
306
  rule 'x' , 'xes'
272
307
  rule 'lf' , 'lves'
308
+ rule 'ffe' , 'ffes'
309
+ rule 'afe' , 'aves'
310
+ rule 'ouse' , 'ouses'
311
+ # more cases of words ending in -oses not being singularized properly
312
+ # than cases of words ending in -osis
313
+ # rule 'osis' , 'oses'
314
+ rule 'ox' , 'oxes'
273
315
  rule 'us' , 'uses'
274
316
  rule '' , 's'
275
317
 
@@ -281,8 +323,11 @@ module English
281
323
 
282
324
  # One-way plural rules.
283
325
 
284
- plural_rule 'fe' , 'ves' # safe, wife
285
- plural_rule 's' , 'ses'
326
+ #plural_rule 'fe' , 'ves' # safe, wife
327
+ plural_rule 's' , 'ses'
328
+ plural_rule 'ive' , 'ives' # don't want to snag wife
329
+ plural_rule 'fe' , 'ves' # don't want to snag perspectives
330
+
286
331
 
287
332
  end
288
333
  end