123456789_123456789_123456789_123456789_123456789_

Class: Mongo::WriteConcern::Base

Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Subclasses:
Inherits: Object
Defined in: lib/mongo/write_concern/base.rb

Overview

Defines common behavior for write concerns.

Since:

  • 2.7.0

Class Method Summary

Instance Attribute Summary

Constructor Details

.new(options) ⇒ Base

Instantiate a new write concern given the options.

Examples:

Instantiate a new write concern mode.

Mongo::WriteConcern::Acknowledged.new(:w => 1)

Parameters:

  • options (Hash)

    The options to instantiate with.

Options Hash (options):

  • :w (Integer, String)

    The number of servers or the custom mode to acknowledge.

  • :j (true, false)

    Whether to acknowledge a write to the journal.

  • :fsync (true, false)

    Should the write be synced to disc.

  • :wtimeout (Integer)

    The number of milliseconds to wait for acknowledgement before raising an error.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/write_concern/base.rb', line 45

def initialize(options)
  options = Options::Mapper.transform_keys_to_symbols(options)
  options = Options::Mapper.transform_values_to_strings(options).freeze

  if options[:w]
    if options[:w] == 0 && options[:j]
      raise Error::InvalidWriteConcern,
            "Invalid write concern options: :j cannot be true when :w is 0: #{options.inspect}"
    elsif options[:w] == 0 && options[:fsync]
      raise Error::InvalidWriteConcern,
            "Invalid write concern options: :fsync cannot be true when :w is 0: #{options.inspect}"
    elsif options[:w].is_a?(Integer) && options[:w] < 0
      raise Error::InvalidWriteConcern,
            "Invalid write concern options: :w cannot be negative (#{options[:w]}): #{options.inspect}"
    end
  end

  if options[:journal]
    raise Error::InvalidWriteConcern, "Invalid write concern options: use :j for journal: #{options.inspect}"
  end

  @options = options
end

Instance Attribute Details

#optionsHash (readonly)

Returns:

  • (Hash)

    The write concern options.

Since:

  • 2.7.0

[ GitHub ]

  
# File 'lib/mongo/write_concern/base.rb', line 24

attr_reader :options