Class: Farce::Local::Molecule
- Inherits:
-
Abstract::Molecule
- Object
- Abstract::Molecule
- Farce::Local::Molecule
- 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
-
#scope ⇒ Symbol
readonly
The scope used by newly created field atoms.
Class Method Summary collapse
-
.default_scope ⇒ Symbol
The default scope for new records.
-
.define(*fields, scope: nil) ⇒ Class<Molecule>
Create a record class with a default scope for newly created field atoms.
Instance Method Summary collapse
-
#initialize(scope: self.class.default_scope) ⇒ Molecule
constructor
Initialize fields using this variant's atoms.
Methods included from 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.
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.
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.
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.
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 |