Module: Hammerspace::HashMethods

Included in:
Backend::Base
Defined in:
lib/hammerspace/hash_methods.rb

Overview

Basic implementations of most methods supported by Ruby’s hash

Analogous to Enumerable. Mixed into Hammerspace::Backend::Base. A backend need only implement four of these methods: [], []=, delete, and each. (A backend should also implement close and uid, but these are not hash methods; they are hammerspace-specific.) However, a backend may choose to override some of the default implementations if the backend is able to implement the methods more efficiently.

Instance Method Summary collapse

Instance Method Details

#==(hash) ⇒ Object



13
14
15
16
17
18
19
20
# File 'lib/hammerspace/hash_methods.rb', line 13

def ==(hash)
  return false if size != hash.size
  each do |key, value|
    return false unless hash.has_key?(key)
    return false unless hash[key] == value
  end
  true
end

#[](key) ⇒ Object

Raises:

  • (NotImplementedError)


22
23
24
# File 'lib/hammerspace/hash_methods.rb', line 22

def [](key)
  raise NotImplementedError
end

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

Raises:

  • (NotImplementedError)


26
27
28
# File 'lib/hammerspace/hash_methods.rb', line 26

def []=(key, value)
  raise NotImplementedError
end

#assoc(key) ⇒ Object



30
31
32
# File 'lib/hammerspace/hash_methods.rb', line 30

def assoc(key)
  find { |k,v| k == key }
end

#clearObject



34
35
36
37
38
# File 'lib/hammerspace/hash_methods.rb', line 34

def clear
  each { |key, value| delete(key) }
  close # flush immediately
  frontend
end

#delete(key) ⇒ Object

Raises:

  • (NotImplementedError)


40
41
42
# File 'lib/hammerspace/hash_methods.rb', line 40

def delete(key)
  raise NotImplementedError
end

#delete_if(&block) ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/hammerspace/hash_methods.rb', line 44

def delete_if(&block)
  if block_given?
    reject!(&block)
    frontend
  else
    reject!
  end
end

#each(&block) ⇒ Object Also known as: each_pair

Raises:

  • (NotImplementedError)


53
54
55
# File 'lib/hammerspace/hash_methods.rb', line 53

def each(&block)
  raise NotImplementedError
end

#each_keyObject



57
58
59
60
61
62
63
# File 'lib/hammerspace/hash_methods.rb', line 57

def each_key
  if block_given?
    each { |key, value| yield key }
  else
    Enumerator.new { |y| each { |key, value| y << key } }
  end
end

#each_valueObject



65
66
67
68
69
70
71
# File 'lib/hammerspace/hash_methods.rb', line 65

def each_value
  if block_given?
    each { |key, value| yield value }
  else
    Enumerator.new { |y| each { |key, value| y << value } }
  end
end

#empty?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/hammerspace/hash_methods.rb', line 73

def empty?
  size == 0
end

#eql?(hash) ⇒ Boolean

Returns:

  • (Boolean)


77
78
79
80
81
82
83
84
# File 'lib/hammerspace/hash_methods.rb', line 77

def eql?(hash)
  return false if size != hash.size
  each do |key, value|
    return false unless hash.has_key?(key)
    return false unless hash[key].eql?(value)
  end
  true
end

#fetch(key, *args) ⇒ Object

Raises:

  • (ArgumentError)


86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/hammerspace/hash_methods.rb', line 86

def fetch(key, *args)
  raise ArgumentError, "wrong number of arguments" if args.size > 1

  return self[key] if has_key?(key)

  if block_given?
    yield key
  elsif args.size == 1
    args.first
  else
    raise KeyError, "key not found: \"#{key}\""
  end
end

#flatten(*args) ⇒ Object

Raises:

  • (ArgumentError)


100
101
102
103
104
105
106
107
108
109
110
# File 'lib/hammerspace/hash_methods.rb', line 100

def flatten(*args)
  # Note: the optional level argument is supported for compatibility, but
  # it will never have an effect because only string values are
  # supported.
  raise ArgumentError, "wrong number of arguments" if args.size > 1

  each_with_object([]) do |args, array|
    array << args.first
    array << args.last
  end
end

#has_key?(key) ⇒ Boolean Also known as: key?

Returns:

  • (Boolean)


112
113
114
# File 'lib/hammerspace/hash_methods.rb', line 112

def has_key?(key)
  !!find { |k,v| k.eql?(key) }
end

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

Returns:

  • (Boolean)


116
117
118
# File 'lib/hammerspace/hash_methods.rb', line 116

def has_value?(value)
  !!find { |k,v| v == value }
end

#include?(key) ⇒ Boolean

alias_method seems to conflict with Enumerable’s version of these methods

Returns:

  • (Boolean)


225
# File 'lib/hammerspace/hash_methods.rb', line 225

def include?(key); has_key?(key); end

#keep_if(&block) ⇒ Object



120
121
122
123
124
125
126
127
# File 'lib/hammerspace/hash_methods.rb', line 120

def keep_if(&block)
  if block_given?
    select!(&block)
    frontend
  else
    select!
  end
end

#key(key) ⇒ Object



129
130
131
# File 'lib/hammerspace/hash_methods.rb', line 129

def key(key)
  has_key?(key) ? self[key] : nil
end

#keysObject



133
134
135
# File 'lib/hammerspace/hash_methods.rb', line 133

def keys
  each.map { |key, value| key }
end

#member?(key) ⇒ Boolean

Returns:

  • (Boolean)


226
# File 'lib/hammerspace/hash_methods.rb', line 226

def member?(key); has_key?(key); end

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



137
138
139
140
141
142
143
144
145
146
# File 'lib/hammerspace/hash_methods.rb', line 137

def merge!(hash)
  hash.each do |key, value|
    if block_given?
      self[key] = yield key, self[key], value
    else
      self[key] = value
    end
  end
  frontend
end

#rassoc(value) ⇒ Object



148
149
150
# File 'lib/hammerspace/hash_methods.rb', line 148

def rassoc(value)
  find { |k,v| v == value }
end

#reject!Object



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/hammerspace/hash_methods.rb', line 152

def reject!
  if block_given?
    any_deleted = false
    each do |key, value|
      if yield key, value
        any_deleted = true
        delete(key)
      end
    end
    any_deleted ? frontend : nil
  else
    Enumerator.new do |y|
      each { |key, value| delete(key) if y.yield(key, value) }
    end
  end
end

#replace(hash) ⇒ Object Also known as: initialize_copy



169
170
171
172
# File 'lib/hammerspace/hash_methods.rb', line 169

def replace(hash)
  clear
  merge!(hash)
end

#select!Object



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/hammerspace/hash_methods.rb', line 174

def select!
  if block_given?
    any_deleted = false
    each do |key, value|
      unless yield key, value
        any_deleted = true
        delete(key)
      end
    end
    any_deleted ? frontend : nil
  else
    Enumerator.new do |y|
      each { |key, value| delete(key) unless y.yield(key, value) }
    end
  end
end

#shiftObject



191
192
193
194
195
196
197
198
199
200
# File 'lib/hammerspace/hash_methods.rb', line 191

def shift
  items = take(1)
  if items.empty?
    frontend.default
  else
    pair = items.first
    delete(pair.first)
    pair
  end
end

#sizeObject Also known as: length



202
203
204
205
206
# File 'lib/hammerspace/hash_methods.rb', line 202

def size
  count = 0
  each { |key, value| count += 1 }
  count
end

#to_hashObject



208
209
210
# File 'lib/hammerspace/hash_methods.rb', line 208

def to_hash
  each_with_object({}) { |args, hash| hash[args.first] = args.last }
end

#valuesObject



212
213
214
# File 'lib/hammerspace/hash_methods.rb', line 212

def values
  each.map { |key, value| value }
end

#values_at(*args) ⇒ Object



216
217
218
# File 'lib/hammerspace/hash_methods.rb', line 216

def values_at(*args)
  args.map { |key| self[key] }
end