modelize 0.0.2 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +2 -0
- data/CHANGELOG.md +3 -0
- data/Gemfile +9 -0
- data/LICENSE +21 -0
- data/README.md +85 -0
- data/config/boot.rb +13 -0
- data/lib/modelize.rb +2 -0
- data/lib/modelize/core.rb +18 -0
- data/lib/modelize/ext.rb +34 -0
- data/modelize.gemspec +21 -0
- data/models/flat.rb +7 -0
- data/models/model.rb +3 -0
- metadata +57 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9f857eb7b855f5aa821498911f415a582a359d84
|
4
|
+
data.tar.gz: 308f8449a4d2035ff19893f15e3a35ae1818df76
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1f1d2367a81949c4b8551462067caab4e5b985efabf9ab94e77dd3264ed764ca3383cb453637cc7a89ccb3b90f66927b1f3b1de3c795bcc70a7d98c6407670a3
|
7
|
+
data.tar.gz: d2dc9f4fef900564345e83732908c474ba46ccea15ae57d3588e1bad3bc54fa8b35dd1bb8141c6088e885b86d67e5d1964d0644d3d9a7261c4720e4f6b7d2214
|
data/.gitignore
ADDED
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2016 Fugroup
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
# Modelize MongoDB Documents
|
2
|
+
Automatically turn MongoDB BSON Documents into custom models.
|
3
|
+
|
4
|
+
Works 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)
|
5
|
+
|
6
|
+
### Installation
|
7
|
+
```ruby
|
8
|
+
gem install modelize
|
9
|
+
```
|
10
|
+
or add to your Gemfile. That's it!
|
11
|
+
|
12
|
+
### Settings
|
13
|
+
```ruby
|
14
|
+
# Enable / disable Modelize on the fly
|
15
|
+
Modelize.enable = true
|
16
|
+
|
17
|
+
# Tell Modelize in which module to find your model class
|
18
|
+
Modelize.module = ''
|
19
|
+
|
20
|
+
# If your model class is in the Fu::Models module
|
21
|
+
Modelize.module = 'Fu::Models'
|
22
|
+
|
23
|
+
# Debug option
|
24
|
+
Modelize.debug = false
|
25
|
+
```
|
26
|
+
|
27
|
+
### Usage
|
28
|
+
If your MongoDB collection is named "models", then your model class should be named "Model", and it'll automatically be picked up.
|
29
|
+
|
30
|
+
The gem works by adding some spice to the Mongo::Collection::View "to_a" and "first" methods.
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
# If you don't have bundler set up
|
34
|
+
require 'modelize'
|
35
|
+
|
36
|
+
# Create your model, no includes necessary
|
37
|
+
class Model
|
38
|
+
# Your model definitions here ...
|
39
|
+
end
|
40
|
+
|
41
|
+
# Without Modelize, just pure Ruby driver
|
42
|
+
models = find(:models).to_a # => [BSON::Document, BSON::Document]
|
43
|
+
model = first(:models) # => BSON::Document
|
44
|
+
|
45
|
+
# With Modelize and Minimongo
|
46
|
+
models = find(:models).to_a # => [Model, Model]
|
47
|
+
model = first(:models) # => Model
|
48
|
+
|
49
|
+
# With Modelize and the Mongodb Ruby driver
|
50
|
+
models = $db[:models].find.to_a # => [Model, Model]
|
51
|
+
model = $db[:models].find.first # => Model
|
52
|
+
```
|
53
|
+
The models will be of type "Model" if you've defined such as class. You can then do:
|
54
|
+
```ruby
|
55
|
+
model = first(:models)
|
56
|
+
|
57
|
+
# The original BSON::Document
|
58
|
+
model.doc # => BSON::Document instance
|
59
|
+
|
60
|
+
# The model class accepts any method that BSON::Document accepts
|
61
|
+
model.to_h # => {'_id' => BSON::ObjectId('586c4ac80aec08424e3a5287')}
|
62
|
+
|
63
|
+
# You can read and write variables
|
64
|
+
model.description = 'we need truth'
|
65
|
+
model.light = 'yes'
|
66
|
+
model.description # => 'we need truth'
|
67
|
+
model.light # => 'yes'
|
68
|
+
|
69
|
+
# And then save the changes (Minimongo)
|
70
|
+
model.earth = 'undiscovered'
|
71
|
+
update(:models, {:_id => m._id}, m.to_h)
|
72
|
+
|
73
|
+
# Refetch it and it's saved
|
74
|
+
model = first(:models, :_id => m._id)
|
75
|
+
m.earth # => 'undiscovered'
|
76
|
+
|
77
|
+
# Returns nil if not found, doesn't raise an error
|
78
|
+
model.darkness # => nil
|
79
|
+
```
|
80
|
+
You can now add validations or whatever you want to your model, it's just a normal Ruby class. Enjoy!
|
81
|
+
|
82
|
+
### Contribute
|
83
|
+
Created and maintained by [Fugroup Ltd.](https://www.fugroup.net) We are the creators of [CrowdfundHQ.](https://crowdfundhq.com)
|
84
|
+
|
85
|
+
`@authors: Vidar`
|
data/config/boot.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'bundler/setup'
|
2
|
+
Bundler.require(:default, :development)
|
3
|
+
|
4
|
+
MODE = ENV['RACK_ENV'] || 'development'
|
5
|
+
|
6
|
+
require './lib/modelize.rb'
|
7
|
+
require './models/model.rb'
|
8
|
+
require './models/flat.rb'
|
9
|
+
|
10
|
+
# Connect to DB
|
11
|
+
Minimongo.db = Mongo::Client.new(['127.0.0.1:27017'], :database => "modelize_#{MODE}")
|
12
|
+
|
13
|
+
include Minimongo::Query
|
data/lib/modelize.rb
CHANGED
@@ -0,0 +1,18 @@
|
|
1
|
+
module Modelize
|
2
|
+
module Core
|
3
|
+
|
4
|
+
attr_accessor :doc
|
5
|
+
|
6
|
+
# Read and write to the BSON::Document
|
7
|
+
def method_missing(name, *args, &block)
|
8
|
+
# Extract name and write mode
|
9
|
+
name =~ /([^=]+)(=)?/
|
10
|
+
|
11
|
+
# Call methods before instance variables
|
12
|
+
return args[0] ? @doc.send($1, args[0]) : @doc.send($1) if @doc.respond_to?($1)
|
13
|
+
|
14
|
+
# Write or read the BSON::Document, nil if nothing found
|
15
|
+
return args[0] ? @doc[$1] = args[0] : @doc[$1]
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
data/lib/modelize/ext.rb
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
# # # # # # # # #
|
2
|
+
# Patch the Mongo::Collection::View class to return documents as models
|
3
|
+
# If you query the :models collection, if a Model class exists
|
4
|
+
# and the setting Minimongo.enable = true
|
5
|
+
#
|
6
|
+
# Set Minimongo.modules = 'Fu::Models' if your models is inside a module,
|
7
|
+
# default module is root '::'
|
8
|
+
#
|
9
|
+
|
10
|
+
module Mongo
|
11
|
+
class Collection
|
12
|
+
class View
|
13
|
+
|
14
|
+
# To a BSON::Documents or models
|
15
|
+
def to_a
|
16
|
+
Modelize.enable ? super.map{|doc| modelize(doc)} : super
|
17
|
+
end
|
18
|
+
|
19
|
+
# First BSON::Document or model
|
20
|
+
def first
|
21
|
+
Modelize.enable ? modelize(super) : super
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
# Get the model class, BSON::Document if it doesn't exist
|
27
|
+
def modelize(doc)
|
28
|
+
Object.const_get("#{Modelize.module}::#{collection.name[0..-2].capitalize}").include(::Modelize::Core).new.tap{|m| m.doc = doc} rescue doc
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
data/modelize.gemspec
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = 'modelize'
|
3
|
+
s.version = '0.1.0'
|
4
|
+
s.date = '2017-01-05'
|
5
|
+
s.summary = "Modelize MongoDB Documents"
|
6
|
+
s.description = "Automatically turn MongoDB BSON Documents into custom models"
|
7
|
+
s.authors = ["Fugroup Limited"]
|
8
|
+
s.email = 'mail@fugroup.net'
|
9
|
+
|
10
|
+
s.add_runtime_dependency 'mongo', '~> 2.2'
|
11
|
+
s.add_development_dependency 'futest', '>= 0'
|
12
|
+
s.add_development_dependency 'minimongo', '>= 0'
|
13
|
+
|
14
|
+
s.homepage = 'https://github.com/fugroup/modelize'
|
15
|
+
s.license = 'MIT'
|
16
|
+
|
17
|
+
s.require_paths = ['lib']
|
18
|
+
s.files = `git ls-files -z`.split("\x0").reject do |f|
|
19
|
+
f.match(%r{^(test|spec|features)/})
|
20
|
+
end
|
21
|
+
end
|
data/models/flat.rb
ADDED
data/models/model.rb
ADDED
metadata
CHANGED
@@ -1,22 +1,75 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: modelize
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Fugroup Limited
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-01-
|
12
|
-
dependencies:
|
11
|
+
date: 2017-01-05 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: mongo
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2.2'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.2'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: futest
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minimongo
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
13
55
|
description: Automatically turn MongoDB BSON Documents into custom models
|
14
56
|
email: mail@fugroup.net
|
15
57
|
executables: []
|
16
58
|
extensions: []
|
17
59
|
extra_rdoc_files: []
|
18
60
|
files:
|
61
|
+
- ".gitignore"
|
62
|
+
- CHANGELOG.md
|
63
|
+
- Gemfile
|
64
|
+
- LICENSE
|
65
|
+
- README.md
|
66
|
+
- config/boot.rb
|
19
67
|
- lib/modelize.rb
|
68
|
+
- lib/modelize/core.rb
|
69
|
+
- lib/modelize/ext.rb
|
70
|
+
- modelize.gemspec
|
71
|
+
- models/flat.rb
|
72
|
+
- models/model.rb
|
20
73
|
homepage: https://github.com/fugroup/modelize
|
21
74
|
licenses:
|
22
75
|
- MIT
|
@@ -37,7 +90,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
37
90
|
version: '0'
|
38
91
|
requirements: []
|
39
92
|
rubyforge_project:
|
40
|
-
rubygems_version: 2.
|
93
|
+
rubygems_version: 2.6.8
|
41
94
|
signing_key:
|
42
95
|
specification_version: 4
|
43
96
|
summary: Modelize MongoDB Documents
|