wycats-merb-core 0.9.8
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/CHANGELOG +992 -0
- data/CONTRIBUTORS +94 -0
- data/LICENSE +20 -0
- data/PUBLIC_CHANGELOG +142 -0
- data/README +21 -0
- data/Rakefile +458 -0
- data/TODO +0 -0
- data/bin/merb +11 -0
- data/bin/merb-specs +5 -0
- data/lib/merb-core.rb +598 -0
- data/lib/merb-core/autoload.rb +31 -0
- data/lib/merb-core/bootloader.rb +717 -0
- data/lib/merb-core/config.rb +305 -0
- data/lib/merb-core/constants.rb +45 -0
- data/lib/merb-core/controller/abstract_controller.rb +568 -0
- data/lib/merb-core/controller/exceptions.rb +315 -0
- data/lib/merb-core/controller/merb_controller.rb +256 -0
- data/lib/merb-core/controller/mime.rb +107 -0
- data/lib/merb-core/controller/mixins/authentication.rb +123 -0
- data/lib/merb-core/controller/mixins/conditional_get.rb +83 -0
- data/lib/merb-core/controller/mixins/controller.rb +319 -0
- data/lib/merb-core/controller/mixins/render.rb +513 -0
- data/lib/merb-core/controller/mixins/responder.rb +469 -0
- data/lib/merb-core/controller/template.rb +254 -0
- data/lib/merb-core/core_ext.rb +9 -0
- data/lib/merb-core/core_ext/hash.rb +7 -0
- data/lib/merb-core/core_ext/kernel.rb +340 -0
- data/lib/merb-core/dispatch/cookies.rb +130 -0
- data/lib/merb-core/dispatch/default_exception/default_exception.rb +93 -0
- data/lib/merb-core/dispatch/default_exception/views/_css.html.erb +198 -0
- data/lib/merb-core/dispatch/default_exception/views/_javascript.html.erb +73 -0
- data/lib/merb-core/dispatch/default_exception/views/index.html.erb +94 -0
- data/lib/merb-core/dispatch/dispatcher.rb +176 -0
- data/lib/merb-core/dispatch/request.rb +729 -0
- data/lib/merb-core/dispatch/router.rb +151 -0
- data/lib/merb-core/dispatch/router/behavior.rb +566 -0
- data/lib/merb-core/dispatch/router/cached_proc.rb +52 -0
- data/lib/merb-core/dispatch/router/resources.rb +191 -0
- data/lib/merb-core/dispatch/router/route.rb +511 -0
- data/lib/merb-core/dispatch/session.rb +222 -0
- data/lib/merb-core/dispatch/session/container.rb +74 -0
- data/lib/merb-core/dispatch/session/cookie.rb +173 -0
- data/lib/merb-core/dispatch/session/memcached.rb +68 -0
- data/lib/merb-core/dispatch/session/memory.rb +99 -0
- data/lib/merb-core/dispatch/session/store_container.rb +150 -0
- data/lib/merb-core/dispatch/worker.rb +28 -0
- data/lib/merb-core/gem_ext/erubis.rb +77 -0
- data/lib/merb-core/logger.rb +203 -0
- data/lib/merb-core/plugins.rb +67 -0
- data/lib/merb-core/rack.rb +25 -0
- data/lib/merb-core/rack/adapter.rb +44 -0
- data/lib/merb-core/rack/adapter/ebb.rb +25 -0
- data/lib/merb-core/rack/adapter/evented_mongrel.rb +26 -0
- data/lib/merb-core/rack/adapter/fcgi.rb +17 -0
- data/lib/merb-core/rack/adapter/irb.rb +118 -0
- data/lib/merb-core/rack/adapter/mongrel.rb +26 -0
- data/lib/merb-core/rack/adapter/runner.rb +28 -0
- data/lib/merb-core/rack/adapter/swiftiplied_mongrel.rb +26 -0
- data/lib/merb-core/rack/adapter/thin.rb +39 -0
- data/lib/merb-core/rack/adapter/thin_turbo.rb +24 -0
- data/lib/merb-core/rack/adapter/webrick.rb +36 -0
- data/lib/merb-core/rack/application.rb +32 -0
- data/lib/merb-core/rack/handler/mongrel.rb +97 -0
- data/lib/merb-core/rack/middleware.rb +20 -0
- data/lib/merb-core/rack/middleware/conditional_get.rb +29 -0
- data/lib/merb-core/rack/middleware/content_length.rb +18 -0
- data/lib/merb-core/rack/middleware/csrf.rb +73 -0
- data/lib/merb-core/rack/middleware/path_prefix.rb +31 -0
- data/lib/merb-core/rack/middleware/profiler.rb +19 -0
- data/lib/merb-core/rack/middleware/static.rb +45 -0
- data/lib/merb-core/rack/middleware/tracer.rb +20 -0
- data/lib/merb-core/server.rb +284 -0
- data/lib/merb-core/tasks/audit.rake +68 -0
- data/lib/merb-core/tasks/gem_management.rb +229 -0
- data/lib/merb-core/tasks/merb.rb +1 -0
- data/lib/merb-core/tasks/merb_rake_helper.rb +80 -0
- data/lib/merb-core/tasks/stats.rake +71 -0
- data/lib/merb-core/test.rb +11 -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 +393 -0
- data/lib/merb-core/test/helpers/route_helper.rb +39 -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 +351 -0
- data/lib/merb-core/test/matchers/route_matchers.rb +137 -0
- data/lib/merb-core/test/matchers/view_matchers.rb +375 -0
- data/lib/merb-core/test/run_specs.rb +49 -0
- data/lib/merb-core/test/tasks/spectasks.rb +68 -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/test/test_ext/string.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 +342 -0
- data/lib/merb-core/version.rb +3 -0
- metadata +253 -0
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'open3'
|
3
|
+
require 'benchmark'
|
4
|
+
|
5
|
+
# Runs specs in all files matching the file pattern.
|
6
|
+
#
|
7
|
+
# ==== Parameters
|
8
|
+
# globs<String, Array[String]>:: File patterns to look for.
|
9
|
+
# spec_cmd<~to_s>:: The spec command. Defaults to "spec".
|
10
|
+
# run_opts<String>:: Options to pass to spec commands, for instance,
|
11
|
+
# if you want to use profiling formatter.
|
12
|
+
# except<Array[String]>:: File paths to skip.
|
13
|
+
def run_specs(globs, spec_cmd='spec', run_opts = "-c", except = [])
|
14
|
+
require "optparse"
|
15
|
+
require "spec"
|
16
|
+
globs = globs.is_a?(Array) ? globs : [globs]
|
17
|
+
examples, failures, errors, pending = 0, 0, 0, 0
|
18
|
+
|
19
|
+
time = Benchmark.measure do
|
20
|
+
globs.each do |glob|
|
21
|
+
(Dir[glob] - except).each do |spec|
|
22
|
+
STDOUT.puts "\n\nRunning #{spec}...\n"
|
23
|
+
response = Open3.popen3("#{spec_cmd} #{File.expand_path(spec)} #{run_opts}") do |i,o,e|
|
24
|
+
while out = o.gets
|
25
|
+
STDOUT.puts out
|
26
|
+
#STDOUT.flush
|
27
|
+
if out =~ /\d+ example/
|
28
|
+
e, f, p = out.match(/(\d+) examples?, (\d+) failures?(?:, (\d+) pending?)?/)[1..-1]
|
29
|
+
examples += e.to_i; failures += f.to_i; pending += p.to_i
|
30
|
+
end
|
31
|
+
end
|
32
|
+
errors += 1 if e.is_a?(IO)
|
33
|
+
STDOUT.puts e.read if e.is_a?(IO)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
puts
|
40
|
+
puts "*** TOTALS ***"
|
41
|
+
if failures == 0
|
42
|
+
print "\e[32m"
|
43
|
+
else
|
44
|
+
print "\e[31m"
|
45
|
+
end
|
46
|
+
puts "#{examples} examples, #{failures} failures, #{errors} errors, #{pending} pending, #{sprintf("suite run in %3.3f seconds", time.real)}"
|
47
|
+
# TODO: we need to report pending examples all together
|
48
|
+
print "\e[0m"
|
49
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
desc "Run specs, run a specific spec with TASK=spec/path_to_spec.rb"
|
2
|
+
task :spec => [ "spec:default" ]
|
3
|
+
|
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
|
+
|
12
|
+
Spec::Rake::SpecTask.new('default') do |t|
|
13
|
+
t.spec_opts = SPEC_OPTS
|
14
|
+
if(ENV['TASK'])
|
15
|
+
t.spec_files = [ENV['TASK']]
|
16
|
+
else
|
17
|
+
t.spec_files = Dir['spec/**/*_spec.rb'].sort
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
desc "Run all model specs, run a spec for a specific Model with MODEL=MyModel"
|
22
|
+
Spec::Rake::SpecTask.new('model') do |t|
|
23
|
+
t.spec_opts = SPEC_OPTS
|
24
|
+
if(ENV['MODEL'])
|
25
|
+
t.spec_files = Dir["spec/models/**/#{ENV['MODEL']}_spec.rb"].sort
|
26
|
+
else
|
27
|
+
t.spec_files = Dir['spec/models/**/*_spec.rb'].sort
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
desc "Run all controller specs, run a spec for a specific Controller with CONTROLLER=MyController"
|
32
|
+
Spec::Rake::SpecTask.new('controller') do |t|
|
33
|
+
t.spec_opts = SPEC_OPTS
|
34
|
+
if(ENV['CONTROLLER'])
|
35
|
+
t.spec_files = Dir["spec/controllers/**/#{ENV['CONTROLLER']}_spec.rb"].sort
|
36
|
+
else
|
37
|
+
t.spec_files = Dir['spec/controllers/**/*_spec.rb'].sort
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
desc "Run all view specs, run specs for a specific controller (and view) with CONTROLLER=MyController (VIEW=MyView)"
|
42
|
+
Spec::Rake::SpecTask.new('view') do |t|
|
43
|
+
t.spec_opts = SPEC_OPTS
|
44
|
+
if(ENV['CONTROLLER'] and ENV['VIEW'])
|
45
|
+
t.spec_files = Dir["spec/views/**/#{ENV['CONTROLLER']}/#{ENV['VIEW']}*_spec.rb"].sort
|
46
|
+
elsif(ENV['CONTROLLER'])
|
47
|
+
t.spec_files = Dir["spec/views/**/#{ENV['CONTROLLER']}/*_spec.rb"].sort
|
48
|
+
else
|
49
|
+
t.spec_files = Dir['spec/views/**/*_spec.rb'].sort
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
desc "Run all specs and output the result in html"
|
54
|
+
Spec::Rake::SpecTask.new('html') do |t|
|
55
|
+
t.spec_opts = ["--format", "html"]
|
56
|
+
t.libs = ['lib', 'server/lib' ]
|
57
|
+
t.spec_files = Dir['spec/**/*_spec.rb'].sort
|
58
|
+
end
|
59
|
+
|
60
|
+
desc "Run specs and check coverage with rcov"
|
61
|
+
Spec::Rake::SpecTask.new('coverage') do |t|
|
62
|
+
t.spec_opts = SPEC_OPTS
|
63
|
+
t.spec_files = Dir['spec/**/*_spec.rb'].sort
|
64
|
+
t.libs = ['lib', 'server/lib' ]
|
65
|
+
t.rcov = true
|
66
|
+
t.rcov_opts = ["--exclude 'config,spec,#{Gem::path.join(',')}'"]
|
67
|
+
end
|
68
|
+
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
|