Class: BSON::Int32

Inherits:
Object
  • Object
show all
Defined in:
lib/bson/int32.rb

Overview

Represents int32 type.

See Also:

Since:

  • 2.0.0

Constant Summary collapse

BSON_TYPE =

A boolean is type 0x08 in the BSON spec.

Since:

  • 2.0.0

16.chr.force_encoding(BINARY).freeze
BYTES_LENGTH =

The number of bytes constant.

Since:

  • 4.0.0

4
PACK =

Constant for the int 32 pack directive.

Since:

  • 2.0.0

"l<".freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ Int32

Instantiate a BSON Int32.

Parameters:

  • value (Integer)

    The 32-bit integer.

See Also:

Since:

  • 4.2.0


61
62
63
64
65
66
67
68
69
70
71
# File 'lib/bson/int32.rb', line 61

def initialize(value)
  if value.is_a?(self.class)
    @value = value.value
    return
  end

  unless value.bson_int32?
    raise RangeError.new("#{value} cannot be stored in 32 bits")
  end
  @value = value.freeze
end

Instance Attribute Details

#valueInteger (readonly)

Returns the value of this Int32.

Returns:

Since:

  • 2.0.0


76
77
78
# File 'lib/bson/int32.rb', line 76

def value
  @value
end

Class Method Details

.from_bson(buffer, **options) ⇒ Integer

Deserialize an Integer from BSON.

Parameters:

  • buffer (ByteBuffer)

    The byte buffer.

  • options (Hash)

    a customizable set of options

Options Hash (**options):

  • :mode (nil | :bson)

    Decoding mode to use.

Returns:

  • (Integer)

    The decoded Integer.

See Also:

Since:

  • 2.0.0


50
51
52
# File 'lib/bson/int32.rb', line 50

def self.from_bson(buffer, **options)
  buffer.get_int32
end

Instance Method Details

#==(other) ⇒ true, false Also known as: eql?, ===

Check equality of the int32 with another object.

Parameters:

  • other (Object)

    The object to check against.

Returns:

  • (true, false)

    If the objects are equal.

Since:

  • 4.4.0


113
114
115
116
# File 'lib/bson/int32.rb', line 113

def ==(other)
  return false unless other.is_a?(Int32)
  value == other.value
end

#as_extended_json(**options) ⇒ Hash | Integer

Converts this object to a representation directly serializable to Extended JSON (github.com/mongodb/specifications/blob/master/source/extended-json.rst).

This method returns the integer value if relaxed representation is requested, otherwise a $numberInt hash.

Parameters:

  • opts (Hash)

    a customizable set of options

Returns:

  • (Hash | Integer)

    The extended json representation.

Since:

  • 2.0.0


130
131
132
133
134
135
136
# File 'lib/bson/int32.rb', line 130

def as_extended_json(**options)
  if options[:mode] == :relaxed || options[:mode] == :legacy
    value
  else
    {'$numberInt' => value.to_s}
  end
end

#to_bson(buffer = ByteBuffer.new, validating_keys = Config.validating_keys?) ⇒ BSON::ByteBuffer

Append the integer as encoded BSON to a ByteBuffer.

Examples:

Encoded the integer and append to a ByteBuffer.

int32.to_bson

Returns:

See Also:

Since:

  • 4.2.0


88
89
90
# File 'lib/bson/int32.rb', line 88

def to_bson(buffer = ByteBuffer.new, validating_keys = Config.validating_keys?)
  buffer.put_int32(value)
end

#to_bson_key(validating_keys = Config.validating_keys?) ⇒ String

Convert the integer to a BSON string key.

Examples:

Convert the integer to a BSON key string.

int.to_bson_key

Parameters:

  • validating_keys (true, false) (defaults to: Config.validating_keys?)

    If BSON should validate the key.

Returns:

  • (String)

    The string key.

Since:

  • 4.2.0


102
103
104
# File 'lib/bson/int32.rb', line 102

def to_bson_key(validating_keys = Config.validating_keys?)
  value
end