mongoid-state_bits 0.1.0

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: cff50fbda762d896f4348e08bce7958ab8c9bff3
4
+ data.tar.gz: dcedf6d0e7d88495736dc7ff5f3ea8d582f40a9a
5
+ SHA512:
6
+ metadata.gz: 081e8a5c312fda195735f338af53feae57751b3a8954234ccc97aee6956144aab29258b98a00ededb80b505a65d84ca74736848694308c44ec2ad5faf091040a
7
+ data.tar.gz: ed79e03c1001c9dd6bdc5f9454e2946f19a25d7b6a19bdc3a000ebbbf186c4121e43b121b4230e28b9844eeb964b89f1646a31f23707a6cc50f207328af31be4
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in mongoid-state_bits.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 binz
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,42 @@
1
+ # Mongoid::StateBits
2
+
3
+ When making the database design, we often need to add a lot of Boolean type field, such as whether to open an article, whether to allow comments, whether original and so on. When only a Boolean field, direct statement of Boolean type is OK; When there are a lot of fields, it is clear eleven declarations harm than good, a waste of space and will bring a lot of duplicate code.
4
+
5
+ A common practice is to these boolean fields are integrated into an integer field, a binary bit represents a Boolean field. For an article, if 1 (the first bit) means open, 2 (second bit) means commentable, 4 means original, then an open, commentable, original article was marked as 7 (1 +2 +4), open, does not allow comments, original articles marked as 5 (1 +0 +4). Fields can be marked with an integer instead of multiple Boolean attributes, obviously it is good, but doing so will cause inconvenience on code management. For a programmer to deal directly with these flag bits will be a headache, and such code does not look elegant.
6
+
7
+ Mongoid::StateBits provides an elegant way to manage such situation.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ gem 'mongoid-state_bits'
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install mongoid-state_bits
22
+
23
+ ## Usage
24
+ Use the follwing code:
25
+
26
+ class Post
27
+ include Mongoid::Document
28
+ include Mongoid::StateBits
29
+ state_bits %w{deleted commentable original}
30
+ end
31
+
32
+ Three state_bits statement of three boolean field will generate three methods for each one, commentable/commentable?/commentable= () in `commentable` case, and also generate a `scope` corresponding to each field.
33
+
34
+ If you need to increase the field in the array can be directly% w {deleted commentable original} which added that all field default values ​​are false.
35
+
36
+ ## Contributing
37
+
38
+ 1. Fork it ( http://github.com/<my-github-username>/mongoid-state_bits/fork )
39
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
40
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
41
+ 4. Push to the branch (`git push origin my-new-feature`)
42
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ desc 'Run tests'
4
+ task :test do
5
+ system "bundle exec rspec spec"
6
+ end
@@ -0,0 +1,5 @@
1
+ module Mongoid
2
+ module StateBits
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,42 @@
1
+ require "mongoid/state_bits/version"
2
+
3
+ # class Post
4
+ # include Mongoid::Document
5
+ # include Mongoid::StateBits
6
+ # state_bits %w{public commentable original}
7
+ # end
8
+ # =====================================
9
+
10
+ module Mongoid
11
+ module StateBits
12
+
13
+ extend ActiveSupport::Concern
14
+
15
+ included do
16
+ field :state_bits, type: Integer, default: 0
17
+ index :state_bits => -1
18
+ end
19
+
20
+ module ClassMethods
21
+ def state_bits(bits)
22
+ bits.each_with_index do |bit, index|
23
+ define_method "#{bit}=" do |bool|
24
+ if bool
25
+ self.state_bits |= 2**index
26
+ else
27
+ self.state_bits -= (self.state_bits & 2**index)
28
+ end
29
+ end
30
+ define_method "#{bit}?" do
31
+ self.state_bits & 2**index != 0
32
+ end
33
+ alias_method("#{bit}", "#{bit}?")
34
+ # then define scopes
35
+ scope bit, ->{ where("this.state_bits & #{2**index}")}
36
+ scope "non#{bit}", ->{ where("this.state_bits & #{2**index} == 0")}
37
+ end
38
+ end
39
+ end #ClassMethods
40
+
41
+ end
42
+ 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/state_bits/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "mongoid-state_bits"
8
+ spec.version = Mongoid::StateBits::VERSION
9
+ spec.authors = ["Binz"]
10
+ spec.email = ["xinkiang@gmail.com"]
11
+ spec.summary = %q{Use one field to generate multiple boolean fields transparently.}
12
+ spec.description = %q{Merge multiple boolean fields into one field.}
13
+ spec.homepage = "http://creatist.cn/blog/201308/mongoidstatebits/"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.5"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+
25
+ spec.add_runtime_dependency "mongoid"
26
+ end
@@ -0,0 +1,25 @@
1
+ require 'mongoid'
2
+ require 'mongoid/state_bits'
3
+
4
+ class Post
5
+ include Mongoid::Document
6
+ include Mongoid::StateBits
7
+ state_bits %w{deleted commentable original}
8
+ end
9
+
10
+ describe Mongoid::StateBits do
11
+ it "should generate specified fields" do
12
+ p = Post.new
13
+ p.commentable.should be_false
14
+ p.commentable = true
15
+ p.commentable.should be_true
16
+ end
17
+
18
+ it "state_bits field sholud record state bits" do
19
+ p = Post.new
20
+ p.original = true
21
+ p.commentable = false
22
+ p.deleted = true
23
+ p.state_bits.should eq(5)
24
+ end
25
+ end
metadata ADDED
@@ -0,0 +1,110 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mongoid-state_bits
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Binz
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-02-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.5'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
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: rspec
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'
55
+ - !ruby/object:Gem::Dependency
56
+ name: mongoid
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Merge multiple boolean fields into one field.
70
+ email:
71
+ - xinkiang@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - Gemfile
78
+ - LICENSE.txt
79
+ - README.md
80
+ - Rakefile
81
+ - lib/mongoid/state_bits.rb
82
+ - lib/mongoid/state_bits/version.rb
83
+ - mongoid-state_bits.gemspec
84
+ - spec/mongoid-state_bits_spec.rb
85
+ homepage: http://creatist.cn/blog/201308/mongoidstatebits/
86
+ licenses:
87
+ - MIT
88
+ metadata: {}
89
+ post_install_message:
90
+ rdoc_options: []
91
+ require_paths:
92
+ - lib
93
+ required_ruby_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ required_rubygems_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ requirements: []
104
+ rubyforge_project:
105
+ rubygems_version: 2.2.2
106
+ signing_key:
107
+ specification_version: 4
108
+ summary: Use one field to generate multiple boolean fields transparently.
109
+ test_files:
110
+ - spec/mongoid-state_bits_spec.rb