module CowProxy::Enumerable

A mixin to add mutable methods for keep_if, delete_if, select! and reject! to wrap block params with CowProxy

Public Instance Methods

delete_if(&block) click to toggle source

Deletes every element of self for which block evaluates to true.

@yield [item] Gives each element in self to the block @yieldparam item Wrapped item in self @yieldreturn [Boolean] true if item must be deleted @return [CowProxy::Array] self if block given @return [Enumerator] if no block given

# File lib/cow_proxy/enumerable.rb, line 37
def delete_if(&block)
  mutable_selector(:reject, &block)
end
keep_if(&block) click to toggle source

Invokes the given block passing in successive elements from self, deleting elements for which the block returns a false value.

@yield [item] Gives each element in self to the block @yieldparam item Wrapped item in self @yieldreturn [Boolean] true if item must be kept @return [CowProxy::Array] self if block given @return [Enumerator] if no block given

# File lib/cow_proxy/enumerable.rb, line 13
def keep_if(&block)
  mutable_selector(:select, &block)
end
reject!(&block) click to toggle source

Deletes every element of self for which block evaluates to true.

@yield [item] Gives each element in self to the block @yieldparam item Wrapped item in self @yieldreturn [Boolean] true if item must be deleted @return [CowProxy::Array] self if block given and changes were made @return [nil] if block given and no changes were made @return [Enumerator] if no block given

# File lib/cow_proxy/enumerable.rb, line 49
def reject!(&block)
  mutable_selector!(:delete_if, &block)
end
select!(&block) click to toggle source

Invokes the given block passing in successive elements from self, deleting elements for which the block returns a false value.

@yield [item] Gives each element in self to the block @yieldparam item Wrapped item in self @yieldreturn [Boolean] true if item must be kept @return [CowProxy::Array] self if block given and changes were made @return [nil] if block given and no changes were made @return [Enumerator] if no block given

# File lib/cow_proxy/enumerable.rb, line 26
def select!(&block)
  mutable_selector!(:keep_if, &block)
end

Private Instance Methods

mutable_selector(method, &block) click to toggle source
# File lib/cow_proxy/enumerable.rb, line 55
def mutable_selector(method, &block)
  return send(method) unless block
  @delegate_dc_obj = send(method, &block).tap do
    @dc_obj_duplicated = true
  end
  self
end
mutable_selector!(method, &block) click to toggle source
# File lib/cow_proxy/enumerable.rb, line 63
def mutable_selector!(method, &block)
  return send(method) unless block
  size = __getobj__.size
  send(method, &block)
  self unless __getobj__.size == size
end