emerald_odm 1.0.1 → 1.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bcf3882c777f9cb8c824984d2eefaf5b4abdcc442439c2ab0bdcaa41a1c50177
4
- data.tar.gz: 2e2b31d76322c32bdb3c8147152151d94c7570653108f47a9d1486914335a3b8
3
+ metadata.gz: 801a45bf7c2940c3547f9260d255c47b2b87fc8becad126899ea1e8c078aaf00
4
+ data.tar.gz: de2e365a5c4e60dc4c79766ff624f8300e84245d09c6937169e7fa98c23771f3
5
5
  SHA512:
6
- metadata.gz: 168688c5ae6ccce8007d462b6b352a5ef237f403091b96159da32548a4e684b92b8222956140875aace4f1ad3629882e7b11f7967b592deececf54732985b3ea
7
- data.tar.gz: 055e466d0dd48a9cc002e0c294921096b2741ef5c77ef4f15202c08377eee5d32686be7e91522241631eb7688fa8dfe2f1cfdb6a4f310be8df0c9f3ed6b03f8e
6
+ metadata.gz: 40df24b7d653f85b7707d212ef97d661f196e3f3b96cd7dc6a7c735cd74bd44acc2708dc2700e894288f5fc9c12a4d818c7896315e353758b44381797f639f85
7
+ data.tar.gz: '08e0a9504e115b9d296689b4467147b34f2a74cf8e9d3d7d4ea20788e504de2be38d65f72bd83b835fd893a6adcc89df6197dc7352f2e2a5f6965f50c437ffb2'
data/.gitignore CHANGED
@@ -7,6 +7,8 @@
7
7
  /spec/reports/
8
8
  /tmp/
9
9
  .idea
10
+ *.gem
11
+ .ruby-version
10
12
 
11
13
  # rspec failure tracking
12
14
  .rspec_status
data/README.md CHANGED
@@ -1,40 +1,121 @@
1
- # Emerald::Orm
1
+ # EmeraldODM
2
+ EmeraldODM is an Object-Document Mapper (ODM) for Ruby that allows you to interact with MongoDB databases in a simple, Ruby-like way. It provides a high-level, easy-to-use interface for working with MongoDB documents, while abstracting away the low-level details of the MongoDB driver.
2
3
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/emerald/orm`. To experiment with that code, run `bin/console` for an interactive prompt.
4
-
5
- TODO: Delete this and the text above, and describe your gem
6
-
7
- ## Installation
8
-
9
- Add this line to your application's Gemfile:
4
+ The main objective of this gem is primarily to facilitate reading data from a MongoDB database.
10
5
 
6
+ # Installation
7
+ To install EmeraldODM, simply add it to your Gemfile:
11
8
  ```ruby
12
- gem 'emerald-orm'
9
+ gem 'emerald_odm'
13
10
  ```
11
+ Then run bundle install to install the gem and its dependencies.
14
12
 
15
- And then execute:
16
-
17
- $ bundle install
13
+ # Usage
14
+ Here's a quick example of how to use EmeraldODM to interact with a MongoDB database:
18
15
 
19
- Or install it yourself as:
20
-
21
- $ gem install emerald-orm
22
-
23
- ## Usage
24
-
25
- TODO: Write usage instructions here
16
+ ## 1. Setup DB connection
17
+ ```ruby
18
+ require 'emerald_odm'
19
+
20
+ # Connect to the MongoDB servers before using EmeraldODM
21
+ EmeraldODM.databases_settings.merge!(
22
+ {
23
+ blog: [
24
+ [ '192.168.0.1:27017', '192.168.1.1:27017'],
25
+ {
26
+ database: 'blog',
27
+ user: ENV['MONGO_LOGIN'],
28
+ password: ENV['MONGO_PASSWD'],
29
+ auth_source: ENV['auth_source'],
30
+ max_pool_size: 20,
31
+ }
32
+ ],
33
+ ecommerce: [
34
+ [ '193.168.0.1:27017', '193.168.1.1:27017'],
35
+ {
36
+ database: 'ecommerce',
37
+ user: ENV['MONGO_LOGIN'],
38
+ password: ENV['MONGO_PASSWD'],
39
+ auth_source: ENV['auth_source'],
40
+ max_pool_size: 20,
41
+ }
42
+ ],
43
+ }
44
+ )
26
45
 
27
- ## Development
46
+ ```
28
47
 
29
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
48
+ ## 2. Define your model
49
+ ```ruby
50
+ require 'emerald_odm'
51
+ # Define a model for the "users" collection
52
+ class User < EmeraldODM::Collection
53
+
54
+ attr_accessor :_id, :name, :email, :posts, :keywords_count
55
+
56
+ def self.collection_name
57
+ :users
58
+ end
59
+
60
+ def self.db_name
61
+ :blog
62
+ end
63
+
64
+ def self.posts=(posts)
65
+ @posts = posts.map { |post| Post.new(post)}
66
+ end
67
+
68
+ class Post < EmeraldODM::AttrInitializer
69
+ attr_accessor :id, :title, :body, :created_at, :updated_at
70
+
71
+ def created_at
72
+ Time.parse(@created_at)
73
+ end
74
+
75
+ def updated_at
76
+ Time.parse(@updated_at)
77
+ end
78
+
79
+ def keywords
80
+ body.scan(/\w+/)
81
+ end
82
+ end
83
+
84
+ end
30
85
 
