Sha256: 02e88c31036a5e3e9d7b22806b9a9b9ab2ed4c0e61609bfce152ecf1e00fa4bb

Contents?: true

Size: 578 Bytes

Versions: 9

Compression:

Stored size: 578 Bytes

Contents

# frozen_string_literal: true

module Memoize
  class ArgsError < StandardError; end

  module ClassMethods
    def memoize(name)
      ivar = :"@#{name.to_s.sub('?', '_predicate')}"
      prepend Module.new {
        define_method(name) do |*args|
          raise ArgsError, 'cannot pass arguments to memoized method %p' % name unless args.empty?
          return instance_variable_get(ivar) if instance_variable_defined?(ivar)

          instance_variable_set(ivar, super())
        end
      }
    end
  end

  def self.included(base)
    base.extend(ClassMethods)
  end
end

Version data entries

9 entries across 9 versions & 2 rubygems

Version Path
dpl-2.0.5.2.beta.1 lib/dpl/helper/memoize.rb
dpl-2.0.5.1 lib/dpl/helper/memoize.rb
dpl-2.0.5.beta.2.1 lib/dpl/helper/memoize.rb
dpl-2.0.5.beta.2 lib/dpl/helper/memoize.rb
dpl-2.0.5 lib/dpl/helper/memoize.rb
dpl-2.0.5.beta.1 lib/dpl/helper/memoize.rb
dpl-2.0.5.beta lib/dpl/helper/memoize.rb
travis_dpl_test-2.0.3.beta.4.ror.1 lib/dpl/helper/memoize.rb
travis_dpl_test-2.0.3.beta.4.ror lib/dpl/helper/memoize.rb