merb-core 0.9.2
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +20 -0
- data/README +21 -0
- data/Rakefile +285 -0
- data/TODO +0 -0
- data/bin/merb +8 -0
- data/bin/merb-specs +5 -0
- data/docs/bootloading.dox +57 -0
- data/docs/documentation_standards +40 -0
- data/docs/new_render_api +51 -0
- data/lib/merb-core.rb +304 -0
- data/lib/merb-core/autoload.rb +29 -0
- data/lib/merb-core/bootloader.rb +601 -0
- data/lib/merb-core/config.rb +284 -0
- data/lib/merb-core/constants.rb +43 -0
- data/lib/merb-core/controller/abstract_controller.rb +531 -0
- data/lib/merb-core/controller/exceptions.rb +257 -0
- data/lib/merb-core/controller/merb_controller.rb +214 -0
- data/lib/merb-core/controller/mime.rb +88 -0
- data/lib/merb-core/controller/mixins/controller.rb +262 -0
- data/lib/merb-core/controller/mixins/render.rb +324 -0
- data/lib/merb-core/controller/mixins/responder.rb +464 -0
- data/lib/merb-core/controller/template.rb +205 -0
- data/lib/merb-core/core_ext.rb +12 -0
- data/lib/merb-core/core_ext/class.rb +192 -0
- data/lib/merb-core/core_ext/hash.rb +422 -0
- data/lib/merb-core/core_ext/kernel.rb +304 -0
- data/lib/merb-core/core_ext/mash.rb +154 -0
- data/lib/merb-core/core_ext/object.rb +136 -0
- data/lib/merb-core/core_ext/object_space.rb +14 -0
- data/lib/merb-core/core_ext/rubygems.rb +28 -0
- data/lib/merb-core/core_ext/set.rb +41 -0
- data/lib/merb-core/core_ext/string.rb +69 -0
- data/lib/merb-core/dispatch/cookies.rb +92 -0
- data/lib/merb-core/dispatch/dispatcher.rb +233 -0
- data/lib/merb-core/dispatch/exceptions.html.erb +297 -0
- data/lib/merb-core/dispatch/request.rb +560 -0
- data/lib/merb-core/dispatch/router.rb +141 -0
- data/lib/merb-core/dispatch/router/behavior.rb +777 -0
- data/lib/merb-core/dispatch/router/cached_proc.rb +52 -0
- data/lib/merb-core/dispatch/router/route.rb +212 -0
- data/lib/merb-core/dispatch/session.rb +28 -0
- data/lib/merb-core/dispatch/session/cookie.rb +166 -0
- data/lib/merb-core/dispatch/session/memcached.rb +161 -0
- data/lib/merb-core/dispatch/session/memory.rb +234 -0
- data/lib/merb-core/gem_ext/erubis.rb +19 -0
- data/lib/merb-core/logger.rb +230 -0
- data/lib/merb-core/plugins.rb +25 -0
- data/lib/merb-core/rack.rb +15 -0
- data/lib/merb-core/rack/adapter.rb +42 -0
- data/lib/merb-core/rack/adapter/ebb.rb +22 -0
- data/lib/merb-core/rack/adapter/evented_mongrel.rb +24 -0
- data/lib/merb-core/rack/adapter/fcgi.rb +16 -0
- data/lib/merb-core/rack/adapter/irb.rb +108 -0
- data/lib/merb-core/rack/adapter/mongrel.rb +25 -0
- data/lib/merb-core/rack/adapter/runner.rb +27 -0
- data/lib/merb-core/rack/adapter/thin.rb +27 -0
- data/lib/merb-core/rack/adapter/webrick.rb +35 -0
- data/lib/merb-core/rack/application.rb +77 -0
- data/lib/merb-core/rack/handler/mongrel.rb +97 -0
- data/lib/merb-core/server.rb +184 -0
- data/lib/merb-core/test.rb +10 -0
- data/lib/merb-core/test/helpers.rb +9 -0
- data/lib/merb-core/test/helpers/controller_helper.rb +8 -0
- data/lib/merb-core/test/helpers/multipart_request_helper.rb +175 -0
- data/lib/merb-core/test/helpers/request_helper.rb +257 -0
- data/lib/merb-core/test/helpers/route_helper.rb +33 -0
- data/lib/merb-core/test/helpers/view_helper.rb +121 -0
- data/lib/merb-core/test/matchers.rb +9 -0
- data/lib/merb-core/test/matchers/controller_matchers.rb +269 -0
- data/lib/merb-core/test/matchers/route_matchers.rb +136 -0
- data/lib/merb-core/test/matchers/view_matchers.rb +293 -0
- data/lib/merb-core/test/run_specs.rb +38 -0
- data/lib/merb-core/test/tasks/spectasks.rb +39 -0
- data/lib/merb-core/test/test_ext/hpricot.rb +32 -0
- data/lib/merb-core/test/test_ext/object.rb +14 -0
- data/lib/merb-core/vendor/facets.rb +2 -0
- data/lib/merb-core/vendor/facets/dictionary.rb +433 -0
- data/lib/merb-core/vendor/facets/inflect.rb +211 -0
- data/lib/merb-core/version.rb +11 -0
- data/spec/private/config/adapter_spec.rb +32 -0
- data/spec/private/config/config_spec.rb +139 -0
- data/spec/private/config/environment_spec.rb +13 -0
- data/spec/private/config/spec_helper.rb +1 -0
- data/spec/private/core_ext/hash_spec.rb +506 -0
- data/spec/private/core_ext/kernel_spec.rb +46 -0
- data/spec/private/core_ext/object_spec.rb +39 -0
- data/spec/private/core_ext/set_spec.rb +26 -0
- data/spec/private/core_ext/string_spec.rb +9 -0
- data/spec/private/dispatch/cookies_spec.rb +107 -0
- data/spec/private/dispatch/dispatch_spec.rb +26 -0
- data/spec/private/dispatch/fixture/app/controllers/application.rb +4 -0
- data/spec/private/dispatch/fixture/app/controllers/exceptions.rb +27 -0
- data/spec/private/dispatch/fixture/app/controllers/foo.rb +21 -0
- data/spec/private/dispatch/fixture/app/helpers/global_helpers.rb +8 -0
- data/spec/private/dispatch/fixture/app/views/exeptions/client_error.html.erb +37 -0
- data/spec/private/dispatch/fixture/app/views/exeptions/internal_server_error.html.erb +216 -0
- data/spec/private/dispatch/fixture/app/views/exeptions/not_acceptable.html.erb +38 -0
- data/spec/private/dispatch/fixture/app/views/exeptions/not_found.html.erb +40 -0
- data/spec/private/dispatch/fixture/app/views/foo/bar.html.erb +0 -0
- data/spec/private/dispatch/fixture/app/views/layout/application.html.erb +11 -0
- data/spec/private/dispatch/fixture/config/environments/development.rb +6 -0
- data/spec/private/dispatch/fixture/config/environments/production.rb +5 -0
- data/spec/private/dispatch/fixture/config/environments/test.rb +6 -0
- data/spec/private/dispatch/fixture/config/init.rb +45 -0
- data/spec/private/dispatch/fixture/config/rack.rb +1 -0
- data/spec/private/dispatch/fixture/config/router.rb +35 -0
- data/spec/private/dispatch/fixture/log/development.log +1 -0
- data/spec/private/dispatch/fixture/log/merb.4000.pid +1 -0
- data/spec/private/dispatch/fixture/log/merb_test.log +2040 -0
- data/spec/private/dispatch/fixture/log/production.log +1 -0
- data/spec/private/dispatch/fixture/merb.4000.pid +1 -0
- data/spec/private/dispatch/fixture/public/images/merb.jpg +0 -0
- data/spec/private/dispatch/fixture/public/merb.fcgi +4 -0
- data/spec/private/dispatch/fixture/public/stylesheets/master.css +119 -0
- data/spec/private/dispatch/route_params_spec.rb +24 -0
- data/spec/private/dispatch/spec_helper.rb +1 -0
- data/spec/private/plugins/plugin_spec.rb +81 -0
- data/spec/private/rack/application_spec.rb +43 -0
- data/spec/public/DEFINITIONS +11 -0
- data/spec/public/abstract_controller/controllers/alt_views/layout/application.erb +1 -0
- data/spec/public/abstract_controller/controllers/alt_views/layout/merb/test/fixtures/abstract/render_string_controller_layout.erb +1 -0
- data/spec/public/abstract_controller/controllers/alt_views/layout/merb/test/fixtures/abstract/render_template_controller_layout.erb +1 -0
- data/spec/public/abstract_controller/controllers/alt_views/merb/test/fixtures/abstract/display_object_with_multiple_roots/index.erb +1 -0
- data/spec/public/abstract_controller/controllers/alt_views/merb/test/fixtures/abstract/display_object_with_multiple_roots/show.erb +1 -0
- data/spec/public/abstract_controller/controllers/alt_views/merb/test/fixtures/abstract/render_template_multiple_roots/index.erb +1 -0
- data/spec/public/abstract_controller/controllers/alt_views/partial/basic_partial_with_multiple_roots/_partial.erb +1 -0
- data/spec/public/abstract_controller/controllers/alt_views/render_template_multiple_roots_and_custom_location/index.erb +1 -0
- data/spec/public/abstract_controller/controllers/alt_views/render_template_multiple_roots_inherited/index.erb +1 -0
- data/spec/public/abstract_controller/controllers/display.rb +54 -0
- data/spec/public/abstract_controller/controllers/filters.rb +167 -0
- data/spec/public/abstract_controller/controllers/helpers.rb +31 -0
- data/spec/public/abstract_controller/controllers/partial.rb +106 -0
- data/spec/public/abstract_controller/controllers/render.rb +86 -0
- data/spec/public/abstract_controller/controllers/views/helpers/capture/index.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/helpers/concat/index.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/layout/alt.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/layout/custom.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/merb/test/fixtures/abstract/display_object/index.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/merb/test/fixtures/abstract/display_object_with_action/new.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/merb/test/fixtures/abstract/render_template/index.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/merb/test/fixtures/abstract/render_template_app_layout/index.erb +0 -0
- data/spec/public/abstract_controller/controllers/views/merb/test/fixtures/abstract/render_template_custom_layout/index.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/merb/test/fixtures/abstract/render_template_multiple_roots/index.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/merb/test/fixtures/abstract/render_template_multiple_roots/show.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/partial/another_directory/_partial.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/partial/basic_partial/_partial.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/partial/basic_partial/index.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/partial/basic_partial_with_multiple_roots/index.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/partial/nested_partial/_first.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/partial/nested_partial/_second.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/partial/nested_partial/index.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/partial/partial_in_another_directory/index.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/partial/partial_with_both/_collection.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/partial/partial_with_both/index.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/partial/partial_with_collections/_collection.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/partial/partial_with_collections/index.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/partial/partial_with_collections_and_as/_collection.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/partial/partial_with_collections_and_as/index.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/partial/partial_with_locals/_variables.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/partial/partial_with_locals/index.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/partial/partial_with_with_and_locals/_both.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/partial/partial_with_with_and_locals/index.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/partial/with_as_partial/_with_partial.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/partial/with_as_partial/index.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/partial/with_nil_partial/_with_partial.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/partial/with_nil_partial/index.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/partial/with_partial/_with_partial.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/partial/with_partial/index.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/test_display/foo.html.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/test_render/foo.html.erb +0 -0
- data/spec/public/abstract_controller/controllers/views/wonderful/index.erb +1 -0
- data/spec/public/abstract_controller/display_spec.rb +33 -0
- data/spec/public/abstract_controller/filter_spec.rb +80 -0
- data/spec/public/abstract_controller/helper_spec.rb +13 -0
- data/spec/public/abstract_controller/partial_spec.rb +53 -0
- data/spec/public/abstract_controller/render_spec.rb +70 -0
- data/spec/public/abstract_controller/spec_helper.rb +27 -0
- data/spec/public/boot_loader/boot_loader_spec.rb +33 -0
- data/spec/public/boot_loader/spec_helper.rb +1 -0
- data/spec/public/controller/base_spec.rb +31 -0
- data/spec/public/controller/controllers/base.rb +41 -0
- data/spec/public/controller/controllers/display.rb +40 -0
- data/spec/public/controller/controllers/responder.rb +67 -0
- data/spec/public/controller/controllers/url.rb +7 -0
- data/spec/public/controller/controllers/views/layout/custom.html.erb +1 -0
- data/spec/public/controller/controllers/views/merb/test/fixtures/controllers/class_provides/index.html.erb +1 -0
- data/spec/public/controller/controllers/views/merb/test/fixtures/controllers/class_provides/index.xml.erb +1 -0
- data/spec/public/controller/controllers/views/merb/test/fixtures/controllers/display_with_template/index.html.erb +1 -0
- data/spec/public/controller/controllers/views/merb/test/fixtures/controllers/html_default/index.html.erb +1 -0
- data/spec/public/controller/controllers/views/merb/test/fixtures/controllers/layout/custom.html.erb +1 -0
- data/spec/public/controller/controllers/views/merb/test/fixtures/controllers/local_provides/index.html.erb +1 -0
- data/spec/public/controller/controllers/views/merb/test/fixtures/controllers/local_provides/index.xml.erb +1 -0
- data/spec/public/controller/controllers/views/merb/test/fixtures/controllers/multi_provides/index.html.erb +1 -0
- data/spec/public/controller/controllers/views/merb/test/fixtures/controllers/multi_provides/index.js.erb +1 -0
- data/spec/public/controller/display_spec.rb +34 -0
- data/spec/public/controller/log/merb.4000.pid +1 -0
- data/spec/public/controller/responder_spec.rb +95 -0
- data/spec/public/controller/spec_helper.rb +9 -0
- data/spec/public/controller/url_spec.rb +152 -0
- data/spec/public/directory_structure/directory/app/controllers/application.rb +3 -0
- data/spec/public/directory_structure/directory/app/controllers/base.rb +13 -0
- data/spec/public/directory_structure/directory/app/controllers/custom.rb +19 -0
- data/spec/public/directory_structure/directory/app/views/base/template.html.erb +1 -0
- data/spec/public/directory_structure/directory/app/views/wonderful/template.erb +1 -0
- data/spec/public/directory_structure/directory/config/router.rb +3 -0
- data/spec/public/directory_structure/directory/log/merb.4000.pid +1 -0
- data/spec/public/directory_structure/directory/log/merb_test.log +265 -0
- data/spec/public/directory_structure/directory/merb.4000.pid +1 -0
- data/spec/public/directory_structure/directory_spec.rb +44 -0
- data/spec/public/logger/logger_spec.rb +175 -0
- data/spec/public/logger/spec_helper.rb +1 -0
- data/spec/public/reloading/directory/app/controllers/application.rb +3 -0
- data/spec/public/reloading/directory/app/controllers/reload.rb +6 -0
- data/spec/public/reloading/directory/config/init.rb +2 -0
- data/spec/public/reloading/directory/log/merb.4000.pid +1 -0
- data/spec/public/reloading/directory/log/merb_test.log +59 -0
- data/spec/public/reloading/directory/merb.4000.pid +1 -0
- data/spec/public/reloading/reload_spec.rb +80 -0
- data/spec/public/request/multipart_spec.rb +15 -0
- data/spec/public/request/request_spec.rb +207 -0
- data/spec/public/router/default_spec.rb +21 -0
- data/spec/public/router/deferred_spec.rb +22 -0
- data/spec/public/router/namespace_spec.rb +113 -0
- data/spec/public/router/nested_resources_spec.rb +34 -0
- data/spec/public/router/resource_spec.rb +45 -0
- data/spec/public/router/resources_spec.rb +57 -0
- data/spec/public/router/spec_helper.rb +72 -0
- data/spec/public/router/special_spec.rb +44 -0
- data/spec/public/router/string_spec.rb +61 -0
- data/spec/public/template/template_spec.rb +92 -0
- data/spec/public/template/templates/error.html.erb +2 -0
- data/spec/public/template/templates/template.html.erb +1 -0
- data/spec/public/template/templates/template.html.myt +1 -0
- data/spec/public/test/controller_matchers_spec.rb +378 -0
- data/spec/public/test/controllers/controller_assertion_mock.rb +7 -0
- data/spec/public/test/controllers/dispatch_controller.rb +11 -0
- data/spec/public/test/controllers/spec_helper_controller.rb +30 -0
- data/spec/public/test/multipart_request_helper_spec.rb +159 -0
- data/spec/public/test/multipart_upload_text_file.txt +1 -0
- data/spec/public/test/request_helper_spec.rb +153 -0
- data/spec/public/test/route_helper_spec.rb +54 -0
- data/spec/public/test/route_matchers_spec.rb +133 -0
- data/spec/public/test/view_helper_spec.rb +96 -0
- data/spec/public/test/view_matchers_spec.rb +107 -0
- data/spec/spec_helper.rb +71 -0
- metadata +488 -0
@@ -0,0 +1,39 @@
|
|
1
|
+
desc "Run all specs"
|
2
|
+
Spec::Rake::SpecTask.new('specs') do |t|
|
3
|
+
t.spec_opts = ["--format", "specdoc", "--colour"]
|
4
|
+
t.spec_files = Dir['spec/**/*_spec.rb'].sort
|
5
|
+
end
|
6
|
+
|
7
|
+
desc "Run all model specs"
|
8
|
+
Spec::Rake::SpecTask.new('model_specs') do |t|
|
9
|
+
t.spec_opts = ["--format", "specdoc", "--colour"]
|
10
|
+
t.spec_files = Dir['spec/models/**/*_spec.rb'].sort
|
11
|
+
end
|
12
|
+
|
13
|
+
desc "Run all controller specs"
|
14
|
+
Spec::Rake::SpecTask.new('controller_specs') do |t|
|
15
|
+
t.spec_opts = ["--format", "specdoc", "--colour"]
|
16
|
+
t.spec_files = Dir['spec/controllers/**/*_spec.rb'].sort
|
17
|
+
end
|
18
|
+
|
19
|
+
desc "Run a specific spec with TASK=xxxx"
|
20
|
+
Spec::Rake::SpecTask.new('spec') do |t|
|
21
|
+
t.spec_opts = ["--format", "specdoc", "--colour"]
|
22
|
+
t.libs = ['lib', 'server/lib' ]
|
23
|
+
t.spec_files = ["spec/#{ENV['TASK']}_spec.rb"]
|
24
|
+
end
|
25
|
+
|
26
|
+
desc "Run all specs output html"
|
27
|
+
Spec::Rake::SpecTask.new('specs_html') do |t|
|
28
|
+
t.spec_opts = ["--format", "html"]
|
29
|
+
t.libs = ['lib', 'server/lib' ]
|
30
|
+
t.spec_files = Dir['spec/**/*_spec.rb'].sort
|
31
|
+
end
|
32
|
+
|
33
|
+
desc "RCov"
|
34
|
+
Spec::Rake::SpecTask.new('rcov') do |t|
|
35
|
+
t.spec_opts = ["--format", "specdoc", "--colour"]
|
36
|
+
t.spec_files = Dir['spec/**/*_spec.rb'].sort
|
37
|
+
t.libs = ['lib', 'server/lib' ]
|
38
|
+
t.rcov = true
|
39
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# http://yehudakatz.com/2007/01/27/a-better-assert_select-assert_elements/
|
2
|
+
# based on assert_elements
|
3
|
+
# Author: Yehuda Katz
|
4
|
+
# Email: wycats @nospam@ gmail.com
|
5
|
+
# Web: http://www.yehudakatz.com
|
6
|
+
#
|
7
|
+
# which was based on HpricotTestHelper
|
8
|
+
# Author: Luke Redpath
|
9
|
+
# Email: contact @nospam@ lukeredpath.co.uk
|
10
|
+
# Web: www.lukeredpath.co.uk / opensource.agileevolved.com
|
11
|
+
|
12
|
+
class Hpricot::Elem
|
13
|
+
def contain?(value)
|
14
|
+
self.inner_text.include?(value)
|
15
|
+
end
|
16
|
+
|
17
|
+
alias_method :contains?, :contain?
|
18
|
+
|
19
|
+
def match?(regex)
|
20
|
+
self.inner_text.match(regex)
|
21
|
+
end
|
22
|
+
|
23
|
+
alias_method :matches?, :match?
|
24
|
+
|
25
|
+
# courtesy of 'thomas' from the comments
|
26
|
+
# of _whys blog - get in touch if you want a better credit!
|
27
|
+
def inner_text
|
28
|
+
self.children.collect do |child|
|
29
|
+
child.is_a?(Hpricot::Text) ? child.content : ((child.respond_to?("inner_text") && child.inner_text) || "")
|
30
|
+
end.join.strip
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
class Object
|
2
|
+
# ==== Parameters
|
3
|
+
# attr<~to_s>:: The name of the instance variable to get.
|
4
|
+
#
|
5
|
+
# ==== Returns
|
6
|
+
# Object:: The instance variable @attr for this object.
|
7
|
+
#
|
8
|
+
# ==== Examples
|
9
|
+
# # In a spec
|
10
|
+
# @my_obj.assigns(:my_value).should == @my_value
|
11
|
+
def assigns(attr)
|
12
|
+
self.instance_variable_get("@#{attr}")
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,433 @@
|
|
1
|
+
# TITLE:
|
2
|
+
#
|
3
|
+
# Dictionary
|
4
|
+
#
|
5
|
+
# AUTHORS:
|
6
|
+
#
|
7
|
+
# - Jan Molic
|
8
|
+
# - Thomas Sawyer
|
9
|
+
#
|
10
|
+
# CREDIT:
|
11
|
+
#
|
12
|
+
# - Andrew Johnson (merge, to_a, inspect, shift and Hash[])
|
13
|
+
# - Jeff Sharpe (reverse and reverse!)
|
14
|
+
# - Thomas Leitner (has_key? and key?)
|
15
|
+
#
|
16
|
+
# LICENSE:
|
17
|
+
#
|
18
|
+
# Copyright (c) 2005 Jan Molic, Thomas Sawyer
|
19
|
+
#
|
20
|
+
# Ruby License
|
21
|
+
#
|
22
|
+
# This module is free software. You may use, modify, and/or redistribute this
|
23
|
+
# software under the same terms as Ruby.
|
24
|
+
#
|
25
|
+
# This program is distributed in the hope that it will be useful, but WITHOUT
|
26
|
+
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
27
|
+
# FOR A PARTICULAR PURPOSE.
|
28
|
+
#
|
29
|
+
# Originally ported from OrderHash 2.0, Copyright (c) 2005 jan molic
|
30
|
+
#
|
31
|
+
# LOG:
|
32
|
+
#
|
33
|
+
# - 2007.10.31 trans
|
34
|
+
# Fixed initialize so the constructor blocks correctly effected dictionary
|
35
|
+
# rather then just the internal hash.
|
36
|
+
|
37
|
+
# = Dictionary
|
38
|
+
#
|
39
|
+
# The Dictionary class is a Hash that preserves order.
|
40
|
+
# So it has some array-like extensions also. By defualt
|
41
|
+
# a Dictionary object preserves insertion order, but any
|
42
|
+
# order can be specified including alphabetical key order.
|
43
|
+
#
|
44
|
+
# == Usage
|
45
|
+
#
|
46
|
+
# Just require this file and use Dictionary instead of Hash.
|
47
|
+
#
|
48
|
+
# # You can do simply
|
49
|
+
# hsh = Dictionary.new
|
50
|
+
# hsh['z'] = 1
|
51
|
+
# hsh['a'] = 2
|
52
|
+
# hsh['c'] = 3
|
53
|
+
# p hsh.keys #=> ['z','a','c']
|
54
|
+
#
|
55
|
+
# # or using Dictionary[] method
|
56
|
+
# hsh = Dictionary['z', 1, 'a', 2, 'c', 3]
|
57
|
+
# p hsh.keys #=> ['z','a','c']
|
58
|
+
#
|
59
|
+
# # but this doesn't preserve order
|
60
|
+
# hsh = Dictionary['z'=>1, 'a'=>2, 'c'=>3]
|
61
|
+
# p hsh.keys #=> ['a','c','z']
|
62
|
+
#
|
63
|
+
# # Dictionary has useful extensions: push, pop and unshift
|
64
|
+
# p hsh.push('to_end', 15) #=> true, key added
|
65
|
+
# p hsh.push('to_end', 30) #=> false, already - nothing happen
|
66
|
+
# p hsh.unshift('to_begin', 50) #=> true, key added
|
67
|
+
# p hsh.unshift('to_begin', 60) #=> false, already - nothing happen
|
68
|
+
# p hsh.keys #=> ["to_begin", "a", "c", "z", "to_end"]
|
69
|
+
# p hsh.pop #=> ["to_end", 15], if nothing remains, return nil
|
70
|
+
# p hsh.keys #=> ["to_begin", "a", "c", "z"]
|
71
|
+
# p hsh.shift #=> ["to_begin", 30], if nothing remains, return nil
|
72
|
+
#
|
73
|
+
# == Usage Notes
|
74
|
+
#
|
75
|
+
# * You can use #order_by to set internal sort order.
|
76
|
+
# * #<< takes a two element [k,v] array and inserts.
|
77
|
+
# * Use ::auto which creates Dictionay sub-entries as needed.
|
78
|
+
# * And ::alpha which creates a new Dictionary sorted by key.
|
79
|
+
|
80
|
+
class Dictionary
|
81
|
+
|
82
|
+
include Enumerable
|
83
|
+
|
84
|
+
class << self
|
85
|
+
#--
|
86
|
+
# TODO is this needed? Doesn't the super class do this?
|
87
|
+
#++
|
88
|
+
def [](*args)
|
89
|
+
hsh = new
|
90
|
+
if Hash === args[0]
|
91
|
+
hsh.replace(args[0])
|
92
|
+
elsif (args.size % 2) != 0
|
93
|
+
raise ArgumentError, "odd number of elements for Hash"
|
94
|
+
else
|
95
|
+
while !args.empty?
|
96
|
+
hsh[args.shift] = args.shift
|
97
|
+
end
|
98
|
+
end
|
99
|
+
hsh
|
100
|
+
end
|
101
|
+
|
102
|
+
# Like #new but the block sets the order.
|
103
|
+
#
|
104
|
+
def new_by(*args, &blk)
|
105
|
+
new(*args).order_by(&blk)
|
106
|
+
end
|
107
|
+
|
108
|
+
# Alternate to #new which creates a dictionary sorted by key.
|
109
|
+
#
|
110
|
+
# d = Dictionary.alpha
|
111
|
+
# d["z"] = 1
|
112
|
+
# d["y"] = 2
|
113
|
+
# d["x"] = 3
|
114
|
+
# d #=> {"x"=>3,"y"=>2,"z"=>2}
|
115
|
+
#
|
116
|
+
# This is equivalent to:
|
117
|
+
#
|
118
|
+
# Dictionary.new.order_by { |key,value| key }
|
119
|
+
def alpha(*args, &block)
|
120
|
+
new(*args, &block).order_by_key
|
121
|
+
end
|
122
|
+
|
123
|
+
# Alternate to #new which auto-creates sub-dictionaries as needed.
|
124
|
+
#
|
125
|
+
# d = Dictionary.auto
|
126
|
+
# d["a"]["b"]["c"] = "abc" #=> { "a"=>{"b"=>{"c"=>"abc"}}}
|
127
|
+
#
|
128
|
+
def auto(*args)
|
129
|
+
#AutoDictionary.new(*args)
|
130
|
+
leet = lambda { |hsh, key| hsh[key] = new(&leet) }
|
131
|
+
new(*args, &leet)
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
# New Dictiionary.
|
136
|
+
def initialize(*args, &blk)
|
137
|
+
@order = []
|
138
|
+
@order_by = nil
|
139
|
+
if blk
|
140
|
+
dict = self # This ensure autmatic key entry effect the
|
141
|
+
oblk = lambda{ |hsh, key| blk[dict,key] } # dictionary rather then just the interal hash.
|
142
|
+
@hash = Hash.new(*args, &oblk)
|
143
|
+
else
|
144
|
+
@hash = Hash.new(*args)
|
145
|
+
end
|
146
|
+
end
|
147
|
+
|
148
|
+
def order
|
149
|
+
reorder if @order_by
|
150
|
+
@order
|
151
|
+
end
|
152
|
+
|
153
|
+
# Keep dictionary sorted by a specific sort order.
|
154
|
+
def order_by( &block )
|
155
|
+
@order_by = block
|
156
|
+
order
|
157
|
+
self
|
158
|
+
end
|
159
|
+
|
160
|
+
# Keep dictionary sorted by key.
|
161
|
+
#
|
162
|
+
# d = Dictionary.new.order_by_key
|
163
|
+
# d["z"] = 1
|
164
|
+
# d["y"] = 2
|
165
|
+
# d["x"] = 3
|
166
|
+
# d #=> {"x"=>3,"y"=>2,"z"=>2}
|
167
|
+
#
|
168
|
+
# This is equivalent to:
|
169
|
+
#
|
170
|
+
# Dictionary.new.order_by { |key,value| key }
|
171
|
+
#
|
172
|
+
# The initializer Dictionary#alpha also provides this.
|
173
|
+
def order_by_key
|
174
|
+
@order_by = lambda { |k,v| k }
|
175
|
+
order
|
176
|
+
self
|
177
|
+
end
|
178
|
+
|
179
|
+
# Keep dictionary sorted by value.
|
180
|
+
#
|
181
|
+
# d = Dictionary.new.order_by_value
|
182
|
+
# d["z"] = 1
|
183
|
+
# d["y"] = 2
|
184
|
+
# d["x"] = 3
|
185
|
+
# d #=> {"x"=>3,"y"=>2,"z"=>2}
|
186
|
+
#
|
187
|
+
# This is equivalent to:
|
188
|
+
#
|
189
|
+
# Dictionary.new.order_by { |key,value| value }
|
190
|
+
def order_by_value
|
191
|
+
@order_by = lambda { |k,v| v }
|
192
|
+
order
|
193
|
+
self
|
194
|
+
end
|
195
|
+
|
196
|
+
#
|
197
|
+
def reorder
|
198
|
+
if @order_by
|
199
|
+
assoc = @order.collect{ |k| [k,@hash[k]] }.sort_by(&@order_by)
|
200
|
+
@order = assoc.collect{ |k,v| k }
|
201
|
+
end
|
202
|
+
@order
|
203
|
+
end
|
204
|
+
|
205
|
+
def ==(hsh2)
|
206
|
+
if hsh2.is_a?( Dictionary )
|
207
|
+
@order == hsh2.order &&
|
208
|
+
@hash == hsh2.instance_variable_get("@hash")
|
209
|
+
else
|
210
|
+
false
|
211
|
+
end
|
212
|
+
end
|
213
|
+
|
214
|
+
def [] k
|
215
|
+
@hash[ k ]
|
216
|
+
end
|
217
|
+
|
218
|
+
def fetch(k, *a, &b)
|
219
|
+
@hash.fetch(k, *a, &b)
|
220
|
+
end
|
221
|
+
|
222
|
+
# Store operator.
|
223
|
+
#
|
224
|
+
# h[key] = value
|
225
|
+
#
|
226
|
+
# Or with additional index.
|
227
|
+
#
|
228
|
+
# h[key,index] = value
|
229
|
+
def []=(k, i=nil, v=nil)
|
230
|
+
if v
|
231
|
+
insert(i,k,v)
|
232
|
+
else
|
233
|
+
store(k,i)
|
234
|
+
end
|
235
|
+
end
|
236
|
+
|
237
|
+
def insert( i,k,v )
|
238
|
+
@order.insert( i,k )
|
239
|
+
@hash.store( k,v )
|
240
|
+
end
|
241
|
+
|
242
|
+
def store( a,b )
|
243
|
+
@order.push( a ) unless @hash.has_key?( a )
|
244
|
+
@hash.store( a,b )
|
245
|
+
end
|
246
|
+
|
247
|
+
def clear
|
248
|
+
@order = []
|
249
|
+
@hash.clear
|
250
|
+
end
|
251
|
+
|
252
|
+
def delete( key )
|
253
|
+
@order.delete( key )
|
254
|
+
@hash.delete( key )
|
255
|
+
end
|
256
|
+
|
257
|
+
def each_key
|
258
|
+
order.each { |k| yield( k ) }
|
259
|
+
self
|
260
|
+
end
|
261
|
+
|
262
|
+
def each_value
|
263
|
+
order.each { |k| yield( @hash[k] ) }
|
264
|
+
self
|
265
|
+
end
|
266
|
+
|
267
|
+
def each
|
268
|
+
order.each { |k| yield( k,@hash[k] ) }
|
269
|
+
self
|
270
|
+
end
|
271
|
+
alias each_pair each
|
272
|
+
|
273
|
+
def delete_if
|
274
|
+
order.clone.each { |k| delete k if yield(k,@hash[k]) }
|
275
|
+
self
|
276
|
+
end
|
277
|
+
|
278
|
+
def values
|
279
|
+
ary = []
|
280
|
+
order.each { |k| ary.push @hash[k] }
|
281
|
+
ary
|
282
|
+
end
|
283
|
+
|
284
|
+
def keys
|
285
|
+
order
|
286
|
+
end
|
287
|
+
|
288
|
+
def invert
|
289
|
+
hsh2 = self.class.new
|
290
|
+
order.each { |k| hsh2[@hash[k]] = k }
|
291
|
+
hsh2
|
292
|
+
end
|
293
|
+
|
294
|
+
def reject( &block )
|
295
|
+
self.dup.delete_if &block
|
296
|
+
end
|
297
|
+
|
298
|
+
def reject!( &block )
|
299
|
+
hsh2 = reject &block
|
300
|
+
self == hsh2 ? nil : hsh2
|
301
|
+
end
|
302
|
+
|
303
|
+
def replace( hsh2 )
|
304
|
+
@order = hsh2.order
|
305
|
+
@hash = hsh2.hash
|
306
|
+
end
|
307
|
+
|
308
|
+
def shift
|
309
|
+
key = order.first
|
310
|
+
key ? [key,delete(key)] : super
|
311
|
+
end
|
312
|
+
|
313
|
+
def unshift( k,v )
|
314
|
+
unless @hash.include?( k )
|
315
|
+
@order.unshift( k )
|
316
|
+
@hash.store( k,v )
|
317
|
+
true
|
318
|
+
else
|
319
|
+
false
|
320
|
+
end
|
321
|
+
end
|
322
|
+
|
323
|
+
def <<(kv)
|
324
|
+
push *kv
|
325
|
+
end
|
326
|
+
|
327
|
+
def push( k,v )
|
328
|
+
unless @hash.include?( k )
|
329
|
+
@order.push( k )
|
330
|
+
@hash.store( k,v )
|
331
|
+
true
|
332
|
+
else
|
333
|
+
false
|
334
|
+
end
|
335
|
+
end
|
336
|
+
|
337
|
+
def pop
|
338
|
+
key = order.last
|
339
|
+
key ? [key,delete(key)] : nil
|
340
|
+
end
|
341
|
+
|
342
|
+
def inspect
|
343
|
+
ary = []
|
344
|
+
each {|k,v| ary << k.inspect + "=>" + v.inspect}
|
345
|
+
'{' + ary.join(", ") + '}'
|
346
|
+
end
|
347
|
+
|
348
|
+
def dup
|
349
|
+
a = []
|
350
|
+
each{ |k,v| a << k; a << v }
|
351
|
+
self.class[*a]
|
352
|
+
end
|
353
|
+
|
354
|
+
def update( hsh2 )
|
355
|
+
hsh2.each { |k,v| self[k] = v }
|
356
|
+
reorder
|
357
|
+
self
|
358
|
+
end
|
359
|
+
alias :merge! update
|
360
|
+
|
361
|
+
def merge( hsh2 )
|
362
|
+
self.dup.update(hsh2)
|
363
|
+
end
|
364
|
+
|
365
|
+
def select
|
366
|
+
ary = []
|
367
|
+
each { |k,v| ary << [k,v] if yield k,v }
|
368
|
+
ary
|
369
|
+
end
|
370
|
+
|
371
|
+
def reverse!
|
372
|
+
@order.reverse!
|
373
|
+
self
|
374
|
+
end
|
375
|
+
|
376
|
+
def reverse
|
377
|
+
dup.reverse!
|
378
|
+
end
|
379
|
+
|
380
|
+
def first
|
381
|
+
@hash[order.first]
|
382
|
+
end
|
383
|
+
|
384
|
+
def last
|
385
|
+
@hash[order.last]
|
386
|
+
end
|
387
|
+
|
388
|
+
def length
|
389
|
+
@order.length
|
390
|
+
end
|
391
|
+
alias :size :length
|
392
|
+
|
393
|
+
def empty?
|
394
|
+
@hash.empty?
|
395
|
+
end
|
396
|
+
|
397
|
+
def has_key?(key)
|
398
|
+
@hash.has_key?(key)
|
399
|
+
end
|
400
|
+
|
401
|
+
def key?(key)
|
402
|
+
@hash.key?(key)
|
403
|
+
end
|
404
|
+
|
405
|
+
def to_a
|
406
|
+
ary = []
|
407
|
+
each { |k,v| ary << [k,v] }
|
408
|
+
ary
|
409
|
+
end
|
410
|
+
|
411
|
+
def to_json
|
412
|
+
buf = "["
|
413
|
+
map do |k,v|
|
414
|
+
buf << k.to_json
|
415
|
+
buf << ", "
|
416
|
+
buf << v.to_json
|
417
|
+
end.join(", ")
|
418
|
+
buf << "]"
|
419
|
+
buf
|
420
|
+
end
|
421
|
+
|
422
|
+
def to_s
|
423
|
+
self.to_a.to_s
|
424
|
+
end
|
425
|
+
|
426
|
+
def to_hash
|
427
|
+
@hash.dup
|
428
|
+
end
|
429
|
+
|
430
|
+
def to_h
|
431
|
+
@hash.dup
|
432
|
+
end
|
433
|
+
end
|