mongoid-flags 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.gitignore ADDED
@@ -0,0 +1,49 @@
1
+ # rcov generated
2
+ coverage
3
+ coverage.data
4
+
5
+ # rdoc generated
6
+ rdoc
7
+
8
+ # yard generated
9
+ doc
10
+ .yardoc
11
+
12
+ # bundler
13
+ .bundle
14
+
15
+ # jeweler generated
16
+ pkg
17
+
18
+ # Have editor/IDE/OS specific files you need to ignore? Consider using a global gitignore:
19
+ #
20
+ # * Create a file at ~/.gitignore
21
+ # * Include files you want ignored
22
+ # * Run: git config --global core.excludesfile ~/.gitignore
23
+ #
24
+ # After doing this, these files will be ignored in all your git projects,
25
+ # saving you from having to 'pollute' every project you touch with them
26
+ #
27
+ # Not sure what to needs to be ignored for particular editors/OSes? Here's some ideas to get you started. (Remember, remove the leading # of the line)
28
+ #
29
+ # For MacOS:
30
+ #
31
+ .DS_Store
32
+
33
+ # For TextMate
34
+ *.tmproj
35
+ tmtags
36
+
37
+ # For emacs:
38
+ #*~
39
+ #\#*
40
+ #.\#*
41
+
42
+ # For vim:
43
+ #*.swp
44
+
45
+ # For redcar:
46
+ #.redcar
47
+
48
+ # For rubinius:
49
+ #*.rbc
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ script: rake spec
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem "mongoid", "~> 3.0.3"
4
+ gem "bson_ext", "~> 1.6"
5
+
6
+ group :development do
7
+ gem "jeweler", "~> 1.8.4"
8
+ gem "rspec", "~> 2.10.0"
9
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,51 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ activemodel (3.2.9)
5
+ activesupport (= 3.2.9)
6
+ builder (~> 3.0.0)
7
+ activesupport (3.2.9)
8
+ i18n (~> 0.6)
9
+ multi_json (~> 1.0)
10
+ bson (1.7.0)
11
+ bson_ext (1.7.0)
12
+ bson (~> 1.7.0)
13
+ builder (3.0.4)
14
+ diff-lcs (1.1.3)
15
+ git (1.2.5)
16
+ i18n (0.6.1)
17
+ jeweler (1.8.4)
18
+ bundler (~> 1.0)
19
+ git (>= 1.2.5)
20
+ rake
21
+ rdoc
22
+ json (1.7.5)
23
+ mongoid (3.0.13)
24
+ activemodel (~> 3.1)
25
+ moped (~> 1.1)
26
+ origin (~> 1.0)
27
+ tzinfo (~> 0.3.22)
28
+ moped (1.2.9)
29
+ multi_json (1.3.7)
30
+ origin (1.0.10)
31
+ rake (10.0.1)
32
+ rdoc (3.12)
33
+ json (~> 1.4)
34
+ rspec (2.10.0)
35
+ rspec-core (~> 2.10.0)
36
+ rspec-expectations (~> 2.10.0)
37
+ rspec-mocks (~> 2.10.0)
38
+ rspec-core (2.10.0)
39
+ rspec-expectations (2.10.0)
40
+ diff-lcs (~> 1.1.3)
41
+ rspec-mocks (2.10.1)
42
+ tzinfo (0.3.35)
43
+
44
+ PLATFORMS
45
+ ruby
46
+
47
+ DEPENDENCIES
48
+ bson_ext (~> 1.6)
49
+ jeweler (~> 1.8.4)
50
+ mongoid (~> 3.0.3)
51
+ rspec (~> 2.10.0)
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Thiago Jackiw
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,125 @@
1
+ # mongoid-flags [![Build Status](http://travis-ci.org/tjackiw/mongoid-flags.png)](http://travis-ci.org/tjackiw/mongoid-flags)
2
+
3
+ Simple document flagging system for mongoid v3. HEAVILY INSPIRED by mongoid-simple-roles
4
+
5
+ ## Install
6
+
7
+ Add this line to your Gemfile:
8
+
9
+ ```ruby
10
+ gem 'mongoid-flags'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ ```ruby
16
+ bundle install
17
+ ```
18
+
19
+ Or install it yourself:
20
+
21
+ ```ruby
22
+ gem install mongoid-flags
23
+ ```
24
+
25
+ ## Usage
26
+
27
+ ### Model
28
+
29
+ ```ruby
30
+ class User
31
+ include Mongoid::Document
32
+ include Mongoid::Document::Flagable
33
+
34
+ field :name, type: String
35
+ end
36
+ ```
37
+
38
+ ### Instance Methods
39
+
40
+ Let's first create an instance of the User model
41
+
42
+ ```ruby
43
+ user = User.create(name: 'John Doe')
44
+ ```
45
+
46
+ `add_flag(flag)` will add a flag to the instance. You need to explicitly call `.save` to persist the changes.
47
+
48
+ ```ruby
49
+ user.add_flag('suspended')
50
+ => ["suspended"]
51
+ user.reload
52
+ user.has_flag?('suspended')
53
+ => false
54
+ ```
55
+
56
+ `add_flag!(flag)` is same as `add_flag` but it automatically saves the instance.
57
+
58
+ ```ruby
59
+ user.add_flag!('suspended')
60
+ => ["suspended"]
61
+ user.reload
62
+ user.has_flag?('suspended')
63
+ => true
64
+ ```
65
+
66
+ `has_flag?(flag)` checks if a flag exist and returns `true` or `false`.
67
+
68
+ ```ruby
69
+ user.add_flag!('suspended')
70
+ => ["suspended"]
71
+ user.has_flag?('suspended')
72
+ => true
73
+ user.has_flag?('underage')
74
+ => false
75
+ ```
76
+
77
+ `remove_flag(flag)` removes a flag. You need to explicitly call `.save` to persist the changes.
78
+
79
+ ```ruby
80
+ user.remove_flag('suspended')
81
+ => []
82
+ user.reload
83
+ user.has_flag?('suspended')
84
+ => true
85
+ ```
86
+
87
+ `remove_flag!(flag)` same as `remove_flag` but it automatically saves the instance.
88
+
89
+ ```ruby
90
+ user.remove_flag!('suspended')
91
+ => []
92
+ user.reload
93
+ user.has_flag?('suspended')
94
+ => false
95
+ ```
96
+
97
+ ### Class Methods
98
+
99
+ `find_by_flags(flags)` performs a query based on the flags provided and returns an array of objects (if any). The argument can either be a String or an Array.
100
+
101
+ ```ruby
102
+ User.find_by_flags('suspended')
103
+ => [<User>]
104
+
105
+ User.find_by_flags(['suspended', 'underage'])
106
+ => [<User>]
107
+ ```
108
+
109
+ ## Contributing to mongoid-flags
110
+
111
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
112
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
113
+ * Fork the project.
114
+ * Start a feature/bugfix branch.
115
+ * Commit and push until you are happy with your contribution.
116
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
117
+ * Please try not to mess with the Rakefile, version, or history.
118
+
119
+ ## Authors
120
+
121
+ * Thiago Jackiw: [@tjackiw](http://twitter.com/tjackiw)
122
+
123
+ ## Copyright
124
+
125
+ Copyright (c) 2012 Thiago Jackiw. See LICENSE.txt for further details.
data/Rakefile ADDED
@@ -0,0 +1,36 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+
13
+ require 'rake'
14
+ require 'jeweler'
15
+
16
+ Jeweler::Tasks.new do |gem|
17
+ gem.name = "mongoid-flags"
18
+ gem.version = '0.0.1'
19
+ gem.homepage = "http://github.com/tjackiw/mongoid-flags"
20
+ gem.license = "MIT"
21
+ gem.summary = %Q{Simple record flagging system for Mongoid documents}
22
+ gem.description = %Q{Simple record flagging system for Mongoid documents}
23
+ gem.authors = ["Thiago Jackiw"]
24
+ gem.email = "tjackiw@gmail.com"
25
+ gem.files = `git ls-files`.split("\n")
26
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
27
+ gem.require_paths = ["lib"]
28
+ end
29
+ Jeweler::RubygemsDotOrgTasks.new
30
+
31
+ task :default => :spec
32
+
33
+ desc "Run all specs"
34
+ task "spec" do
35
+ exec "bundle exec rspec spec"
36
+ end
@@ -0,0 +1,58 @@
1
+ module Mongoid
2
+ module Document
3
+ module Flagable
4
+ def self.included(base)
5
+ base.class_eval do |klass|
6
+ klass.field :flags, type: Array
7
+ klass.index({ flags: 1 }, { background: true })
8
+
9
+ include InstanceMethods
10
+ extend ClassMethods
11
+ end
12
+ end
13
+
14
+ module InstanceMethods
15
+
16
+ # Adds a flag, and you need to
17
+ # explicitly save the object
18
+ def add_flag(flag)
19
+ (self.flags ||= []) << flag
20
+ self.flags.uniq!
21
+ self.flags
22
+ end
23
+
24
+ # Adds a flag, and saves the object
25
+ def add_flag!(flag)
26
+ add_flag(flag)
27
+ save!
28
+ self.flags
29
+ end
30
+
31
+ # Checks if a flag exists
32
+ def has_flag?(flag)
33
+ (self.flags || []).include?(flag)
34
+ end
35
+
36
+ # Removes a flag, and you need to
37
+ # explicitly save the object
38
+ def remove_flag(flag)
39
+ (self.flags || []).delete_if { |n| n.casecmp(flag) == 0 }
40
+ end
41
+
42
+ # Removes a flag, and saves the object
43
+ def remove_flag!(flag)
44
+ remove_flag(flag)
45
+ save!
46
+ self.flags
47
+ end
48
+ end
49
+
50
+ module ClassMethods
51
+ def find_by_flags(flags)
52
+ flags = [flags] unless flags.is_a?(Array)
53
+ criteria.in(:flags => flags).to_a
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,62 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "mongoid-flags"
8
+ s.version = "0.0.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Thiago Jackiw"]
12
+ s.date = "2012-11-22"
13
+ s.description = "Simple record flagging system for Mongoid documents"
14
+ s.email = "tjackiw@gmail.com"
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.md"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ ".travis.yml",
23
+ "Gemfile",
24
+ "Gemfile.lock",
25
+ "LICENSE.txt",
26
+ "README.md",
27
+ "Rakefile",
28
+ "lib/mongoid-flags.rb",
29
+ "mongoid-flags.gemspec",
30
+ "mongoid.yml",
31
+ "spec/helper.rb",
32
+ "spec/mongoid-flags_spec.rb"
33
+ ]
34
+ s.homepage = "http://github.com/tjackiw/mongoid-flags"
35
+ s.licenses = ["MIT"]
36
+ s.require_paths = ["lib"]
37
+ s.rubygems_version = "1.8.24"
38
+ s.summary = "Simple record flagging system for Mongoid documents"
39
+ s.test_files = ["spec/helper.rb", "spec/mongoid-flags_spec.rb"]
40
+
41
+ if s.respond_to? :specification_version then
42
+ s.specification_version = 3
43
+
44
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
45
+ s.add_runtime_dependency(%q<mongoid>, ["~> 3.0.3"])
46
+ s.add_runtime_dependency(%q<bson_ext>, ["~> 1.6"])
47
+ s.add_development_dependency(%q<jeweler>, ["~> 1.8.4"])
48
+ s.add_development_dependency(%q<rspec>, ["~> 2.10.0"])
49
+ else
50
+ s.add_dependency(%q<mongoid>, ["~> 3.0.3"])
51
+ s.add_dependency(%q<bson_ext>, ["~> 1.6"])
52
+ s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
53
+ s.add_dependency(%q<rspec>, ["~> 2.10.0"])
54
+ end
55
+ else
56
+ s.add_dependency(%q<mongoid>, ["~> 3.0.3"])
57
+ s.add_dependency(%q<bson_ext>, ["~> 1.6"])
58
+ s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
59
+ s.add_dependency(%q<rspec>, ["~> 2.10.0"])
60
+ end
61
+ end
62
+
data/mongoid.yml ADDED
@@ -0,0 +1,6 @@
1
+ test:
2
+ sessions:
3
+ default:
4
+ database: mongoid-flags
5
+ hosts:
6
+ - localhost:27017
data/spec/helper.rb ADDED
@@ -0,0 +1,20 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+
11
+ require 'mongoid'
12
+ require 'mongoid-flags'
13
+
14
+ Mongoid.load!("mongoid.yml", :test)
15
+
16
+ RSpec.configure do |config|
17
+ config.after(:each) do
18
+ Mongoid::Config.purge!
19
+ end
20
+ end
@@ -0,0 +1,61 @@
1
+ require 'helper'
2
+
3
+ class User
4
+ include Mongoid::Document
5
+ include Mongoid::Document::Flagable
6
+ field :name
7
+ end
8
+
9
+ describe "A Flagable model" do
10
+ let(:user) { User.new(name: 'John Doe') }
11
+
12
+ it "should have an add_flag method" do
13
+ user.should respond_to :add_flag
14
+ end
15
+
16
+ it "should have an add_flag! method" do
17
+ user.should respond_to :add_flag!
18
+ end
19
+
20
+ it "should have a has_flag? method" do
21
+ user.should respond_to :has_flag?
22
+ end
23
+
24
+ it "should have a remove_flag method" do
25
+ user.should respond_to :remove_flag
26
+ end
27
+
28
+ it "should have a remove_flag! method" do
29
+ user.should respond_to :remove_flag!
30
+ end
31
+
32
+ it "should be able to add a flag" do
33
+ flag = "suspended"
34
+ user.add_flag(flag)
35
+ user.has_flag?(flag).should be true
36
+ end
37
+
38
+ it "should be able to remove a flag" do
39
+ flag = "suspended"
40
+ user.add_flag(flag)
41
+ user.remove_flag(flag)
42
+ user.has_flag?(flag).should_not be true
43
+ end
44
+
45
+ it "should be able to call add_flag multiple times" do
46
+ user.add_flag('suspended')
47
+ user.add_flag('to be removed')
48
+ user.flags.count.should be 2
49
+ end
50
+
51
+ it "should only contain unique flags" do
52
+ user.add_flag('suspended')
53
+ user.add_flag('suspended')
54
+ user.flags.count.should be 1
55
+ end
56
+
57
+ it "should be able to find flagged users with find_by_flags" do
58
+ user.add_flag!('suspended')
59
+ User.find_by_flags('suspended').first.should eq(user)
60
+ end
61
+ end
metadata ADDED
@@ -0,0 +1,129 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mongoid-flags
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Thiago Jackiw
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-22 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: mongoid
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 3.0.3
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 3.0.3
30
+ - !ruby/object:Gem::Dependency
31
+ name: bson_ext
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '1.6'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '1.6'
46
+ - !ruby/object:Gem::Dependency
47
+ name: jeweler
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: 1.8.4
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 1.8.4
62
+ - !ruby/object:Gem::Dependency
63
+ name: rspec
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: 2.10.0
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: 2.10.0
78
+ description: Simple record flagging system for Mongoid documents
79
+ email: tjackiw@gmail.com
80
+ executables: []
81
+ extensions: []
82
+ extra_rdoc_files:
83
+ - LICENSE.txt
84
+ - README.md
85
+ files:
86
+ - .document
87
+ - .gitignore
88
+ - .travis.yml
89
+ - Gemfile
90
+ - Gemfile.lock
91
+ - LICENSE.txt
92
+ - README.md
93
+ - Rakefile
94
+ - lib/mongoid-flags.rb
95
+ - mongoid-flags.gemspec
96
+ - mongoid.yml
97
+ - spec/helper.rb
98
+ - spec/mongoid-flags_spec.rb
99
+ homepage: http://github.com/tjackiw/mongoid-flags
100
+ licenses:
101
+ - MIT
102
+ post_install_message:
103
+ rdoc_options: []
104
+ require_paths:
105
+ - lib
106
+ required_ruby_version: !ruby/object:Gem::Requirement
107
+ none: false
108
+ requirements:
109
+ - - ! '>='
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ segments:
113
+ - 0
114
+ hash: -344049846899188400
115
+ required_rubygems_version: !ruby/object:Gem::Requirement
116
+ none: false
117
+ requirements:
118
+ - - ! '>='
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ requirements: []
122
+ rubyforge_project:
123
+ rubygems_version: 1.8.24
124
+ signing_key:
125
+ specification_version: 3
126
+ summary: Simple record flagging system for Mongoid documents
127
+ test_files:
128
+ - spec/helper.rb
129
+ - spec/mongoid-flags_spec.rb