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



134
135
136
# File 'lib/farce/memoize.rb', line 134

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



140
141
142
# File 'lib/farce/memoize.rb', line 140

def mode
  @mode
end

#scope ⇒ Symbol (readonly)

Returns the scope of the memoization.

Returns:

  • (Symbol) —

    the scope of the memoization



137
138
139
# File 'lib/farce/memoize.rb', line 137

def scope
  @scope
end

#visibility ⇒ Symbol (readonly)

Returns the visibility of the memoized methods.

Returns:

  • (Symbol) —

    the visibility of the memoized methods



143
144
145
# File 'lib/farce/memoize.rb', line 143

def visibility
  @visibility
end

Class Method Details

.new(*methods, scope: :global, mode: :smart, weak: false, visibility: nil) ⇒ Mixin

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

Parameters:

  • methods (Array<Symbol>) —

    The methods to memoize

  • scope (Symbol) (defaults to: :global) —

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

  • mode (Symbol) (defaults to: :smart) —

    The mode of the memoization. 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) (defaults to: false) —

    Whether to use weak memoization. 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) (defaults to: nil) —

    The visibility of the memoized methods. 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.

Returns:

  • (Mixin) —

    the memoization mixin



107
108
109
110
111
112
113
# File 'lib/farce/memoize.rb', line 107

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

#weak? ⇒ Boolean

Returns whether the memoization is weak.

Returns:

  • (Boolean) —

    whether the memoization is weak



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

def weak? = @weak