Module: Farce::Abstract::Flag Abstract

Includes:
Value, Internal::Copyable, Internal::ValueSerialization, Internal::Inspect, Internal::MarshalSupport::Flag
Included in:
Flag, Local::Flag
Defined in:
lib/farce/abstract/flag.rb

Overview

This module is abstract.

Common boolean interface and serialization for atomic flags.

Note:

This is a module rather than a class so Flag can inherit directly from the native flag and avoid delegation overhead.

Instance Method Summary collapse

Methods included from Internal::ValueSerialization

#as_extended_json, #as_json, #to_bson, #to_bson_normalized_value, #to_cbor, #to_json, #to_msgpack

Methods included from Value

#blank?, #unwrap

Methods included from Internal::Copyable

#duplicable?

Instance Method Details

#compare_and_set(expected_value, replacement_value) ⇒ Boolean

Replace the value only if it matches the expected value.

Parameters:

  • expected_value (Boolean) —

    the expected current value

  • replacement_value (Boolean) —

    the new value

Returns:

  • (Boolean) —

    whether the replacement succeeded

Raises:

  • (ArgumentError) —

    if either argument is not true or false



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/farce/abstract/flag.rb', line 47

module Flag
  include Internal::MarshalSupport::Flag
  include Internal::Copyable
  include Value
  include Internal::ValueSerialization
  include Internal::Inspect

  # @api private
  def inspect_with(inspector)
    begin
      current = value
    rescue StandardError
      # FrozenError may inspect an allocated flag before it is initialized.
      return inspector.object_with_address(self)
    end

    super do
      yield if block_given?
      inspector.breakable
      inspector.object(current)
    end
  end
end

#set ⇒ true

Set the flag to true.

Returns:

  • (true)


47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/farce/abstract/flag.rb', line 47

module Flag
  include Internal::MarshalSupport::Flag
  include Internal::Copyable
  include Value
  include Internal::ValueSerialization
  include Internal::Inspect

  # @api private
  def inspect_with(inspector)
    begin
      current = value
    rescue StandardError
      # FrozenError may inspect an allocated flag before it is initialized.
      return inspector.object_with_address(self)
    end

    super do
      yield if block_given?
      inspector.breakable
      inspector.object(current)
    end
  end
end

#store(value) ⇒ Boolean

Set the flag to the given value.

Parameters:

  • value (Boolean) —

    the new value

Returns:

  • (Boolean) —

    the new value

Raises:

  • (ArgumentError) —

    if the value is not true or false



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/farce/abstract/flag.rb', line 47

module Flag
  include Internal::MarshalSupport::Flag
  include Internal::Copyable
  include Value
  include Internal::ValueSerialization
  include Internal::Inspect

  # @api private
  def inspect_with(inspector)
    begin
      current = value
    rescue StandardError
      # FrozenError may inspect an allocated flag before it is initialized.
      return inspector.object_with_address(self)
    end

    super do
      yield if block_given?
      inspector.breakable
      inspector.object(current)
    end
  end
end

#swap(value) ⇒ Boolean

Replace the value and return its previous state.

Parameters:

  • value (Boolean) —

    the new value

Returns:

  • (Boolean) —

    the previous value

Raises:

  • (ArgumentError) —

    if the value is not true or false



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/farce/abstract/flag.rb', line 47

module Flag
  include Internal::MarshalSupport::Flag
  include Internal::Copyable
  include Value
  include Internal::ValueSerialization
  include Internal::Inspect

  # @api private
  def inspect_with(inspector)
    begin
      current = value
    rescue StandardError
      # FrozenError may inspect an allocated flag before it is initialized.
      return inspector.object_with_address(self)
    end

    super do
      yield if block_given?
      inspector.breakable
      inspector.object(current)
    end
  end
end

#toggle ⇒ Boolean

Atomically invert the value.

Returns:

  • (Boolean) —

    the new value



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/farce/abstract/flag.rb', line 47

module Flag
  include Internal::MarshalSupport::Flag
  include Internal::Copyable
  include Value
  include Internal::ValueSerialization
  include Internal::Inspect

  # @api private
  def inspect_with(inspector)
    begin
      current = value
    rescue StandardError
      # FrozenError may inspect an allocated flag before it is initialized.
      return inspector.object_with_address(self)
    end

    super do
      yield if block_given?
      inspector.breakable
      inspector.object(current)
    end
  end
end

#unset ⇒ false

Set the flag to false.

Returns:

  • (false)


47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/farce/abstract/flag.rb', line 47

module Flag
  include Internal::MarshalSupport::Flag
  include Internal::Copyable
  include Value
  include Internal::ValueSerialization
  include Internal::Inspect

  # @api private
  def inspect_with(inspector)
    begin
      current = value
    rescue StandardError
      # FrozenError may inspect an allocated flag before it is initialized.
      return inspector.object_with_address(self)
    end

    super do
      yield if block_given?
      inspector.breakable
      inspector.object(current)
    end
  end
end

#value ⇒ Boolean

Returns the current value.

Returns:

  • (Boolean) —

    the current value



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/farce/abstract/flag.rb', line 47

module Flag
  include Internal::MarshalSupport::Flag
  include Internal::Copyable
  include Value
  include Internal::ValueSerialization
  include Internal::Inspect

  # @api private
  def inspect_with(inspector)
    begin
      current = value
    rescue StandardError
      # FrozenError may inspect an allocated flag before it is initialized.
      return inspector.object_with_address(self)
    end

    super do
      yield if block_given?
      inspector.breakable
      inspector.object(current)
    end
  end
end

#value=(value) ⇒ BasicObject

Alias for #store



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/farce/abstract/flag.rb', line 47

module Flag
  include Internal::MarshalSupport::Flag
  include Internal::Copyable
  include Value
  include Internal::ValueSerialization
  include Internal::Inspect

  # @api private
  def inspect_with(inspector)
    begin
      current = value
    rescue StandardError
      # FrozenError may inspect an allocated flag before it is initialized.
      return inspector.object_with_address(self)
    end

    super do
      yield if block_given?
      inspector.breakable
      inspector.object(current)
    end
  end
end