aboutsummaryrefslogtreecommitdiffstats
path: root/lib/bundler/vendor/thor/lib/thor/core_ext/ordered_hash.rb
blob: 76f1e43c65728acd1dfdccfa90a3f167a8aa4a15 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
class Bundler::Thor
  module CoreExt
    class OrderedHash < ::Hash
      if RUBY_VERSION < "1.9"
        def initialize(*args, &block)
          super
          @keys = []
        end

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

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

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

        def delete_if
          super
          sync_keys!
          self
        end

        alias_method :reject!, :delete_if

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

        def keys
          @keys.dup
        end

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

        def to_hash
          self
        end

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

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

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

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

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

        alias_method :select, :find_all

        def clear
          super
          @keys.clear
          self
        end

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

        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

        alias_method :update, :merge!

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

        # When replacing with another hash, the initial order of our keys must come from the other hash -ordered or not.
        def replace(other)
          super
          @keys = other.keys
          self
        end

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

        private

        def sync_keys!
          @keys.delete_if { |k| !key?(k) }
        end
      end
    end
  end
end