abstract class Moongoon::Document
- Moongoon::Document
- Reference
- Object
Overview
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
Included Modules
- BSON::Serializable
- JSON::Serializable
Defined in:
models/models.crConstructors
- .new(pull : JSON::PullParser)
-
.new(bson : BSON)
Allocate an instance and copies data from a BSON struct.
-
.new(**args : **T) forall T
Creates a new instance of the class from variadic arguments.
Class Method Summary
-
.from_bson(bson : BSON)
NOTE See
self.new
.
Instance Method Summary
-
#to_bson(bson = BSON.new)
Converts to a BSON representation.
-
#to_tuple
Instantiate a named tuple from the model instance properties.
Constructor Detail
def self.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)
def self.new(**args : **T) forall T
#
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.
Class Method Detail
Instance Method Detail
def to_bson(bson = BSON.new)
#
Converts to a BSON representation.
user = User.new name: "John"
bson = user.to_bson
def 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.