Class: Launchr::ActiveSupport::OrderedHash

Inherits:
Hash
  • Object
show all
Defined in:
lib/launchr/mixin/ordered_hash.rb

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.

Direct Known Subclasses

OrderedHash

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, &block) ⇒ OrderedHash

Returns a new instance of OrderedHash.



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

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

Class Method Details

.[](*args) ⇒ Object



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/launchr/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

#[]=(key, value) ⇒ Object



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

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

#clearObject



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

def clear
  super
  @keys.clear
  self
end

#delete(key) ⇒ Object



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

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

#delete_ifObject



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

def delete_if
  super
  sync_keys!
  self
end

#eachObject Also known as: each_pair



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

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

#each_keyObject



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

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

#each_valueObject



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

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

#initialize_copy(other) ⇒ Object



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

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

#inspectObject



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

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

#keysObject



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

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

#merge(other_hash) ⇒ Object



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

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

#merge!(other_hash) ⇒ Object



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

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

#reject(&block) ⇒ Object



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

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

#reject!Object



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

def reject!
  super
  sync_keys!
  self
end

#replace(other) ⇒ Object

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/launchr/mixin/ordered_hash.rb', line 153

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

#shiftObject



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

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

#store(key, value) ⇒ Object



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

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

#to_aObject



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

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

#to_hashObject



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

def to_hash
  self
end

#valuesObject



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

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