Class: BSON::Int64

Inherits:
Object
  • Object
show all
Includes:
JSON
Defined in:
lib/bson/int64.rb

Overview

Represents int64 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

::String.new(18.chr, encoding: BINARY).freeze
PACK =

Constant for the int 64 pack directive.

Since:

  • 2.0.0

"q<"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from JSON

#to_json

Constructor Details

#initialize(value) ⇒ Int64

Instantiate a BSON Int64.

Parameters:

  • value (Integer)

    The 64-bit integer.

See Also:

Since:

  • 4.2.0



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/bson/int64.rb', line 64

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

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

Instance Attribute Details

#valueInteger (readonly)

Returns the value of this Int64.

Returns:

Since:

  • 2.0.0



79
80
81
# File 'lib/bson/int64.rb', line 79

def value
  @value
end

Class Method Details

.from_bson(buffer, **options) ⇒ Integer | BSON::Int64

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:

See Also:

Since:

  • 2.0.0



48
49
50
51
52
53
54
55
# File 'lib/bson/int64.rb', line 48

def self.from_bson(buffer, **options)
  value = buffer.get_int64
  if options[:mode] == :bson
    new(value)
  else
    value
  end
end

Instance Method Details

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

Check equality of the int64 with another object.

Parameters:

  • other (Object)

    The object to check against.

Returns:

  • (true, false)

    If the objects are equal.

Since:

  • 4.4.0



114
115
116
117
# File 'lib/bson/int64.rb', line 114

def ==(other)
  return false unless other.is_a?(Int64)
  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 $numberLong hash.

Parameters:

  • opts (Hash)

    a customizable set of options

Returns:

  • (Hash | Integer)

    The extended json representation.

Since:

  • 2.0.0



143
144
145
146
147
148
149
# File 'lib/bson/int64.rb', line 143

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

#as_json(**options) ⇒ Integer

Return a string representation of the Int64 for use in application-level JSON serialization. This method is intentionally different from #as_extended_json.

Examples:

Get the Int64 as a JSON-serializable object.

int64.as_json

Returns:

  • (Integer)

    The Int64 as an Integer.

Since:

  • 2.0.0



129
130
131
# File 'lib/bson/int64.rb', line 129

def as_json(**options)
  value
end

#to_bson(buffer = ByteBuffer.new) ⇒ BSON::ByteBuffer

Append the integer as encoded BSON to a ByteBuffer.

Examples:

Encoded the integer and append to a ByteBuffer.

int64.to_bson

Returns:

See Also:

Since:

  • 4.2.0



91
92
93
# File 'lib/bson/int64.rb', line 91

def to_bson(buffer = ByteBuffer.new)
  buffer.put_int64(value)
end

#to_bson_keyString

Convert the integer to a BSON string key.

Examples:

Convert the integer to a BSON key string.

int.to_bson_key

Returns:

  • (String)

    The string key.

Since:

  • 4.2.0



103
104
105
# File 'lib/bson/int64.rb', line 103

def to_bson_key
  value
end