Module: IIIF::HashBehaviours

Extended by:
Forwardable
Included in:
Service
Defined in:
lib/iiif/hash_behaviours.rb

Constant Summary collapse

SIMPLE_SELF_RETURNERS =

Methods that take a block and should return an instance (self or a new’ instance) have been overridden to do so, rather than an’ IIIF::OrderedHash based on the internal hash

%w[delete_if each each_key each_value keep_if]

Instance Method Summary collapse

Instance Method Details

#clearObject

Clear is the only method that returns self but doesn’t accept a block



36
37
38
39
# File 'lib/iiif/hash_behaviours.rb', line 36

def clear
  @data.clear
  return self
end

#merge(another_obj) ⇒ Object

Returns a new instance of this class containing the contents of’ another_obj. The argument can be any object that implements two methods:

obj.each { |k,v| block }
obj.has_key?

If no block is specified, the value for entries with duplicate keys’ will be those of the argument, but at the index of the original; all’ other entries will be appended to the end.

If a block is specified the value for each duplicate key is determined’ by calling the block with the key, its value in hsh and its value in’ another_obj.



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/iiif/hash_behaviours.rb', line 55

def merge another_obj
  new_instance =  self.class.new
  # self.clone # Would this be better? What happens to other attributes of the class?
  if block_given?
    self.each do |k,v|
      if another_obj.has_key? k
        new_instance[k] = yield(k, self[k], another_obj[k])
      else
        new_instance[k] = v
      end
    end
  else
    self.each { |k,v| new_instance[k] = v }
    another_obj.each { |k,v| new_instance[k] = v }
  end
  new_instance
end

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

Adds the entries from another obj to this one. The argument can be any object that implements two methods:

obj.each { |k,v| block }
obj.has_key?

If no block is specified, the value for entries with duplicate keys’ will be those of the argument, but at the index of the original; all’ other entries will be appended to the end.

If a block is specified the value for each duplicate key is determined’ by calling the block with the key, its value in hsh and its value in’ another_obj.



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/iiif/hash_behaviours.rb', line 86

def merge! another_obj
  if block_given?
    self.each do |k,v|
      if another_obj.has_key? k
        self[k] = yield(k, self[k], another_obj[k])
      else
        self[k] = v
      end
    end
  else
    self.each { |k,v| self[k] = v }
    another_obj.each { |k,v| self[k] = v }
  end
  self
end

#reject!Object

Deletes entries for which the supplied block evaluates to true. Equivalent to #delete_if, but returns nil if there were no changes



105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/iiif/hash_behaviours.rb', line 105

def reject!
  if block_given?
    return_nil = true
    @data.each do |k, v|
      if yield(k, v)
        @data.delete(k)
        return_nil = false
      end
    end
    return return_nil ? nil : self
  else
    return self.data.reject!
  end
end

#selectObject

Returns a new instance consisting of entries for which the block returns true. Not that an enumerator is not available for the OrderedHash’ implementation



123
124
125
126
127
128
129
# File 'lib/iiif/hash_behaviours.rb', line 123

def select
  new_instance = self.class.new
  if block_given?
    @data.each { |k,v| new_instance.data[k] = v if yield(k,v) }
  end
  return new_instance
end

#select!Object

Deletes entries for which the supplied block evaluates to false. Equivalent to Hash#keep_if, but returns nil if no changes were made.



133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/iiif/hash_behaviours.rb', line 133

def select!
  if block_given?
    return_nil = true
    @data.each do |k,v|
      unless yield(k,v)
        @data.delete(k)
        return_nil = false
      end
    end
    return nil if return_nil
  end
  self
end