mongoid-sample 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8c0598c09b54cbf3e5af1a6e01b86cc1ec0d6e9f
4
+ data.tar.gz: e28da839c96c9e151944665d29a899939bfe3b24
5
+ SHA512:
6
+ metadata.gz: 620e6853d1cf4169c6d3fd493499c69acfc8dbe4e6a6f7cd443bd2ca4993400e4a8ebed45da2081ca89de29b1450df9873870b43b870b6700406b81e0ccb3b80
7
+ data.tar.gz: b4f67150c2becac4d9f3bfabc1462ccd0f33c5c4a15a5d38eabe61b69c5b4b03441d4c2c0265aaf4173024a7a678b350964c9c661573500c3b7ce455ddefaf2f
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.3.0
4
+ before_install: gem install bundler -v 1.11.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in mongoid-sample.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Ben Crouse
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.
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Ben Crouse
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
13
+ all 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
21
+ THE SOFTWARE.
@@ -0,0 +1,71 @@
1
+ # Mongoid::Sample
2
+
3
+ Brings the API of Ruby's Array#sample to Mongoid collections
4
+
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ gem 'mongoid-sample'
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install mongoid-sample
19
+
20
+ And require:
21
+
22
+ require 'mongoid/sample'
23
+
24
+ ## Usage
25
+
26
+ ### Sample a single document
27
+
28
+ ```ruby
29
+ require 'mongoid/sample'
30
+
31
+ class MyModel
32
+ include Mongoid::Document
33
+ field :name
34
+ end
35
+
36
+ puts MyModel.sample.inspect # => nil
37
+ 5.times { MyModel.create(name: 'Foo') }
38
+ puts MyModel.sample.inspect # => #<MyModel _id: 57954c1002182308d7000000, name: "Foo">
39
+ ```
40
+
41
+ ### Sample multiple documents
42
+
43
+ ```ruby
44
+ require 'mongoid/sample'
45
+
46
+ class MyModel
47
+ include Mongoid::Document
48
+ field :name
49
+ end
50
+
51
+ puts MyModel.sample(3).inspect # => []
52
+ 5.times { MyModel.create(name: 'Foo') }
53
+ puts MyModel.sample(3).inspect # => [#<MyModel _id: 57954c6f02182308d7000008, name: "Foo">, #<MyModel _id: 57954c6f02182308d700000a, name: "Foo">, #<MyModel _id: 57954c6f02182308d7000009, name: "Foo">]
54
+ ```
55
+
56
+ ### Sample from scope
57
+
58
+ ```ruby
59
+ require 'mongoid/sample'
60
+
61
+ class MyModel
62
+ include Mongoid::Document
63
+ field :name
64
+ end
65
+
66
+ puts MyModel.where(name: 'Foo').sample(3).inspect # => []
67
+ 5.times { MyModel.create(name: 'Foo') }
68
+ 5.times { MyModel.create(name: 'Bar') }
69
+ puts MyModel.sample(3).inspect # => [#<MyModel _id: 57954c6f02182308d7000007, name: "Foo">, #<MyModel _id: 57954c6f02182308d7000008, name: "Foo">, #<MyModel _id: 57954ca702182308d700000e, name: "Bar">]
70
+ puts MyModel.where(name: 'Foo').sample(3).inspect # => [#<MyModel _id: 57954c6f02182308d7000008, name: "Foo">, #<MyModel _id: 57954c6f02182308d7000009, name: "Foo">, #<MyModel _id: 57954c6f02182308d7000006, name: "Foo">]
71
+ ```
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "mongoid/sample"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,26 @@
1
+ require 'mongoid/sample/version'
2
+ require 'mongoid'
3
+
4
+ module Mongoid
5
+ class Criteria
6
+ def sample(n = 1)
7
+ indexes = (0..count - 1).sort_by { rand }.slice(0, n)
8
+
9
+ if n == 1
10
+ skip(indexes.first).first
11
+ else
12
+ indexes.map { |i| skip(i).first }
13
+ end
14
+ end
15
+ end
16
+ end
17
+
18
+ module Mongoid
19
+ module Sample
20
+ def sample(*args)
21
+ criteria.sample(*args)
22
+ end
23
+ end
24
+ end
25
+
26
+ Mongoid::Document::ClassMethods.send(:include, Mongoid::Sample)
@@ -0,0 +1,5 @@
1
+ module Mongoid
2
+ module Sample
3
+ VERSION = '0.1.0'
4
+ end
5
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'mongoid/sample/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'mongoid-sample'
8
+ spec.version = Mongoid::Sample::VERSION
9
+ spec.authors = ['Ben Crouse']
10
+ spec.email = ['bencrouse@gmail.com']
11
+
12
+ spec.summary = "Brings Ruby's Array#sample to Mongoid collections"
13
+ spec.homepage = 'https://github.com/bencrouse/mongoid-sample'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = 'exe'
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_dependency 'mongoid', '>= 4.0'
22
+
23
+ spec.add_development_dependency 'bundler', '~> 1.11'
24
+ spec.add_development_dependency 'rake', '~> 10.0'
25
+ spec.add_development_dependency 'minitest', '~> 5.0'
26
+ end
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mongoid-sample
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ben Crouse
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-11-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: mongoid
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '4.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '4.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.11'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.11'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '5.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '5.0'
69
+ description:
70
+ email:
71
+ - bencrouse@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".travis.yml"
78
+ - Gemfile
79
+ - LICENSE
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - bin/console
84
+ - bin/setup
85
+ - lib/mongoid/sample.rb
86
+ - lib/mongoid/sample/version.rb
87
+ - mongoid-sample.gemspec
88
+ homepage: https://github.com/bencrouse/mongoid-sample
89
+ licenses:
90
+ - MIT
91
+ metadata: {}
92
+ post_install_message:
93
+ rdoc_options: []
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ requirements: []
107
+ rubyforge_project:
108
+ rubygems_version: 2.5.1
109
+ signing_key:
110
+ specification_version: 4
111
+ summary: Brings Ruby's Array#sample to Mongoid collections
112
+ test_files: []
113
+ has_rdoc: