File: instance_methods.rb

package info (click to toggle)
ruby-memoizable 0.4.2-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 192 kB
  • sloc: ruby: 605; makefile: 2
file content (49 lines) | stat: -rw-r--r-- 904 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# encoding: utf-8

module Memoizable

  # Methods mixed in to memoizable instances
  module InstanceMethods

    # Freeze the object
    #
    # @example
    #   object.freeze  # object is now frozen
    #
    # @return [Object]
    #
    # @api public
    def freeze
      memoized_method_cache  # initialize method cache
      super
    end

    # Sets a memoized value for a method
    #
    # @example
    #   object.memoize(hash: 12345)
    #
    # @param [Hash{Symbol => Object}] data
    #   the data to memoize
    #
    # @return [self]
    #
    # @api public
    def memoize(data)
      data.each { |name, value| memoized_method_cache[name] = value }
      self
    end

  private

    # The memoized method results
    #
    # @return [Hash]
    #
    # @api private
    def memoized_method_cache
      @_memoized_method_cache ||= Memory.new
    end

  end # InstanceMethods
end # Memoizable