Class: Foreman::Thor::CoreExt::OrderedHash

Inherits:
Hash
  • Object
show all
Defined in:
lib/foreman/vendor/thor/lib/thor/core_ext/ordered_hash.rb

Instance Method Summary collapse

Constructor Details

#initialize(*args, &block) ⇒ OrderedHash

Returns a new instance of OrderedHash.



5
6
7
8
# File 'lib/foreman/vendor/thor/lib/thor/core_ext/ordered_hash.rb', line 5

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

Instance Method Details

#[]=(key, value) ⇒ Object



16
17
18
19
# File 'lib/foreman/vendor/thor/lib/thor/core_ext/ordered_hash.rb', line 16

def []=(key, value)
  @keys << key unless key?(key)
  super
end

#clearObject



83
84
85
86
87
# File 'lib/foreman/vendor/thor/lib/thor/core_ext/ordered_hash.rb', line 83

def clear
  super
  @keys.clear
  self
end

#delete(key) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/foreman/vendor/thor/lib/thor/core_ext/ordered_hash.rb', line 21

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

#delete_ifObject Also known as: reject!



29
30
31
32
33
# File 'lib/foreman/vendor/thor/lib/thor/core_ext/ordered_hash.rb', line 29

def delete_if
  super
  sync_keys!
  self
end

#eachObject



69
70
71
72
73
# File 'lib/foreman/vendor/thor/lib/thor/core_ext/ordered_hash.rb', line 69

def each
  return to_enum(:each) unless block_given?
  @keys.each { |key| yield([key, self[key]]) }
  self
end

#each_keyObject



57
58
59
60
61
# File 'lib/foreman/vendor/thor/lib/thor/core_ext/ordered_hash.rb', line 57

def each_key
  return to_enum(:each_key) unless block_given?
  @keys.each { |key| yield(key) }
  self
end

#each_pairObject



75
76
77
78
79
# File 'lib/foreman/vendor/thor/lib/thor/core_ext/ordered_hash.rb', line 75

def each_pair
  return to_enum(:each_pair) unless block_given?
  @keys.each { |key| yield(key, self[key]) }
  self
end

#each_valueObject



63
64
65
66
67
# File 'lib/foreman/vendor/thor/lib/thor/core_ext/ordered_hash.rb', line 63

def each_value
  return to_enum(:each_value) unless block_given?
  @keys.each { |key| yield(self[key]) }
  self
end

#initialize_copy(other) ⇒ Object



10
11
12
13
14
# File 'lib/foreman/vendor/thor/lib/thor/core_ext/ordered_hash.rb', line 10

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

#inspectObject



117
118
119
# File 'lib/foreman/vendor/thor/lib/thor/core_ext/ordered_hash.rb', line 117

def inspect
  "#<#{self.class} #{super}>"
end

#keysObject



41
42
43
# File 'lib/foreman/vendor/thor/lib/thor/core_ext/ordered_hash.rb', line 41

def keys
  @keys.dup
end

#merge(other_hash, &block) ⇒ Object



106
107
108
# File 'lib/foreman/vendor/thor/lib/thor/core_ext/ordered_hash.rb', line 106

def merge(other_hash, &block)
  dup.merge!(other_hash, &block)
end

#merge!(other_hash) ⇒ Object Also known as: update



95
96
97
98
99
100
101
102
# File 'lib/foreman/vendor/thor/lib/thor/core_ext/ordered_hash.rb', line 95

def merge!(other_hash)
  if block_given?
    other_hash.each { |k, v| self[k] = key?(k) ? yield(k, self[k], v) : v }
  else
    other_hash.each { |k, v| self[k] = v }
  end
  self
end

#reject(&block) ⇒ Object



37
38
39
# File 'lib/foreman/vendor/thor/lib/thor/core_ext/ordered_hash.rb', line 37

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

#replace(other) ⇒ Object

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



111
112
113
114
115
# File 'lib/foreman/vendor/thor/lib/thor/core_ext/ordered_hash.rb', line 111

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

#shiftObject



89
90
91
92
93
# File 'lib/foreman/vendor/thor/lib/thor/core_ext/ordered_hash.rb', line 89

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

#to_aObject



53
54
55
# File 'lib/foreman/vendor/thor/lib/thor/core_ext/ordered_hash.rb', line 53

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

#to_hashObject



49
50
51
# File 'lib/foreman/vendor/thor/lib/thor/core_ext/ordered_hash.rb', line 49

def to_hash
  self
end

#valuesObject



45
46
47
# File 'lib/foreman/vendor/thor/lib/thor/core_ext/ordered_hash.rb', line 45

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