Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/adamluzsi/mongoid-dsl
mongoid-dsl for ease of use
https://github.com/adamluzsi/mongoid-dsl
Last synced: 13 days ago
JSON representation
mongoid-dsl for ease of use
- Host: GitHub
- URL: https://github.com/adamluzsi/mongoid-dsl
- Owner: adamluzsi
- License: mit
- Created: 2014-05-07T09:42:52.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2014-10-21T08:16:16.000Z (about 10 years ago)
- Last Synced: 2024-04-21T03:58:00.175Z (7 months ago)
- Language: Ruby
- Size: 332 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Mongoid-DSL
==============Ruby Module for ease of use with mongoid based models
with this module, you can do:
* recursive methods by calling [:_find,:_find_by,:_where,:_all] on the model (even on deeply embedded ones)
* check connection relation between two model
* get :parents, :references or :documents### Examples for use
recursive find
If the model path is not clear by default the shortest way will be chosen,
You can modify this behavior by adding model names to the args, before or after the query hash
for example: TestD._where( TestA, TestB, TestC, test: "world" ).inspectThe return object will be array if the target model is not a main one but an embedded
```ruby
require 'mongoid-dsl'
require_relative "helper/con":TestA.mongoid_name
#> nilclass TestA
include Mongoid::Document
include Mongoid::Timestampsstore_in :collection => self.mongoid_name
embeds_many :TestB.mongoid_name
field :test,
:type => String,
:presence => true,
:desc => "description for this field",
:accept_only => %W[ hello world hello\ world ]end
:TestA.mongoid_name
#> "test_a"class TestB
include Mongoid::Document
include Mongoid::Timestampsembedded_in :TestA.mongoid_name
embeds_many :TestC.mongoid_namefield :test,
:type => String,
:presence => true,
:desc => "description for this field",
:uniq => trueend
class TestC
include Mongoid::Document
include Mongoid::Timestampsembedded_in :TestB.mongoid_name
embeds_many :TestD.mongoid_namefield :test,
:type => String,
:presence => true,
:desc => "description for this field"end
class TestD
include Mongoid::Document
include Mongoid::Timestampsembedded_in :TestC.mongoid_name
field :test,
:type => String,
:presence => true,
:desc => "description for this field"end
test_a= TestA.create! test: "hello"
test_a.test_b.create(test: "world")
test_b= test_a.test_b.last
test_b.test_c.create(test: "world")
test_c= test_b.test_c.last
test_c.test_d.create(test: "world")
test_d= test_c.test_d.last# puts TestD._find(test_d['_id']).inspect
#puts TestD._all.inspect
#> return every embedded TestD objputs TestD._where( test: "world" ).inspect
#> return by criteria the embedded TestD objectsMongoid.purge!
```
As a plus, you can dump your mongoid models into a Hash Object,
that can be used to serialized as yaml and lend out as documentation if you lazy as me```ruby
File.write "/tmp/#{$0}.model_dump.yml",
Mongoid.model_relations_dump.to_yaml```
you can find working examples in the example folder