Sha256: 9c88a028c2d2ed2e9a5dd00894d2dd972a79ffe02589d20bb204173ec0b6b661

Contents?: true

Size: 650 Bytes

Versions: 15

Compression:

Stored size: 650 Bytes

Contents

class SiteHub
  module Memoize
    def memoize(*methods)
      methods.each do |method|
        method_alias = "_#{method}"
        alias_method method_alias, method

        define_memoized_method(method, method_alias)
      end
    end

    private

    def define_memoized_method(method, method_alias)
      define_method(method) do |*args, &block|
        attribute = "@#{method}".gsub('?', 'question_mark')
        return instance_variable_get(attribute) if instance_variable_defined? attribute

        send(method_alias, *args, &block).tap do |result|
          instance_variable_set(attribute, result)
        end
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
sitehub-0.5.0.alpha12 lib/sitehub/memoize.rb
sitehub-0.4.10 lib/sitehub/memoize.rb
sitehub-0.5.0.alpha11 lib/sitehub/memoize.rb
sitehub-0.5.0.alpha10 lib/sitehub/memoize.rb
sitehub-0.5.0.alpha8 lib/sitehub/memoize.rb
sitehub-0.5.0.alpha7 lib/sitehub/memoize.rb
sitehub-0.5.0.alpha6 lib/sitehub/memoize.rb
sitehub-0.5.0.alpha5 lib/sitehub/memoize.rb
sitehub-0.5.0.alpha4 lib/sitehub/memoize.rb
sitehub-0.5.0.alpha3 lib/sitehub/memoize.rb
sitehub-0.5.0.alpha2 lib/sitehub/memoize.rb
sitehub-0.4.9 lib/sitehub/memoize.rb
sitehub-0.4.8 lib/sitehub/memoize.rb
sitehub-0.4.7 lib/sitehub/memoize.rb
sitehub-0.4.6 lib/sitehub/memoize.rb