Class: Helene::SuperHash

Inherits:
Object show all
Includes:
Enumerable
Defined in:
lib/helene/superhash.rb

Defined Under Namespace

Classes: ParentImmutableError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parents = [], default = nil) ⇒ SuperHash

Returns a new instance of SuperHash.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/helene/superhash.rb', line 7

def initialize parents = [], default = nil
  @hash = Hash.new default
  @parents =
    case parents
      when NilClass
        []
      when Array
        parents.flatten
      else
        [parents]
    end
  @parents.each do |parent|
    raise ArgumentError, parent.class.name unless parent.respond_to?('key?')
  end
end

Instance Attribute Details

#parentsObject (readonly)

Returns the value of attribute parents.



5
6
7
# File 'lib/helene/superhash.rb', line 5

def parents
  @parents
end

Instance Method Details

#==(other) ⇒ Object

methods that override Hash methods



43
44
45
46
47
48
49
# File 'lib/helene/superhash.rb', line 43

def ==(other)
  return false unless other.respond_to? :size and
                      size == other.size      and
                      other.respond_to? :[]
  each { |key, value| return false unless self[key] == other[key] }
  return true
end

#[](key) ⇒ Object



51
52
53
# File 'lib/helene/superhash.rb', line 51

def [](key)
  fetch(key) {default}
end

#[]=(key, value) ⇒ Object Also known as: store



55
56
57
# File 'lib/helene/superhash.rb', line 55

def []=(key, value)
  @hash[key] = value
end

#clearObject



60
61
62
# File 'lib/helene/superhash.rb', line 60

def clear
  delete_if {true}
end

#defaultObject



64
65
66
# File 'lib/helene/superhash.rb', line 64

def default
  @hash.default
end

#default=(value) ⇒ Object



68
69
70
# File 'lib/helene/superhash.rb', line 68

def default=(value)
  @hash.default = value
end

#delete(key) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/helene/superhash.rb', line 72

def delete(key)
  if key? key
    @hash.delete(key) do
      value = fetch(key)
      @hash[key] = default
      value
    end
  else
    block_given? ? (yield key) : default
  end
end

#delete_ifObject



84
85
86
87
88
89
90
# File 'lib/helene/superhash.rb', line 84

def delete_if
  each do |key, value|
    if yield key, value
      @hash.delete(key) { @hash[key] = default }
    end
  end
end

#eachObject Also known as: each_pair



92
93
94
95
# File 'lib/helene/superhash.rb', line 92

def each
  keys.each { |k| yield k, fetch(k) }
  self
end

#each_keyObject



98
99
100
101
# File 'lib/helene/superhash.rb', line 98

def each_key
  keys.each { |k| yield k }
  self
end

#each_valueObject



103
104
105
106
# File 'lib/helene/superhash.rb', line 103

def each_value
  keys.each { |k| yield fetch(k) }
  self
end

#empty?Boolean

Returns:

  • (Boolean)


108
109
110
# File 'lib/helene/superhash.rb', line 108

def empty?
  @hash.empty? && ( not @parents.find {|parent| not parent.empty?} )
end

#fetch(*args) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/helene/superhash.rb', line 112

def fetch(*args)
  case args.size
  when 1
    key, = args
    @hash.fetch(key) {
      @parents.each do |parent|
        begin
          return parent.fetch(key)
        rescue IndexError
        end
      end
      if block_given?
        yield key
      else
        raise IndexError, "key not found"
      end
    }
  when 2
    if block_given?
      raise ArgumentError, "wrong # of arguments"
    end
    key, default_object = args
    @hash.fetch(key) {
      @parents.each do |parent|
        begin
          return parent.fetch(key)
        rescue IndexError
        end
      end
      return default_object
    }
  else
    raise ArgumentError, "wrong # of arguments(#{args.size} for 2)"
  end
end

#has_value?(val) ⇒ Boolean Also known as: value?

Returns:

  • (Boolean)


148
149
150
151
# File 'lib/helene/superhash.rb', line 148

