Class: Doodle::OrderedHash

Inherits:
Hash
  • Object
show all
Defined in:
lib/molic_orderedhash.rb

Overview

AUTHOR

jan molic /mig/at/1984/dot/cz/

DESCRIPTION

Hash with preserved order and some array-like extensions
Public domain.

THANKS

Andrew Johnson for his suggestions and fixes of Hash[],
merge, to_a, inspect and shift

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*a, &b) ⇒ OrderedHash

Returns a new instance of OrderedHash.



28
29
30
31
# File 'lib/molic_orderedhash.rb', line 28

def initialize(*a, &b)
  super
  @order = []
end

Instance Attribute Details

#orderObject

Returns the value of attribute order.



13
14
15
# File 'lib/molic_orderedhash.rb', line 13

def order
  @order
end

#to_yaml_styleObject

Returns the value of attribute to_yaml_style.



166
167
168
# File 'lib/molic_orderedhash.rb', line 166

def to_yaml_style
  @to_yaml_style
end

Class Method Details

.[](*args) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/molic_orderedhash.rb', line 16

def [](*args)
  hsh = OrderedHash.new
  if Hash === args[0]
    hsh.replace args[0]
  elsif (args.size % 2) != 0
    raise ArgumentError, "odd number of elements for Hash"
  else
    hsh[args.shift] = args.shift while args.size > 0
  end
  hsh
end

Instance Method Details

#==(hsh2) ⇒ Object



51
52
53
54
55
# File 'lib/molic_orderedhash.rb', line 51

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

#classObject



162
163
164
# File 'lib/molic_orderedhash.rb', line 162

def class
  Hash
end

#clearObject

end



59
60
61
62
# File 'lib/molic_orderedhash.rb', line 59

def clear
  @order = []
  super
end

#cloneObject



32
33
34
35
36
# File 'lib/molic_orderedhash.rb', line 32

def clone
  c = super
  c.order = order.clone
  c
end

#delete(key) ⇒ Object



63
64
65
66
# File 'lib/molic_orderedhash.rb', line 63

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

#delete_ifObject



80
81
82
83
84
85
# File 'lib/molic_orderedhash.rb', line 80

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

#dupObject



37
38
39
40
41
# File 'lib/molic_orderedhash.rb', line 37

def dup
  c = super
  c.order = order.dup
  c
end

#eachObject Also known as: each_pair



75
76
77
78
# File 'lib/molic_orderedhash.rb', line 75

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

#each_keyObject



67
68
69
70
# File 'lib/molic_orderedhash.rb', line 67

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

#each_valueObject



71
72
73
74
# File 'lib/molic_orderedhash.rb', line 71

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

#inspectObject



144
145
146
147
148
# File 'lib/molic_orderedhash.rb', line 144

def inspect
  ary = []
  each {|k,v| ary << k.inspect + "=>" + v.inspect}
  '{' + ary.join(", ") + '}'
end

#invertObject



94
95
96
97
98
# File 'lib/molic_orderedhash.rb', line 94

def invert
  hsh2 = Hash.new
  @order.each { |k| hsh2[self[k]] = k }
  hsh2
end

#keysObject



91
92
93
# File 'lib/molic_orderedhash.rb', line 91

def keys
  @order
end

#merge(hsh2) ⇒ Object



154
155
156
# File 'lib/molic_orderedhash.rb', line 154

def merge(hsh2)
  self.dup.update(hsh2)
end

#orig_storeObject



45
# File 'lib/molic_orderedhash.rb', line 45

alias orig_store store

#popObject



132
133
134
135
# File 'lib/molic_orderedhash.rb', line 132

def pop
  key = @order.last
  key ? [key,delete(key)] : nil
end

#push(k, v) ⇒ Object



123
124
125
126
127
128
129
130
131
# File 'lib/molic_orderedhash.rb', line 123

def push(k,v)
  unless self.include? k
    @order.push k
    orig_store(k,v)
    true
  else
    false
  end
end

#reject(&block) ⇒ Object



99
100
101
# File 'lib/molic_orderedhash.rb', line 99

def reject(&block)
  self.dup.delete_if(&block)
end

#reject!(&block) ⇒ Object



102
103
104
105
# File 'lib/molic_orderedhash.rb', line 102

def reject!(&block)
  hsh2 = reject(&block)
  self == hsh2 ? nil : hsh2
end

#replace(hsh2) ⇒ Object



106
107
108
109
# File 'lib/molic_orderedhash.rb', line 106

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

#selectObject



157
158
159
160
161
# File 'lib/molic_orderedhash.rb', line 157

def select
  ary = []
  each { |k,v| ary << [k,v] if yield k,v }
  ary
end

#shiftObject



110
111
112
113
# File 'lib/molic_orderedhash.rb', line 110

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

#store(a, b) ⇒ Object Also known as: []=



46
47
48
49
# File 'lib/molic_orderedhash.rb', line 46

def store(a,b)
  @order.push a unless has_key? a
  super a,b
end

#store_only(a, b) ⇒ Object



42
43
44
# File 'lib/molic_orderedhash.rb', line 42

def store_only(a,b)
  store a,b
end

#to_aObject



136
137
138
139
140
# File 'lib/molic_orderedhash.rb', line 136

def to_a
  ary = []
  each { |k,v| ary << [k,v] }
  ary
end

#to_sObject



141
142
143
# File 'lib/molic_orderedhash.rb', line 141

def to_s
  self.to_a.to_s
end

#unshift(k, v) ⇒ Object



114
115
116
117
118
119
120
121
122
# File 'lib/molic_orderedhash.rb', line 114

def unshift(k,v)
  unless self.include? k
    @order.unshift k
    orig_store(k,v)
    true
  else
    false
  end
end

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



149
150
151
152
# File 'lib/molic_orderedhash.rb', line 149

def update(hsh2)
  hsh2.each { |k,v| self[k] = v }
  self
end

#valuesObject



86
87
88
89
90
# File 'lib/molic_orderedhash.rb', line 86

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

#yaml_inline!Object



192
# File 'lib/molic_orderedhash.rb', line 192

def yaml_inline!() self.yaml_inline = true end

#yaml_inline=(bool) ⇒ Object



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/molic_orderedhash.rb', line 167

def yaml_inline=(bool)
  if respond_to?("to_yaml_style")
    self.to_yaml_style = :inline
  else
    unless defined? @__yaml_inline_meth
      @__yaml_inline_meth =
        lambda {|opts|
        YAML::quick_emit(object_id, opts) {|emitter|
          emitter << '{ ' << map{|kv| kv.join ': '}.join(', ') << ' }'
        }
      }
      class << self
        def to_yaml opts = {}
          begin
            @__yaml_inline ? @__yaml_inline_meth[ opts ] : super
          rescue
            @to_yaml_style = :inline
            super
          end
        end
      end
    end
  end
  @__yaml_inline = bool
end