Class: Plist4r::ActiveSupport::OrderedHash

Defined in:
lib/plist4r/mixin/ordered_hash.rb

Direct Known Subclasses

Class Methods

Instance Methods

Methods inherited from Hash

#merge_array_of_hashes_of_arrays, #merge_array_of_hashes_of_arrays!

Overview

  ActiveSupport::OrderedHash

  Copyright (c) 2005 David Hansson,
  Copyright (c) 2007 Mauricio Fernandez, Sam Stephenson
  Copyright (c) 2008 Steve Purcell, Josh Peek
  Copyright (c) 2009 Christoffer Sawicki

  Permission is hereby granted, free of charge, to any person obtaining
  a copy of this software and associated documentation files (the
  "Software"), to deal in the Software without restriction, including
  without limitation the rights to use, copy, modify, merge, publish,
  distribute, sublicense, and/or sell copies of the Software, and to
  permit persons to whom the Software is furnished to do so, subject to
  the following conditions:

  The above copyright notice and this permission notice shall be
  included in all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Constructor Details

- (OrderedHash) initialize(*args, &block)

A new instance of OrderedHash



32
33
34
35
# File 'lib/plist4r/mixin/ordered_hash.rb', line 32

def initialize(*args, &block)
  super
  @keys = []
end

Class Method Details

+ (Object) [](*args)



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/plist4r/mixin/ordered_hash.rb', line 37

def self.[](*args)
  ordered_hash = new

  if (args.length == 1 && args.first.is_a?(Array))
    args.first.each do |key_value_pair|
      next unless (key_value_pair.is_a?(Array))
      ordered_hash[key_value_pair[0]] = key_value_pair[1]
    end

    return ordered_hash
  end

  unless (args.size % 2 == 0)
    raise ArgumentError.new("odd number of arguments for Hash")
  end

  args.each_with_index do |val, ind|
    next if (ind % 2 != 0)
    ordered_hash[val] = args[ind + 1]
  end

  ordered_hash
end

Instance Method Details

- (Object) []=(key, value)



72
73
74
75
# File 'lib/plist4r/mixin/ordered_hash.rb', line 72

def []=(key, value)
  @keys << key if !has_key?(key)
  super
end

- (Object) clear



131
132
133
134
135
# File 'lib/plist4r/mixin/ordered_hash.rb', line 131

def clear
  super
  @keys.clear
  self
end

- (Object) delete(key)



77
78
79
80
81
82
83
# File 'lib/plist4r/mixin/ordered_hash.rb', line 77

def delete(key)
  if has_key? key
    index = @keys.index(key)
    @keys.delete_at index
  end
  super
end

- (Object) delete_if



85
86
87
88
89
# File 'lib/plist4r/mixin/ordered_hash.rb', line 85

def delete_if
  super
  sync_keys!
  self
end

- (Object) each Also known as: each_pair



125
126
127
# File 'lib/plist4r/mixin/ordered_hash.rb', line 125

def each
  @keys.each {|key| yield [key, self[key]]}
end

- (Object) each_key



117
118
119
# File 'lib/plist4r/mixin/ordered_hash.rb', line 117

def each_key
  @keys.each { |key| yield key }
end

- (Object) each_value



121
122
123
# File 'lib/plist4r/mixin/ordered_hash.rb', line 121

def each_value
  @keys.each { |key| yield self[key]}
end

- (Object) initialize_copy(other)



61
62
63
64
65
# File 'lib/plist4r/mixin/ordered_hash.rb', line 61

def initialize_copy(other)
  super
  # make a deep copy of keys
  @keys = other.keys
end

- (Object) inspect



159
160
161
# File 'lib/plist4r/mixin/ordered_hash.rb', line 159

def inspect
  "#<OrderedHash #{super}>"
end

- (Object) keys



101
102
103
# File 'lib/plist4r/mixin/ordered_hash.rb', line 101

def keys
  (@keys || []).dup
end

- (Object) merge(other_hash)



148
149
150
# File 'lib/plist4r/mixin/ordered_hash.rb', line 148

def merge(other_hash)
  dup.merge!(other_hash)
end

- (Object) merge!(other_hash)



143
144
145
146
# File 'lib/plist4r/mixin/ordered_hash.rb', line 143

def merge!(other_hash)
  other_hash.each {|k,v| self[k] = v }
  self
end

- (Object) reject(&block)



97
98
99
# File 'lib/plist4r/mixin/ordered_hash.rb', line 97

def reject(&block)
  dup.reject!(&block)
end

- (Object) reject!



91
92
93
94
95
# File 'lib/plist4r/mixin/ordered_hash.rb', line 91

def reject!
  super
  sync_keys!
  self
end

- (Object) replace(other)

When replacing with another hash, the initial order of our keys must come from the other hash -ordered or not.



153
154
155
156
157
# File 'lib/plist4r/mixin/ordered_hash.rb', line 153

def replace(other)
  super
  @keys = other.keys
  self
end

- (Object) shift



137
138
139
140
141
# File 'lib/plist4r/mixin/ordered_hash.rb', line 137

def shift
  k = @keys.first
  v = delete(k)
  [k, v]
end

- (Object) store(key, value)



67
68
69
70
# File 'lib/plist4r/mixin/ordered_hash.rb', line 67

def store(key, value)
  @keys << key if !has_key?(key)
  super
end

- (Object) to_a



113
114
115
# File 'lib/plist4r/mixin/ordered_hash.rb', line 113

def to_a
  @keys.map { |key| [ key, self[key] ] }
end

- (Object) to_hash



109
110
111
# File 'lib/plist4r/mixin/ordered_hash.rb', line 109

def to_hash
  self
end

- (Object) values



105
106
107
# File 'lib/plist4r/mixin/ordered_hash.rb', line 105

def values
  @keys.collect { |key| self[key] }
end