123456789_123456789_123456789_123456789_123456789_

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

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, Hash
Instance Chain:
self, Hash
Inherits: Hash
  • Object
Defined in: lib/bundler/vendor/thor/lib/thor/core_ext/ordered_hash.rb

Class Method Summary

Instance Method Summary

Constructor Details

.new(*args, &block) ⇒ OrderedHash

[ GitHub ]

  
# File 'lib/bundler/vendor/thor/lib/thor/core_ext/ordered_hash.rb', line 5

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

Instance Method Details

#[]=(key, value)

[ GitHub ]

  
# File 'lib/bundler/vendor/thor/lib/thor/core_ext/ordered_hash.rb', line 16

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

#clear

[ GitHub ]

  
# File 'lib/bundler/vendor/thor/lib/thor/core_ext/ordered_hash.rb', line 83

def clear
  super
  @keys.clear
  self
end

#delete(key)

[ GitHub ]

  
# File 'lib/bundler/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_if Also known as: #reject!

[ GitHub ]

  
# File 'lib/bundler/vendor/thor/lib/thor/core_ext/ordered_hash.rb', line 29

def delete_if
  super
  sync_keys!
  self
end

#each

[ GitHub ]

  
# File 'lib/bundler/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_key

[ GitHub ]

  
# File 'lib/bundler/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_pair

[ GitHub ]

  
# File 'lib/bundler/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_value

[ GitHub ]

  
# File 'lib/bundler/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)

[ GitHub ]

  
# File 'lib/bundler/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

#inspect

[ GitHub ]

  
# File 'lib/bundler/vendor/thor/lib/thor/core_ext/ordered_hash.rb', line 117

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

#keys

[ GitHub ]

  
# File 'lib/bundler/vendor/thor/lib/thor/core_ext/ordered_hash.rb', line 41

def keys
  @keys.dup
end

#merge(other_hash, &block)

[ GitHub ]

  
# File 'lib/bundler/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) Also known as: #update

[ GitHub ]

  
# File 'lib/bundler/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)

[ GitHub ]

  
# File 'lib/bundler/vendor/thor/lib/thor/core_ext/ordered_hash.rb', line 37

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

#reject!

Alias for #delete_if.

[ GitHub ]

  
# File 'lib/bundler/vendor/thor/lib/thor/core_ext/ordered_hash.rb', line 35

alias_method :reject!, :delete_if

#replace(other)

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

[ GitHub ]

  
# File 'lib/bundler/vendor/thor/lib/thor/core_ext/ordered_hash.rb', line 111

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

#select

[ GitHub ]

  
# File 'lib/bundler/vendor/thor/lib/thor/core_ext/ordered_hash.rb', line 81

alias_method :select, :find_all

#shift

[ GitHub ]

  
# File 'lib/bundler/vendor/thor/lib/thor/core_ext/ordered_hash.rb', line 89

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

#sync_keys! (private)

[ GitHub ]

  
# File 'lib/bundler/vendor/thor/lib/thor/core_ext/ordered_hash.rb', line 123

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

#to_a

[ GitHub ]

  
# File 'lib/bundler/vendor/thor/lib/thor/core_ext/ordered_hash.rb', line 53

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

#to_hash

[ GitHub ]

  
# File 'lib/bundler/vendor/thor/lib/thor/core_ext/ordered_hash.rb', line 49

def to_hash
  self
end

#update(other_hash)

Alias for #merge!.

[ GitHub ]

  
# File 'lib/bundler/vendor/thor/lib/thor/core_ext/ordered_hash.rb', line 104

alias_method :update, :merge!

#values

[ GitHub ]

  
# File 'lib/bundler/vendor/thor/lib/thor/core_ext/ordered_hash.rb', line 45

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