class IdempotentEnumerable::Configurator

Constants

REDEFINEABLE

Attributes

constructor[W]

Public Class Methods

new(host) click to toggle source
# File lib/idempotent_enumerable/configurator.rb, line 5
def initialize(host)
  @host = host
end

Public Instance Methods

constructor() click to toggle source
# File lib/idempotent_enumerable/configurator.rb, line 9
def constructor
  @constructor || :new
end
redefine_map!(only: REDEFINEABLE, all: nil) click to toggle source
# File lib/idempotent_enumerable/configurator.rb, line 15
def redefine_map!(only: REDEFINEABLE, all: nil)
  (Array(only) & REDEFINEABLE).each { |method| redefine(method, all) }
  self
end

Private Instance Methods

redefine(method, all) click to toggle source
# File lib/idempotent_enumerable/configurator.rb, line 22
def redefine(method, all)
  @host.send(:define_method, method) do |*arg, &block|
    return to_enum(method) unless block
    res = each(*arg).send(method, &block)
    if !all || res.all?(&all)
      idempotently_construct(res)
    else
      res
    end
  end
end