31
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
86
+ ```
32
87
 
33
- ## Contributing
88
+ ## 3. Use it
89
+ ```ruby
90
+ # Find users using a query
91
+ users = User.find(
92
+ {name: 'John Doe'}, # filter query is required
93
+ projection: {name: 1, email: 1, posts: 1, keywords_count: 1}, # optional, the default is to return all fields defined in the model
94
+ limit: 10, # optional, the default is to return all documents
95
+ sort: {name: 1} # optional
96
+ )
97
+
98
+ # users is an array of User objects like Array<User>
99
+ users.each do |user|
100
+ posts = user.posts
101
+ all_user_keywords = posts.map(&:keywords).flatten.uniq
102
+ User.update_one(
103
+ {_id: user._id},
104
+ set: {keywords_count: all_user_keywords.count}
105
+ )
106
+ end
107
+ ```
34
108
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/emerald-orm.
109
+ # Advanced usage
110
+ EmeraldODM supports advanced usage such as:
36
111
 
112
+ ## Accessing the underlying MongoDB driver
113
+ ```ruby
114
+ User.collection # returns the underlying MongoDB::Collection object
115
+ ```
37
116
 
38
- ## License
117
+ # Contributing
118
+ Bug reports and pull requests are welcome on GitHub at https://github.com/MikaelSantilio/emerald-odm/.
39
119
 
40
- The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
120
+ # License
121
+ The gem is available as open source under the terms of the MIT License.
@@ -1,3 +1,3 @@
1
1
  module EmeraldODM
2
- VERSION = '1.0.1'
2
+ VERSION = '1.2.1'
3
3
  end
data/lib/emerald_odm.rb CHANGED
@@ -24,8 +24,7 @@ module EmeraldODM
24
24
  end
25
25
  end
26
26
 
27
- class Collection
28
-
27
+ class AttrInitializer
29
28
  attr_reader :document
30
29
 
31
30
  def initialize(document)
@@ -36,14 +35,6 @@ module EmeraldODM
36
35
  instance_variable_set('@document', document)
37
36
  end
38
37
 
39
- # @return [Symbol, nil]
40
- def self.db_name
41
- nil
42
- end
43
- # @return [Symbol, nil]
44
- def self.collection_name
45
- nil
46
- end
47
38
  # @return [Array]
48
39
  def self.fields
49
40
  public_instance_methods = self.public_instance_methods(false).grep(/=$/)
@@ -57,6 +48,23 @@ module EmeraldODM
57
48
  fields
58
49
  end
59
50
 
51
+ def nil?
52
+ document.nil? || document.empty?
53
+ end
54
+ end
55
+
56
+ class Collection < AttrInitializer
57
+
58
+ # @return [Symbol, nil]
59
+ def self.db_name
60
+ nil
61
+ end
62
+
63
+ # @return [Symbol, nil]
64
+ def self.collection_name
65
+ nil
66
+ end
67
+
60
68
  # @return [Mongo::Collection] The collection
61
69
  def self.collection
62
70
  Connector.database(db_name&.to_sym)[collection_name&.to_sym]
@@ -71,7 +79,7 @@ module EmeraldODM
71
79
  # @param [Hash] sort The sort
72
80
  # @param [Integer] limit The limit
73
81
  # @return [Array<self>] The documents
74
- def find(filter: {}, projection: {}, sort: {}, limit: 0)
82
+ def find(filter, projection: {}, sort: {}, limit: 0)
75
83
  self.class.validate_fields_from_stages(filter, projection, sort)
76
84
 
77
85
  if projection.empty?
@@ -85,8 +93,8 @@ module EmeraldODM
85
93
  query.to_a.map { |document| self.class.new(document) }
86
94
  end
87
95
 
88
- def self.find(filter: {}, projection: {}, sort: {}, limit: 0)
89
- new({}).find(filter: filter, projection: projection, sort: sort, limit: limit)
96
+ def self.find(filter, projection: {}, sort: {}, limit: 0)
97
+ new({}).find(filter, projection: projection, sort: sort, limit: limit)
90
98
  end
91
99
 
92
100
  def self.update(type, filter, set: {}, unset: {})
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: emerald_odm
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - mikael
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-01-18 00:00:00.000000000 Z
11
+ date: 2023-01-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -145,7 +145,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
145
145
  version: '0'
146
146
  requirements: []
147
147
  rubyforge_project:
148
- rubygems_version: 2.7.7
148
+ rubygems_version: 2.7.6
149
149
  signing_key:
150
150
  specification_version: 4
151
151
  summary: MongoDB ODM