class

Moongoon::Document

Inherits BSON::Serializable / JSON::Serializable / Reference / Object

Base model class.

Contains helpers to (de)serialize data to json format and bson format.

class Models::MyModel < Moongoon::Document
  property name : String
  property age : Int32
end

Constructors

new(pull : JSON::PullParser)
Source
new(bson : BSON)

Allocate an instance and copies data from a BSON struct.

class User
  include BSON::Serializable
  property name : String
end

data = BSON.new
data["name"] = "John"
User.new(data)
Source
new(*, __pull_for_json_serializable pull : JSON::PullParser)
Source
new

Creates a new instance of the class from variadic arguments.

User.new first_name: "John", last_name: "Doe"

NOTE: Only instance variables having associated setter methods will be initialized.

Source

Class methods

from_bson(bson : BSON)

NOTE: See self.new.

Source

Instance methods

to_bson(bson = BSON.new)

Converts to a BSON representation.

user = User.new name: "John"
bson = user.to_bson
Source
to_tuple

Instantiate a named tuple from the model instance properties.

user = User.new first_name: "John", last_name: "Doe"
pp user.to_tuple
# => {
#   first_name: "John",
#   last_name: "Doe",
# }

NOTE: Only instance variables having associated getter methods will be returned.

Source