Class: Farce::Local::Molecule

Inherits:
Abstract::Molecule show all
Includes:
Shareable
Defined in:
lib/farce/local/molecule.rb

Overview

A shareable record with independent field values in each selected scope. Use Abstract::Molecule.define to declare named fields. Explicitly supplied atoms keep their own storage and comparison policies.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Shareable

#ractor_shareable?

Methods inherited from Abstract::Molecule

atoms, #atoms, compare_by_identity?, #compare_by_identity?, #each, #each_atom, #each_member, #each_value, #freeze, members, #members

Constructor Details

#initialize(scope: self.class.default_scope) ⇒ Molecule

Initialize fields using this variant's atoms. Valid scopes are:

  • :ractor - The value is shared by all threads within the same Ractor.
  • :thread_group - The value is shared by all threads within the same thread group.
  • :thread - The value is shared by all fibers within the same thread.
  • :fiber_storage - The value is shared by all fibers using the same storage. Unless explicitly specified, storage is inherited from the parent fiber for blocking fibers, but not for non-blocking fibers (such as those created by Scheduler#schedule).
  • :fiber - Each fiber has its own independent value.

Parameters:

  • scope (Symbol) (defaults to: self.class.default_scope) —

    the scope for new field atoms, defaulting to the record class scope

See Also:



34
35
36
37
# File 'lib/farce/local/molecule.rb', line 34

def initialize(*, scope: self.class.default_scope, **)
  @scope = scope
  super(*, **)
end

Instance Attribute Details

#scope ⇒ Symbol (readonly)

Returns the scope used by newly created field atoms.

Returns:

  • (Symbol) —

    the scope used by newly created field atoms



40
41
42
# File 'lib/farce/local/molecule.rb', line 40

def scope
  @scope
end

Class Method Details

.default_scope ⇒ Symbol

Returns the default scope for new records.

Returns:

  • (Symbol) —

    the default scope for new records



28
# File 'lib/farce/local/molecule.rb', line 28

def self.default_scope = :ractor

.define(*fields, scope: nil) ⇒ Class<Molecule>

Create a record class with a default scope for newly created field atoms. Valid scopes are:

  • :ractor - The value is shared by all threads within the same Ractor.
  • :thread_group - The value is shared by all threads within the same thread group.
  • :thread - The value is shared by all fibers within the same thread.
  • :fiber_storage - The value is shared by all fibers using the same storage. Unless explicitly specified, storage is inherited from the parent fiber for blocking fibers, but not for non-blocking fibers (such as those created by Scheduler#schedule).
  • :fiber - Each fiber has its own independent value.

Parameters:

  • fields (Array<Symbol, String>) —

    the field names

  • scope (Symbol, nil) (defaults to: nil) —

    the default scope, or nil to inherit

  • compare_by_identity (Boolean, nil) —

    the default comparison policy

Returns:

Raises:

  • (ArgumentError)


19
20
21
22
23
24
25
# File 'lib/farce/local/molecule.rb', line 19

def self.define(*fields, scope: nil, **)
  raise ArgumentError, "Invalid scope: #{scope.inspect}" unless scope.nil? || Farce::SCOPES.include?(scope)

  subclass = super(*fields, **)
  subclass.class_eval "def self.default_scope = #{scope.inspect}", __FILE__, __LINE__ unless scope.nil?
  subclass
end