Class: Bundler::Thor::CoreExt::OrderedHash

Inherits:
Hash
  • Object
show all
Includes:
Enumerable
Defined in:
lib/bundler/vendor/thor/lib/thor/core_ext/ordered_hash.rb,
lib/bundler/vendor/thor/lib/thor/core_ext/ordered_hash.rb

Overview

This class is based on the Ruby 1.9 ordered hashes.

It keeps the semantics and most of the efficiency of normal hashes while also keeping track of the order in which elements were set.

Defined Under Namespace

Classes: Node

Instance Method Summary collapse

Constructor Details

#initializeOrderedHash

Returns a new instance of OrderedHash.



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

def initialize
  @hash = {}
end

Instance Method Details

#[](key) ⇒ Object



21
22
23
# File 'lib/bundler/vendor/thor/lib/thor/core_ext/ordered_hash.rb', line 21

def [](key)
  @hash[key] && @hash[key].value
end

#[]=(key, value) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/bundler/vendor/thor/lib/thor/core_ext/ordered_hash.rb', line 25

def []=(key, value)
  if node = @hash[key] # rubocop:disable AssignmentInCondition
    node.value = value
  else
    node = Node.new(key, value)

    if !defined?(@first) || @first.nil?
      @first = @last = node
    else
      node.prev = @last
      @last.next = node
      @last = node
    end
  end

  @hash[key] = node
  value
end

#delete(key) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/bundler/vendor/thor/lib/thor/core_ext/ordered_hash.rb', line 44

def delete(key)
  if node = @hash[key] # rubocop:disable AssignmentInCondition
    prev_node = node.prev
    next_node = node.next

    next_node.prev = prev_node if next_node
    prev_node.next = next_node if prev_node

    @first = next_node if @first == node
    @last = prev_node  if @last  == node

    value = node.value
  end

  @hash.delete(key)
  value
end

#each {|[@first.key, @first.value]| ... } ⇒ Object

Yields:

  • ([@first.key, @first.value])


70
71
72
73
74
75
76
# File 'lib/bundler/vendor/thor/lib/thor/core_ext/ordered_hash.rb', line 70

def each
  return unless defined?(@first) && @first
  yield [@first.key, @first.value]
  node = @first
  yield [node.key, node.value] while node = node.next # rubocop:disable AssignmentInCondition
  self
end

#empty?Boolean

Returns:

  • (Boolean)


92
93
94
# File 'lib/bundler/vendor/thor/lib/thor/core_ext/ordered_hash.rb', line 92

def empty?
  @hash.empty?
end

#keysObject



62
63
64
# File 'lib/bundler/vendor/thor/lib/thor/core_ext/ordered_hash.rb', line 62

def keys
  map { |k, v| k }
end

#merge(other) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/bundler/vendor/thor/lib/thor/core_ext/ordered_hash.rb', line 78

def merge(other)
  hash = self.class.new

  each do |key, value|
    hash[key] = value
  end

  other.each do |key, value|
    hash[key] = value
  end

  hash
end

#valuesObject



66
67
68
# File 'lib/bundler/vendor/thor/lib/thor/core_ext/ordered_hash.rb', line 66

def values
  map { |k, v| v }
end