mongoid-state_bits 0.1.0 → 0.2.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cff50fbda762d896f4348e08bce7958ab8c9bff3
4
- data.tar.gz: dcedf6d0e7d88495736dc7ff5f3ea8d582f40a9a
3
+ metadata.gz: 19044835ee724da47afa719178fcfb3bad1c51a2
4
+ data.tar.gz: ade16ed0092d23e1d3eab400603e832a9bfa3b3e
5
5
  SHA512:
6
- metadata.gz: 081e8a5c312fda195735f338af53feae57751b3a8954234ccc97aee6956144aab29258b98a00ededb80b505a65d84ca74736848694308c44ec2ad5faf091040a
7
- data.tar.gz: ed79e03c1001c9dd6bdc5f9454e2946f19a25d7b6a19bdc3a000ebbbf186c4121e43b121b4230e28b9844eeb964b89f1646a31f23707a6cc50f207328af31be4
6
+ metadata.gz: 68f38ced9d72c32f6721ef4d2cc9c58ebf0b7926d5839faf5dc65be076dbb8ce035a3e4ca6fb3a2b63090132eb2f4181320248a55045a316d5d85d275cfea833
7
+ data.tar.gz: 6a15429a9d66a481bed7e2e8e51f25b0812166f3e61bc791223e6dcf16940150ec3ca702789cab5c8e7d7e907fb0a4bbdcf601a97c1e40e6fcc383d62d918202
data/.gitignore CHANGED
File without changes
data/Gemfile CHANGED
File without changes
data/LICENSE.txt CHANGED
File without changes
data/README.md CHANGED
@@ -1,8 +1,8 @@
1
1
  # Mongoid::StateBits
2
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.
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 field, it is clear eleven declarations harm than good, a waste of space and will bring a lot of duplicate code.
4
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.
5
+ A common solution is that these boolean field 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). field 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
6
 
7
7
  Mongoid::StateBits provides an elegant way to manage such situation.
8
8
 
@@ -29,13 +29,13 @@ Use the follwing code:
29
29
  state_bits %w{deleted commentable original}
30
30
  end
31
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.
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
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.
34
+ If you need to add field, just push it on to the end of this array %w{deleted commentable original}, the default value of all field ​​is false(Boolean).
35
35
 
36
36
  ## Contributing
37
37
 
38
- 1. Fork it ( http://github.com/<my-github-username>/mongoid-state_bits/fork )
38
+ 1. Fork it ( http://github.com/Slacken/mongoid-state_bits/fork )
39
39
  2. Create your feature branch (`git checkout -b my-new-feature`)
40
40
  3. Commit your changes (`git commit -am 'Add some feature'`)
41
41
  4. Push to the branch (`git push origin my-new-feature`)
data/Rakefile CHANGED
File without changes
@@ -1,5 +1,5 @@
1
1
  module Mongoid
2
2
  module StateBits
3
- VERSION = "0.1.0"
3
+ VERSION = "0.2.0"
4
4
  end
5
5
  end
@@ -4,21 +4,32 @@ require "mongoid/state_bits/version"
4
4
  # include Mongoid::Document
5
5
  # include Mongoid::StateBits
6
6
  # state_bits %w{public commentable original}
7
+ #or state_bits public: true, commentable: false
7
8
  # end
8
9
  # =====================================
9
10
 
10
11
  module Mongoid
11
12
  module StateBits
12
-
13
13
  extend ActiveSupport::Concern
14
14
 
15
- included do
16
- field :state_bits, type: Integer, default: 0
15
+ included do
17
16
  index :state_bits => -1
18
17
  end
19
18
 
20
19
  module ClassMethods
21
- def state_bits(bits)
20
+ def state_bits(params)
21
+ if params.is_a? Array
22
+ bits = params
23
+ default = 0
24
+ elsif params.is_a? Hash
25
+ bits = params.keys
26
+ default = bits.each_with_index.inject(0){|sum, vi| sum + (params[vi[0]] ? 2**vi[1] : 0)}
27
+ else
28
+ raise "Invalid state_bits params: should be Array or Hash"
29
+ end
30
+ # declare the field
31
+ field :state_bits, type: Integer, default: default
32
+ # define methods
22
33
  bits.each_with_index do |bit, index|
23
34
  define_method "#{bit}=" do |bool|
24
35
  if bool
@@ -27,10 +38,10 @@ module Mongoid
27
38
  self.state_bits -= (self.state_bits & 2**index)
28
39
  end
29
40
  end
30
- define_method "#{bit}?" do
41
+ define_method bit do
31
42
  self.state_bits & 2**index != 0
32
43
  end
33
- alias_method("#{bit}", "#{bit}?")
44
+ alias_method "#{bit}?", "#{bit}"
34
45
  # then define scopes
35
46
  scope bit, ->{ where("this.state_bits & #{2**index}")}
36
47
  scope "non#{bit}", ->{ where("this.state_bits & #{2**index} == 0")}
File without changes
@@ -7,6 +7,12 @@ class Post
7
7
  state_bits %w{deleted commentable original}
8
8
  end
9
9
 
10
+ class User
11
+ include Mongoid::Document
12
+ include Mongoid::StateBits
13
+ state_bits admin: true, deleted: false
14
+ end
15
+
10
16
  describe Mongoid::StateBits do
11
17
  it "should generate specified fields" do
12
18
  p = Post.new
@@ -22,4 +28,10 @@ describe Mongoid::StateBits do
22
28
  p.deleted = true
23
29
  p.state_bits.should eq(5)
24
30
  end
31
+
32
+ it "supports default value definition" do
33
+ u = User.new
34
+ u.admin.should be_true
35
+ u.deleted.should be_false
36
+ end
25
37
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid-state_bits
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Binz
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-02-13 00:00:00.000000000 Z
11
+ date: 2014-06-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler