{"id":19142216,"url":"https://github.com/eldoy/modelize","last_synced_at":"2025-07-29T10:42:51.821Z","repository":{"id":56884191,"uuid":"77963918","full_name":"eldoy/modelize","owner":"eldoy","description":"Make the mongodb driver return custom models instead of BSON::Document objects","archived":false,"fork":false,"pushed_at":"2017-01-05T23:00:03.000Z","size":29,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-22T10:17:12.906Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/eldoy.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-01-04T00:04:30.000Z","updated_at":"2022-06-04T21:58:02.000Z","dependencies_parsed_at":"2022-08-20T13:10:54.788Z","dependency_job_id":null,"html_url":"https://github.com/eldoy/modelize","commit_stats":null,"previous_names":["fugroup/modelize"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eldoy%2Fmodelize","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eldoy%2Fmodelize/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eldoy%2Fmodelize/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eldoy%2Fmodelize/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/eldoy","download_url":"https://codeload.github.com/eldoy/modelize/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240223214,"owners_count":19767597,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-09T07:26:21.589Z","updated_at":"2025-02-22T19:21:14.953Z","avatar_url":"https://github.com/eldoy.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Modelize MongoDB Documents\nAutomatically turn MongoDB BSON Documents into custom models.\n\nWorks with the official [Ruby MongoDB driver](https://docs.mongodb.com/ruby-driver/master/quick-start/), or the [truly great Minimongo.](https://github.com/fugroup/minimongo)\n\n### Installation\n```ruby\ngem install modelize\n```\nor add to your Gemfile. That's it!\n\n### Settings\n```ruby\n# Enable / disable Modelize on the fly\nModelize.enable = true\n\n# Tell Modelize in which module to find your model class\nModelize.module = ''\n\n# If your model class is in the Fu::Models module\nModelize.module = 'Fu::Models'\n\n# Debug option\nModelize.debug = false\n```\n\n### Usage\nIf your MongoDB collection is named \"models\", then your model class should be named \"Model\", and it'll automatically be picked up.\n\nThe gem works by adding some spice to the Mongo::Collection::View \"to_a\" and \"first\" methods.\n\n```ruby\n# If you don't have bundler set up\nrequire 'modelize'\n\n# Create your model, no includes necessary\nclass Model\n  # Your model definitions here ...\nend\n\n# Without Modelize, just pure Ruby driver\nmodels = find(:models).to_a     # =\u003e [BSON::Document, BSON::Document]\nmodel = first(:models)          # =\u003e BSON::Document\n\n# With Modelize and Minimongo\nmodels = find(:models).to_a     # =\u003e [Model, Model]\nmodel = first(:models)          # =\u003e Model\n\n# With Modelize and the Mongodb Ruby driver\nmodels = $db[:models].find.to_a # =\u003e [Model, Model]\nmodel = $db[:models].find.first # =\u003e Model\n```\nThe models will be of type \"Model\" if you've defined such as class. You can then do:\n```ruby\nmodel = first(:models)\n\n# The original BSON::Document\nmodel.doc # =\u003e BSON::Document instance\n\n# The model class accepts any method that BSON::Document accepts\nmodel.to_h # =\u003e {'_id' =\u003e BSON::ObjectId('586c4ac80aec08424e3a5287')}\n\n# You can read and write variables\nmodel.description = 'we need truth'\nmodel.light = 'yes'\nmodel.description # =\u003e 'we need truth'\nmodel.light # =\u003e 'yes'\n\n# And then save the changes (Minimongo)\nmodel.earth = 'undiscovered'\nupdate(:models, {:_id =\u003e m._id}, m.to_h)\n\n# Refetch it and it's saved\nmodel = first(:models, :_id =\u003e m._id)\nm.earth # =\u003e 'undiscovered'\n\n# Returns nil if not found, doesn't raise an error\nmodel.darkness # =\u003e nil\n```\nYou can now add validations or whatever you want to your model, it's just a normal Ruby class. Enjoy!\n\n### Contribute\nCreated and maintained by [Fugroup Ltd.](https://www.fugroup.net) We are the creators of [CrowdfundHQ.](https://crowdfundhq.com)\n\n`@authors: Vidar`\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feldoy%2Fmodelize","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Feldoy%2Fmodelize","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feldoy%2Fmodelize/lists"}