Class: Github::CoreExt::OrderedHash

Inherits:
Hash
  • Object
show all
Defined in:
lib/github_api/core_ext/ordered_hash.rb,
lib/github_api/core_ext/ordered_hash.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Hash

#deep_key?, #deep_keys, #deep_merge, #deep_merge!, #except, #except!, #serialize, #symbolize_keys, #symbolize_keys!

Constructor Details

#initialize(*args, &block) ⇒ OrderedHash

Returns a new instance of OrderedHash.


30
31
32
33
# File 'lib/github_api/core_ext/ordered_hash.rb', line 30

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

Instance Attribute Details

#orderObject

Returns the value of attribute order


10
11
12
# File 'lib/github_api/core_ext/ordered_hash.rb', line 10

def order
  @order
end

Class Method Details

.[](*args) ⇒ Object


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/github_api/core_ext/ordered_hash.rb', line 13

def [](*args)
  hsh = OrderedHash.new
  if Hash == args[0]
    hsh.replace args[0]
  elsif (args.size % 2) != 0
    pp args if ENV['DEBUG']
    raise ArgumentError, "odd number of elements for Hash"
  else
    0.step(args.size - 1, 2) do |a|
      b = a + 1
      hsh[args[a]] = args[b]
    end
  end
  hsh
end

Instance Method Details

#==(hsh2) ⇒ Object


40
41
42
43
# File 'lib/github_api/core_ext/ordered_hash.rb', line 40

def ==(hsh2)
  return false if @order != hsh2.order
  super hsh2
end

#[]=(key, value) ⇒ Object


35
36
37
38
# File 'lib/github_api/core_ext/ordered_hash.rb', line 35

def []=(key, value)
  @order.push key unless member?(key)
  super key, value
end

#__class__Object


100
101
102
# File 'lib/github_api/core_ext/ordered_hash.rb', line 100

def __class__
  OrderedHash
end

#classObject


96
97
98
# File 'lib/github_api/core_ext/ordered_hash.rb', line 96

def class
  Hash
end

#clearObject


45
46
47
48
# File 'lib/github_api/core_ext/ordered_hash.rb', line 45

def clear
  @order = []
  super
end

#delete(key) ⇒ Object


50
51
52
53
# File 'lib/github_api/core_ext/ordered_hash.rb', line 50

def delete(key)
  @order.delete key
  super
end

#delete_ifObject


71
72
73
74
# File 'lib/github_api/core_ext/ordered_hash.rb', line 71

def delete_if
  @order.clone.each { |k| delete k if yield }
  self
end

#eachObject Also known as: each_pair


65
66
67
68
# File 'lib/github_api/core_ext/ordered_hash.rb', line 65

def each
  @order.each { |k| yield k, self[k] }
  self
end

#each_keyObject


55
56
57
58
# File 'lib/github_api/core_ext/ordered_hash.rb', line 55

def each_key
  @order.each { |k| yield k }
  self
end

#each_valueObject


60
61
62
63
# File 'lib/github_api/core_ext/ordered_hash.rb', line 60

def each_value
  @order.each { |k| yield self[k] }
  self
end

#keysObject


82
83
84
# File 'lib/github_api/core_ext/ordered_hash.rb', line 82

def keys
  @order
end

#replace(hsh2) ⇒ Object


86
87
88
89
# File 'lib/github_api/core_ext/ordered_hash.rb', line 86

def replace(hsh2)
  @order = hsh2.keys
  super hsh2
end

#shiftObject


91
92
93
94
# File 'lib/github_api/core_ext/ordered_hash.rb', line 91

def shift
  key = @order.first
  key ? [key, delete(key)] : super
end

#valuesObject


76
77
78
79
80
# File 'lib/github_api/core_ext/ordered_hash.rb', line 76

def values
  ary = []
  @order.each { |k| ary.push self[k] }
  ary
end