Class: AtomicRuby::AtomicBoolean

Inherits:
Object
  • Object
show all
Defined in:
lib/atomic-ruby/atomic_boolean.rb

Defined Under Namespace

Classes: InvalidBooleanError

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ AtomicBoolean

Returns a new instance of AtomicBoolean.



9
10
11
12
13
14
15
16
17
# File 'lib/atomic-ruby/atomic_boolean.rb', line 9

def initialize(value)
  unless value.is_a?(TrueClass) || value.is_a?(FalseClass)
    raise InvalidBooleanError, "expected boolean to be a `TrueClass` or `FalseClass`, got #{value.class}"
  end

  @boolean = Atom.new(value)

  Ractor.make_shareable(self)
end

Instance Method Details

#false?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/atomic-ruby/atomic_boolean.rb', line 27

def false?
  value == false
end

#make_falseObject



35
36
37
# File 'lib/atomic-ruby/atomic_boolean.rb', line 35

def make_false
  @boolean.swap { false }
end

#make_trueObject



31
32
33
# File 'lib/atomic-ruby/atomic_boolean.rb', line 31

def make_true
  @boolean.swap { true }
end

#toggleObject



39
40
41
# File 'lib/atomic-ruby/atomic_boolean.rb', line 39

def toggle
  @boolean.swap { |current_value| !current_value }
end

#true?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/atomic-ruby/atomic_boolean.rb', line 23

def true?
  value == true
end

#valueObject



19
20
21
# File 'lib/atomic-ruby/atomic_boolean.rb', line 19

def value
  @boolean.value
end