easymongo 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e410057d0345f4702f71a094005449ca511dafca
4
+ data.tar.gz: 8b2dab525f5da96fedc58715b2ecd1ead20ed93f
5
+ SHA512:
6
+ metadata.gz: 18c0bb86735521dbf4baf0b67ae551120989c4ee0848bdf62a93ebefd19ea3420638641d12ef4e24ae60b1d5478693d835aa4409aa34db27e7d49678765a65e5
7
+ data.tar.gz: f70bb4fedf16b5e3355cd0e18f14c1ff2b3055acdfb46c15212eba09ed10a6bc7075adf166f1d03905559edb5089c7f8af80168898dbc4a8690911e2b45ee962
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ .bundle
2
+ Gemfile.lock
data/CHANGELOG.md ADDED
@@ -0,0 +1,3 @@
1
+ **Version 0.0.1** - *2017-07-12*
2
+
3
+ - Initial version
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ group :development do
6
+ gem 'rerun'
7
+ gem 'rb-fsevent'
8
+ gem 'terminal-notifier'
9
+ end
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,41 @@
1
+ # Easymongo
2
+ Super easy MongoDB Ruby client. This is the way mongo should be.
3
+
4
+ If you need something closer to pure Mongo, have a look at [Minimongo.](https://github.com/fugroup/minimongo)
5
+
6
+ We also have [Mongocore](https://github.com/fugroup/mongocore) if you're looking for a full ORM.
7
+
8
+ ### Installation
9
+ ```
10
+ gem install easymongo
11
+ ```
12
+ or add to Gemfile.
13
+
14
+ ### Usage
15
+ ```ruby
16
+ # All commands supported
17
+ # https://docs.mongodb.com/ruby-driver/master/quick-start
18
+
19
+ # Connect
20
+ $db = Easymongo::Query.new(['127.0.0.1:27017'], :database => "easymongo_#{ENV['RACK_ENV']}")
21
+
22
+ # First
23
+ $db.collection.get.first
24
+
25
+ # Last
26
+ $db.collection.get.last
27
+
28
+ # All
29
+ $db.collection.get.all
30
+
31
+ # Insert / Update
32
+ $db.collection.set(:name => name)
33
+
34
+ # Delete
35
+ $db.domains.delete(id)
36
+
37
+ ```
38
+
39
+ Created and maintained by [Fugroup Ltd.](https://www.fugroup.net) We are the creators of [CrowdfundHQ.](https://crowdfundhq.com)
40
+
41
+ `@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/easymongo.rb'
7
+
8
+ Mongo::Logger.logger.level = ::Logger::FATAL
9
+
10
+ # Connect
11
+ $db = Easymongo::Query.new(['127.0.0.1:27017'], :database => "easymongo_#{MODE}")
12
+
13
+ include Futest::Helpers
data/easymongo.gemspec ADDED
@@ -0,0 +1,22 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'easymongo'
3
+ s.version = '0.0.1'
4
+ s.date = '2017-07-12'
5
+ s.summary = "Super Easy MongoDB client"
6
+ s.description = "The way MongoDB for Ruby should be, can't get easier than this"
7
+ s.authors = ["Fugroup Limited"]
8
+ s.email = 'vidar@fugroup.net'
9
+
10
+ s.add_runtime_dependency 'mongo', '>= 2.2'
11
+ s.add_runtime_dependency 'activesupport', '>= 4.0'
12
+ s.add_runtime_dependency 'request_store', '>= 1.2'
13
+ s.add_development_dependency 'futest', '>= 0'
14
+
15
+ s.homepage = 'https://github.com/fugroup/easymongo'
16
+ s.license = 'MIT'
17
+
18
+ s.require_paths = ['lib']
19
+ s.files = `git ls-files -z`.split("\x0").reject do |f|
20
+ f.match(%r{^(test|spec|features)/})
21
+ end
22
+ end
@@ -0,0 +1,56 @@
1
+ # The mongodb document
2
+
3
+ module Easymongo
4
+ class Document
5
+
6
+ attr_accessor :doc, :values
7
+
8
+ # Takes a BSON::Document
9
+ def initialize(doc)
10
+
11
+ # Replace _id with id
12
+ doc['id'] = doc.delete('_id')
13
+
14
+ # Convert all BSON::ObjectId to string
15
+ doc.each{|k, v| doc[k] = v.to_s if v.is_a?(BSON::ObjectId)}
16
+
17
+ # Symbolize keys
18
+ self.doc = doc.symbolize_keys
19
+ end
20
+
21
+ # Get bson id
22
+ def bson_id
23
+ @bson_id ||= BSON::ObjectId.from_string(doc[:id])
24
+ end
25
+
26
+ # Creation date
27
+ def date
28
+ bson_id.generation_time
29
+ end
30
+
31
+ # Read value
32
+ def [](key)
33
+ doc[key.to_sym]
34
+ end
35
+
36
+ # Write value
37
+ def []=(key, value)
38
+ doc[key.to_sym] = value
39
+ end
40
+
41
+ # Make the doc user friendly with dot notation
42
+ # Provides access to doc object methods too
43
+ def method_missing(name, *args, &block)
44
+
45
+ # Write value
46
+ return doc[name[0..-2].to_sym] = args.first if args.size == 1 and name[-1] == '='
47
+
48
+ # Read value
49
+ return doc[name] if doc.has_key?(name)
50
+
51
+ # Run method on doc object
52
+ return doc.send(name, *args) if doc.respond_to?(name)
53
+ end
54
+
55
+ end
56
+ end
@@ -0,0 +1,146 @@
1
+ # This is the easiest possible implementation of a mongodb client.
2
+ # We got rid of the ObjectID and the underscore, and made mongodb super easy to use.
3
+ # Use BSON::ObjectId.new.generation_time as timestamp if you need that
4
+ #
5
+ # If you need something close to pure Mongo, check out https://github.com/fugroup/minimongo
6
+ # If you need models for Easymongo, check out https://github.com/fugroup/modelize
7
+ # If you need an ORM, check out https://github.com/fugroup/mongocore
8
+
9
+ require 'mongo'
10
+ require 'active_support'
11
+ require 'active_support/core_ext'
12
+ require 'request_store'
13
+
14
+ module Easymongo
15
+ class Query
16
+
17
+ attr_accessor :client
18
+
19
+ # Set up connection
20
+ def initialize(uri, options)
21
+ self.client = Mongo::Client.new(uri, options)
22
+ end
23
+
24
+ # Set up collection, stored in the thread
25
+ def method_missing(name, *args, &block)
26
+ s[:coll] = name; self
27
+ end
28
+
29
+ # Set values
30
+ def set(*args)
31
+
32
+ # Insert, add oid
33
+ data, values = args.size == 1 ? [{'_id' => oid}, *args] : args
34
+
35
+ # Using set and unset so we don't store nil in the db
36
+ options = {
37
+ :$set => values.select{|k, v| !v.nil?}, :$unset => values.select{|k, v| v.nil?}
38
+ }.delete_if{|k, v| v.empty?}
39
+
40
+ # Normalize data
41
+ data = ids(data)
42
+
43
+ # Update the collection
44
+ result = client[coll].update_one(data, options, :upsert => true)
45
+
46
+ # Return result
47
+ Easymongo::Result.new(result, data, values, options)
48
+ end
49
+
50
+ # Get values, store cursor in thread
51
+ def get(data = {})
52
+ s[:cursor] = client[coll].find(ids(data)); self
53
+ end
54
+
55
+ # Limit
56
+ def limit(n)
57
+ g!; s[:cursor] = cursor.limit(n.to_i); self
58
+ end
59
+
60
+ # Sort
61
+ def sort(data)
62
+ g!; s[:cursor] = cursor.sort(data); self
63
+ end
64
+
65
+ # Get first
66
+ def first
67
+ g!; cursor.first.tap{|r| return ed(r) if r; c!}
68
+ end
69
+
70
+ # Get last
71
+ def last
72
+ g!; cursor.sort(:$natural => -1).first.tap{|r| return ed(r) if r; c!}
73
+ end
74
+
75
+ # Get all
76
+ def all
77
+ g!; cursor.to_a.map{|r| ed(r)}.tap{ c!}
78
+ end
79
+
80
+ # Count
81
+ def count
82
+ g!; cursor.count.tap{ c!}
83
+ end
84
+
85
+ # Remove
86
+ def rm(data)
87
+
88
+ # Normalize data
89
+ data = ids(data)
90
+
91
+ # Delete doc
92
+ result = client[coll].delete_one(data)
93
+
94
+ # Return result
95
+ Easymongo::Result.new(result, data).tap{ c!}
96
+ end
97
+
98
+ # Make sure dataever passed works
99
+ def ids(data)
100
+
101
+ # Just return if nothing to do
102
+ return data if data.empty?
103
+
104
+ # Support passing id as string
105
+ data = {'_id' => data} if data.is_a?(String)
106
+
107
+ # Turn all keys to string
108
+ data = data.stringify_keys
109
+
110
+ # Convert id to _id for mongo
111
+ data['_id'] = data.delete('id') if data['id']
112
+
113
+ # Convert ids to BSON ObjectId
114
+ data.each{|k, v| data[k] = oid(v) if v.is_a?(String) and v =~ /^[0-9a-fA-F]{24}$/}
115
+
116
+ # Return data
117
+ data
118
+ end
119
+
120
+ # Convert to BSON ObjectId or make a new one
121
+ def oid(v = nil)
122
+ return BSON::ObjectId.new if v.nil?; BSON::ObjectId.from_string(v) rescue v
123
+ end
124
+
125
+ private
126
+
127
+ # Get the request store
128
+ def s; RequestStore.store; end
129
+
130
+ # Clear request store
131
+ def c!; RequestStore.clear!; end
132
+
133
+ # Run get if no cursor
134
+ def g!; get unless cursor; end
135
+
136
+ # Get the collection
137
+ def coll; s[:coll]; end
138
+
139
+ # Get the cursor
140
+ def cursor; s[:cursor]; end
141
+
142
+ # Short cut for creating documents
143
+ def ed(r); Easymongo::Document.new(r); end
144
+
145
+ end
146
+ end
@@ -0,0 +1,36 @@
1
+ module Easymongo
2
+ class Result
3
+
4
+ attr_accessor :result, :data, :values, :options
5
+
6
+ # Init takes a Mongo::Operation::Result
7
+ def initialize(result, data, values = nil, options = nil)
8
+ self.result = result
9
+ self.data = data
10
+ self.values = values
11
+ self.options = options
12
+ end
13
+
14
+ # Get the id as BSON::ObjectId
15
+ def bson_id
16
+ result.upserted_id || data['_id']
17
+ end
18
+
19
+ # Get the id if available
20
+ def id
21
+ bson_id ? bson_id.to_s : nil
22
+ end
23
+
24
+ # Creation date
25
+ def date
26
+ bson_id ? bson_id.generation_time : nil
27
+ end
28
+
29
+ # For the mongo operation result
30
+ def method_missing(name, *args, &block)
31
+ return result.send(name, *args) if result.respond_to?(name)
32
+ super
33
+ end
34
+
35
+ end
36
+ end
data/lib/easymongo.rb ADDED
@@ -0,0 +1,26 @@
1
+ require 'mongo'
2
+
3
+ module Easymongo
4
+
5
+ # # # # # #
6
+ # Easymongo MongoDB tiny Ruby library.
7
+ # @homepage: https://github.com/fugroup/easymongo
8
+ # @author: Vidar <vidar@fugroup.net>, Fugroup Ltd.
9
+ # @license: MIT, contributions are welcome.
10
+ # # # # # #
11
+
12
+ class << self; attr_accessor :db; end
13
+ end
14
+
15
+ require_relative 'easymongo/document'
16
+ require_relative 'easymongo/result'
17
+ require_relative 'easymongo/query'
18
+
19
+ # Indexing
20
+ # $db.client[:profiles].indexes.create_one({:key => 1}, :unique => true)
21
+
22
+ # Info on MongoDB Driver
23
+ # https://docs.mongodb.com/ruby-driver/master/quick-start/
24
+ # http://zetcode.com/db/mongodbruby/
25
+ # http://recipes.sinatrarb.com/p/databases/mongo
26
+ # https://github.com/steveren/ruby-driver-sample-app/blob/master/lib/neighborhood.rb
metadata ADDED
@@ -0,0 +1,110 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: easymongo
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Fugroup Limited
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-07-12 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: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '4.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '4.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: request_store
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '1.2'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '1.2'
55
+ - !ruby/object:Gem::Dependency
56
+ name: futest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: The way MongoDB for Ruby should be, can't get easier than this
70
+ email: vidar@fugroup.net
71
+ executables: []
72
+ extensions: []
73
+ extra_rdoc_files: []
74
+ files:
75
+ - ".gitignore"
76
+ - CHANGELOG.md
77
+ - Gemfile
78
+ - LICENSE
79
+ - README.md
80
+ - config/boot.rb
81
+ - easymongo.gemspec
82
+ - lib/easymongo.rb
83
+ - lib/easymongo/document.rb
84
+ - lib/easymongo/query.rb
85
+ - lib/easymongo/result.rb
86
+ homepage: https://github.com/fugroup/easymongo
87
+ licenses:
88
+ - MIT
89
+ metadata: {}
90
+ post_install_message:
91
+ rdoc_options: []
92
+ require_paths:
93
+ - lib
94
+ required_ruby_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ required_rubygems_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ requirements: []
105
+ rubyforge_project:
106
+ rubygems_version: 2.6.10
107
+ signing_key:
108
+ specification_version: 4
109
+ summary: Super Easy MongoDB client
110
+ test_files: []