Class: BSON::Decimal128
- Inherits:
-
Object
- Object
- BSON::Decimal128
- Includes:
- JSON
- Defined in:
- lib/bson/decimal128.rb,
lib/bson/decimal128/builder.rb
Defined Under Namespace
Modules: Builder Classes: InvalidArgument, InvalidRange, InvalidString
Constant Summary collapse
- BSON_TYPE =
A Decimal128 is type 0x13 in the BSON spec.
19.chr.force_encoding(BINARY).freeze
- EXPONENT_OFFSET =
Exponent offset.
6176.freeze
- MIN_EXPONENT =
Minimum exponent.
-6176.freeze
- MAX_EXPONENT =
Maximum exponent.
6111.freeze
- MAX_DIGITS_OF_PRECISION =
Maximum digits of precision.
34.freeze
- EXTENDED_JSON_KEY =
Key for this type when converted to extended json.
"$numberDecimal".freeze
- NATIVE_TYPE =
The native type to which this object can be converted.
BigDecimal
Class Method Summary collapse
-
.from_bits(low, high) ⇒ BSON::Decimal128
Instantiate a Decimal128 from high and low bits.
-
.from_bson(buffer) ⇒ BSON::Decimal128
Deserialize the decimal128 from raw BSON bytes.
-
.from_string(string) ⇒ BSON::Decimal128
Instantiate a Decimal128 from a string.
Instance Method Summary collapse
-
#==(other) ⇒ true, false
(also: #eql?)
Check equality of the decimal128 object with another object.
-
#as_json(*args) ⇒ Hash
Get the Decimal128 as JSON hash data.
-
#hash ⇒ Integer
Get the hash value for the decimal128.
-
#initialize(object) ⇒ Decimal128
constructor
Create a new Decimal128 from a BigDecimal.
-
#inspect ⇒ String
Get a nice string for use with object inspection.
-
#to_big_decimal ⇒ BigDecimal
Get a Ruby BigDecimal object corresponding to this Decimal128.
-
#to_bson(buffer = ByteBuffer.new, validating_keys = Config.validating_keys?) ⇒ BSON::ByteBuffer
Get the decimal128 as its raw BSON data.
-
#to_s ⇒ String
(also: #to_str)
Get the string representation of the decimal128.
Methods included from JSON
Constructor Details
#initialize(object) ⇒ Decimal128
Create a new Decimal128 from a BigDecimal.
98 99 100 101 102 103 104 105 106 |
# File 'lib/bson/decimal128.rb', line 98 def initialize(object) if object.is_a?(String) set_bits(*Builder::FromString.new(object).bits) elsif object.is_a?(BigDecimal) set_bits(*Builder::FromBigDecimal.new(object).bits) else raise InvalidArgument.new end end |
Class Method Details
.from_bits(low, high) ⇒ BSON::Decimal128
Instantiate a Decimal128 from high and low bits.
234 235 236 237 238 |
# File 'lib/bson/decimal128.rb', line 234 def from_bits(low, high) decimal = allocate decimal.send(:set_bits, low, high) decimal end |
.from_bson(buffer) ⇒ BSON::Decimal128
Deserialize the decimal128 from raw BSON bytes.
203 204 205 |
# File 'lib/bson/decimal128.rb', line 203 def from_bson(buffer) from_bits(*buffer.get_decimal128_bytes.unpack('Q<*')) end |
.from_string(string) ⇒ BSON::Decimal128
Instantiate a Decimal128 from a string.
219 220 221 |
# File 'lib/bson/decimal128.rb', line 219 def from_string(string) from_bits(*Builder::FromString.new(string).bits) end |
Instance Method Details
#==(other) ⇒ true, false Also known as: eql?
Check equality of the decimal128 object with another object.
80 81 82 83 84 |
# File 'lib/bson/decimal128.rb', line 80 def ==(other) return false unless other.is_a?(Decimal128) @high == other.instance_variable_get(:@high) && @low == other.instance_variable_get(:@low) end |
#as_json(*args) ⇒ Hash
Get the Decimal128 as JSON hash data.
66 67 68 |
# File 'lib/bson/decimal128.rb', line 66 def as_json(*args) { EXTENDED_JSON_KEY => to_s } end |
#hash ⇒ Integer
Get the hash value for the decimal128.
130 131 132 133 134 |
# File 'lib/bson/decimal128.rb', line 130 def hash num = @high << 64 num |= @low num.hash end |
#inspect ⇒ String
Get a nice string for use with object inspection.
144 145 146 |
# File 'lib/bson/decimal128.rb', line 144 def inspect "BSON::Decimal128('#{to_s}')" end |
#to_big_decimal ⇒ BigDecimal
Get a Ruby BigDecimal object corresponding to this Decimal128. Note that, when converting to a Ruby BigDecimal, non-zero significant digits are preserved but trailing zeroes may be lost. See the following example:
Note that the the BSON::Decimal128 object can represent -NaN, sNaN, and -sNaN while Ruby’s BigDecimal cannot.
180 181 182 |
# File 'lib/bson/decimal128.rb', line 180 def to_big_decimal @big_decimal ||= BigDecimal(to_s) end |
#to_bson(buffer = ByteBuffer.new, validating_keys = Config.validating_keys?) ⇒ BSON::ByteBuffer
Get the decimal128 as its raw BSON data.
118 119 120 |
# File 'lib/bson/decimal128.rb', line 118 def to_bson(buffer = ByteBuffer.new, validating_keys = Config.validating_keys?) buffer.put_decimal128(@low, @high) end |