Class: Farce::Memoize::Mixin

Inherits:
Module
  • Object
show all
Defined in:
lib/farce/memoize.rb

Overview

Mixins used to implement memoization for different methods, scopes, and modes

See Also:

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#memoized_methods ⇒ Array<Symbol> (readonly)

Returns the memoized methods.

Returns:

  • (Array<Symbol>) —

    the memoized methods



149
150
151
# File 'lib/farce/memoize.rb', line 149

def memoized_methods
  @memoized_methods
end

#mode ⇒ Symbol? (readonly)

Returns the transfer mode, or nil for a scoped cache.

Returns:

  • (Symbol, nil) —

    the transfer mode, or nil for a scoped cache



155
156
157
# File 'lib/farce/memoize.rb', line 155

def mode
  @mode
end

#scope ⇒ Symbol (readonly)

Returns the scope of the memoization.

Returns:

  • (Symbol) —

    the scope of the memoization



152
153
154
# File 'lib/farce/memoize.rb', line 152

def scope
  @scope
end

#visibility ⇒ Symbol (readonly)

Returns the visibility of the memoized methods.

Returns:

  • (Symbol) —

    the visibility of the memoized methods



158
159
160
# File 'lib/farce/memoize.rb', line 158

def visibility
  @visibility
end

Class Method Details

.new(*methods, **options) ⇒ Mixin

Creates or retrieves a memoization mixin for the given methods and options.

Parameters:

  • methods (Array<Symbol>) —

    The methods to memoize

  • options (Hash) —

    The options for memoization

Options Hash (**options):

  • scope (Symbol) —

    The scope of the memoization. Defaults to :global. In addition to the normal scopes it may also be set to :global or :unshared.

  • mode (Symbol) —

    The mode of the memoization. Defaults to :smart. Ignored for any scope other than :global. Use :strict to cache shareable results directly, including with weak: true. Defaults to :raise with weak: true, which also supports :make_shareable and :dedup.

  • weak (Boolean) —

    Whether to use weak memoization. Defaults to false. If enabled, memoized values may be garbage collected when no longer referenced. In this case, next access to the memoized method may recompute the value.

  • visibility (Symbol, nil) —

    The visibility of the memoized methods. Defaults to nil. May be one of :public, :protected, or :private. If set to nil, Farce::Memoize#memoize will attempt to infer the visibility based on the context. Other methods will default to :public.

  • reset_copy (Boolean) —

    Give copies made with dup or clone fresh, empty memoization atoms. Defaults to false. By default, copies share their memoization atoms with the original.

  • add_keywords (Boolean) —

    Accept optional constructor keywords named after the memoized methods. Defaults to false. Supplied values prefill the cache before the original initializer runs, including nil and false. These keywords are consumed. Other arguments and the block are forwarded. Values use the configured transfer mode and are stored in the current scope.

Returns:

  • (Mixin) —

    the memoization mixin



120
121
122
123
124
125
126
# File 'lib/farce/memoize.rb', line 120

def self.new(*methods, **options)
  return super unless self == Mixin
  methods = Farce.dedup(methods.map(&:to_sym).uniq.sort)
  options = Farce.dedup(normalize_options(options))
  map     = KNOWN.store_if_absent(options) { Strict::Map.new }
  map.store_if_absent(methods) { super(methods, **options) }
end

Instance Method Details

#add_keywords? ⇒ Boolean

Returns whether constructor keywords can prefill the cache.

Returns:

  • (Boolean) —

    whether constructor keywords can prefill the cache



262
# File 'lib/farce/memoize.rb', line 262

def add_keywords? = @add_keywords

#reset_copy? ⇒ Boolean

Returns whether copies get fresh memoization atoms.

Returns:

  • (Boolean) —

    whether copies get fresh memoization atoms



259
# File 'lib/farce/memoize.rb', line 259

def reset_copy? = @reset_copy

#weak? ⇒ Boolean

Returns whether the memoization is weak.

Returns:

  • (Boolean) —

    whether the memoization is weak



256
# File 'lib/farce/memoize.rb', line 256

def weak? = @weak