def has_value? val
  each { |k,v| return true if val == v }
  return false
end

#index(val) ⇒ Object



154
155
156
157
# File 'lib/helene/superhash.rb', line 154

def index val
  each { |k,v| return k if val == v }
  return false
end

#indexes(*ks) ⇒ Object Also known as: indices



159
160
161
# File 'lib/helene/superhash.rb', line 159

def indexes(*ks)
  ks.collect { |k| index k }
end

#inherits_key?(k) ⇒ Boolean

methods that are not overrides of Hash methods

Returns:

  • (Boolean)


25
26
27
# File 'lib/helene/superhash.rb', line 25

def inherits_key? k
  !(@hash.key? k) && (!! @parents.find {|parent| parent.key? k } )
end

#invertObject



164
165
166
167
168
# File 'lib/helene/superhash.rb', line 164

def invert
  h = {}
  keys.each { |k| h[fetch(k)] = k }
  h
end

#key?(k) ⇒ Boolean Also known as: has_key?, include?, member?

Returns:

  • (Boolean)


170
171
172
# File 'lib/helene/superhash.rb', line 170

def key? k
  (@hash.key? k) || (!! @parents.find {|parent| parent.key?(k)} )
end

#keysObject



177
178
179
# File 'lib/helene/superhash.rb', line 177

def keys
  (@hash.keys + (@parents.collect { |parent| parent.keys }).flatten).uniq
end

#ownObject



29
30
31
# File 'lib/helene/superhash.rb', line 29

def own
  @hash
end

#own_keysObject



33
34
35
# File 'lib/helene/superhash.rb', line 33

def own_keys
  @hash.keys
end

#owns_key?(k) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/helene/superhash.rb', line 37

def owns_key? k
  @hash.key? k
end

#rehashObject



181
182
183
184
185
# File 'lib/helene/superhash.rb', line 181

def rehash
  @hash.rehash
  @parents.each { |parent| parent.rehash if parent.respond_to? :rehash }
  self
end

#rejectObject



187
188
189
# File 'lib/helene/superhash.rb', line 187

def reject
  dup.delete_if { |k, v| yield k, v }   ## or is '&Proc.new' faster?
end

#reject!Object



191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/helene/superhash.rb', line 191

def reject!
  changed = false
  
  each do |key, value|
    if yield key, value
      changed = true
      @hash.delete(key) { @hash[key] = default }
    end
  end
  
  changed ? self : nil
end

#replace(hash) ⇒ Object



204
205
206
207
# File 'lib/helene/superhash.rb', line 204

def replace hash
  @hash.replace hash
  @parents.replace []
end

#shiftObject



211
212
213
214
215
216
217
# File 'lib/helene/superhash.rb', line 211

def shift
  if @hash.empty?
    raise ParentImmutableError, "Attempted to shift data out of parent"
  else
    @hash.shift
  end
end

#sizeObject Also known as: length



219
220
221
# File 'lib/helene/superhash.rb', line 219

def size
  keys.size
end

#sortObject



224
225
226
227
228
229
230
# File 'lib/helene/superhash.rb', line 224

def sort
  if block_given?
    to_a.sort { |x, y| yield x, y }   ## or is '&Proc.new' faster?
  else
    to_a.sort
  end
end

#to_aObject



232
233
234
# File 'lib/helene/superhash.rb', line 232

def to_a
  to_hash.to_a
end

#to_hashObject



236
237
238
239
240
# File 'lib/helene/superhash.rb', line 236

def to_hash
  h = {}
  keys.each { |k| h[k] = fetch(k) }
  h
end

#to_sObject



242
243
244
# File 'lib/helene/superhash.rb', line 242

def to_s
  to_hash.to_s
end

#update(h) ⇒ Object



246
247
248
249
# File 'lib/helene/superhash.rb', line 246

def update h
  @hash.update h
  self
end

#valuesObject



251
252
253
# File 'lib/helene/superhash.rb', line 251

def values
  keys.collect { |k| self[k] }
end