Module: RedisHash::Enumerators

Extended by:
ActiveSupport::Concern
Included in:
Base
Defined in:
lib/redis_hash/concerns/enumerators.rb

Instance Method Summary collapse

Instance Method Details

#delete_ifObject



12
13
14
15
16
17
18
# File 'lib/redis_hash/concerns/enumerators.rb', line 12

def delete_if
  return enum_for(__method__) unless block_given?

  each { |field, value| delete(field) if yield(field, value) }

  to_h
end

#keep_ifObject



20
21
22
23
24
# File 'lib/redis_hash/concerns/enumerators.rb', line 20

def keep_if
  return enum_for(__method__) unless block_given?

  delete_if { |field, value| !yield(field, value) }
end

#reject!(&block) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/redis_hash/concerns/enumerators.rb', line 26

def reject!(&block)
  return enum_for(__method__) unless block_given?

  original = to_h
  delete_if(&block)
  current = to_h

  (original == current) ? nil : current
end

#select!Object



36
37
38
39
40
# File 'lib/redis_hash/concerns/enumerators.rb', line 36

def select!
  return enum_for(__method__) unless block_given?

  reject! { |*arguments| !yield(*arguments) }
end

#transform_values!Object



42
43
44
45
46
47
48
# File 'lib/redis_hash/concerns/enumerators.rb', line 42

def transform_values!
  return enum_for(__method__) unless block_given?

  each { |field, value| store(field, yield(value)) }

  to_h
end