2008-12-17 16:49:06 +00:00
|
|
|
# --
|
2009-01-06 15:51:01 +00:00
|
|
|
# Copyright (C) 2008-2009 10gen Inc.
|
2008-12-17 16:43:08 +00:00
|
|
|
#
|
2009-02-15 13:24:14 +00:00
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
2008-12-17 16:43:08 +00:00
|
|
|
#
|
2009-02-15 13:24:14 +00:00
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
2008-12-17 16:43:08 +00:00
|
|
|
#
|
2009-02-15 13:24:14 +00:00
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
2008-12-17 16:49:06 +00:00
|
|
|
# ++
|
2008-12-17 16:43:08 +00:00
|
|
|
|
|
|
|
# A hash in which the order of keys are preserved.
|
2009-01-12 21:28:29 +00:00
|
|
|
#
|
|
|
|
# Under Ruby 1.9 and greater, this class has no added methods because Ruby's
|
|
|
|
# Hash already keeps its keys ordered by order of insertion.
|
2008-12-08 16:38:42 +00:00
|
|
|
class OrderedHash < Hash
|
|
|
|
|
2009-01-12 21:28:29 +00:00
|
|
|
# We only need the body of this class if the RUBY_VERSION is before 1.9
|
|
|
|
if RUBY_VERSION < '1.9'
|
|
|
|
|
2008-12-08 16:38:42 +00:00
|
|
|
attr_accessor :ordered_keys
|
|
|
|
|
|
|
|
def keys
|
|
|
|
@ordered_keys || []
|
|
|
|
end
|
|
|
|
|
|
|
|
def []=(key, value)
|
|
|
|
@ordered_keys ||= []
|
|
|
|
@ordered_keys << key unless @ordered_keys.include?(key)
|
|
|
|
super(key, value)
|
|
|
|
end
|
|
|
|
|
2009-03-05 18:48:15 +00:00
|
|
|
def ==(other)
|
|
|
|
!other.nil? &&
|
|
|
|
keys == other.keys &&
|
|
|
|
values == other.values
|
|
|
|
end
|
|
|
|
|
2008-12-08 16:38:42 +00:00
|
|
|
def each
|
|
|
|
@ordered_keys ||= []
|
|
|
|
@ordered_keys.each { |k| yield k, self[k] }
|
|
|
|
end
|
|
|
|
|
2009-01-08 12:16:25 +00:00
|
|
|
def values
|
|
|
|
collect { |k, v| v }
|
|
|
|
end
|
|
|
|
|
2008-12-08 16:38:42 +00:00
|
|
|
def merge(other)
|
2009-01-08 12:16:25 +00:00
|
|
|
oh = self.dup
|
|
|
|
oh.merge!(other)
|
|
|
|
oh
|
|
|
|
end
|
|
|
|
|
|
|
|
def merge!(other)
|
2008-12-08 16:38:42 +00:00
|
|
|
@ordered_keys ||= []
|
|
|
|
@ordered_keys += other.keys # unordered if not an OrderedHash
|
2009-01-08 12:16:25 +00:00
|
|
|
@ordered_keys.uniq!
|
2008-12-08 16:38:42 +00:00
|
|
|
super(other)
|
|
|
|
end
|
|
|
|
|
2009-01-09 18:54:12 +00:00
|
|
|
def inspect
|
|
|
|
str = '{'
|
2009-01-10 00:49:27 +00:00
|
|
|
str << (@ordered_keys || []).collect { |k| "\"#{k}\"=>#{self.[](k).inspect}" }.join(", ")
|
2009-01-09 18:54:12 +00:00
|
|
|
str << '}'
|
|
|
|
end
|
|
|
|
|
2009-01-21 15:53:26 +00:00
|
|
|
def delete(key, &block)
|
2009-01-26 18:30:42 +00:00
|
|
|
@ordered_keys.delete(key) if @ordered_keys
|
2009-01-21 15:53:26 +00:00
|
|
|
super
|
|
|
|
end
|
|
|
|
|
|
|
|
def delete_if(&block)
|
|
|
|
self.each { |k,v|
|
|
|
|
if yield k, v
|
|
|
|
delete(k)
|
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def clear
|
|
|
|
super
|
|
|
|
@ordered_keys = []
|
|
|
|
end
|
|
|
|
|
2009-01-12 21:28:29 +00:00
|
|
|
end # Ruby before 1.9
|
|
|
|
|
2008-12-08 16:38:42 +00:00
|
|
|
end
|