bitmask 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +56 -0
- data/Rakefile +10 -0
- data/bitmask.gemspec +21 -0
- data/lib/bitmask/version.rb +3 -0
- data/lib/bitmask.rb +93 -0
- data/test/bitmask_test.rb +94 -0
- data/test/test_helper.rb +4 -0
- metadata +59 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Amiel Martin
|
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,56 @@
|
|
1
|
+
# Bitmask
|
2
|
+
|
3
|
+
Bitmask represents a set of boolean values as an Integer.
|
4
|
+
|
5
|
+
This bitmask gem was extracted from [amiel/has_bitmask_attributes](https://github.com/amiel/has_bitmask_attributes)
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
gem 'bitmask'
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install bitmask
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
|
24
|
+
Examples:
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
masks = {
|
28
|
+
:cat => 0b0001,
|
29
|
+
:dog => 0b0010,
|
30
|
+
:fish => 0b0100,
|
31
|
+
}
|
32
|
+
|
33
|
+
bitmask = Bitmask.new(masks, {:cat => true})
|
34
|
+
bitmask.to_i # => 1
|
35
|
+
bitmask.get :cat # => true
|
36
|
+
bitmask.get :dog # => false
|
37
|
+
bitmask.set :dog, true
|
38
|
+
bitmask.get :dog # => true
|
39
|
+
bitmask.to_i # => 3
|
40
|
+
bitmask.to_h # => { :cat => true, :dog => true, :fish => false }
|
41
|
+
|
42
|
+
bitmask = Bitmask.new(masks, 0b101)
|
43
|
+
bitmask.to_h # => { :cat => true, :dog => false, :fish => true }
|
44
|
+
bitmask.to_i # => 5
|
45
|
+
bitmask.to_i.to_s(2) # => "101"
|
46
|
+
|
47
|
+
Bitmask.new(masks, 5) == Bitmask.new(masks, { :cat => true, :dog => false, :fish => true }) # => true
|
48
|
+
```
|
49
|
+
|
50
|
+
## Contributing
|
51
|
+
|
52
|
+
1. Fork it
|
53
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
54
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
55
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
56
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/bitmask.gemspec
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/bitmask/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Amiel Martin"]
|
6
|
+
gem.email = ["amiel@carnesmedia.com"]
|
7
|
+
gem.description = %q{Represents a set of boolean values as an Integer}
|
8
|
+
gem.summary = <<-SUMMARY
|
9
|
+
Bitmask represents a set of boolean values as an Integer using bitwise arithmetic.
|
10
|
+
|
11
|
+
This allows, for example, for a set of boolean settings to be stored in one
|
12
|
+
integer column in a database.
|
13
|
+
SUMMARY
|
14
|
+
gem.homepage = "https://github.com/amiel/bitmask"
|
15
|
+
|
16
|
+
gem.files = `git ls-files`.split($\)
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test)/})
|
18
|
+
gem.name = "bitmask"
|
19
|
+
gem.require_paths = ["lib"]
|
20
|
+
gem.version = Bitmask::VERSION
|
21
|
+
end
|
data/lib/bitmask.rb
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
require "bitmask/version"
|
2
|
+
|
3
|
+
# Bitmask represents a set of boolean values as an Integer.
|
4
|
+
#
|
5
|
+
# Examples:
|
6
|
+
#
|
7
|
+
# masks = {
|
8
|
+
# :cat => 0b0001,
|
9
|
+
# :dog => 0b0010,
|
10
|
+
# :fish => 0b0100
|
11
|
+
# }
|
12
|
+
#
|
13
|
+
# bitmask = Bitmask.new(masks, {:cat => true})
|
14
|
+
# bitmask.to_i # => 1
|
15
|
+
# bitmask.get :cat # => true
|
16
|
+
# bitmask.get :dog # => false
|
17
|
+
# bitmask.set :dog, true
|
18
|
+
# bitmask.get :dog # => true
|
19
|
+
# bitmask.to_i # => 3
|
20
|
+
# bitmask.to_h # => { :cat => true, :dog => true, :fish => false }
|
21
|
+
#
|
22
|
+
# bitmask = Bitmask.new(masks, 0b101)
|
23
|
+
# bitmask.to_h # => { :cat => true, :dog => false, :fish => true }
|
24
|
+
# bitmask.to_i # => 5
|
25
|
+
# bitmask.to_i.to_s(2) # => "101"
|
26
|
+
#
|
27
|
+
# Bitmask.new(masks, 5) == Bitmask.new(masks, { :cat => true, :dog => false, :fish => true }) # => true
|
28
|
+
#
|
29
|
+
class Bitmask
|
30
|
+
|
31
|
+
def ==(other)
|
32
|
+
other.masks == @masks && other.to_i == self.to_i
|
33
|
+
end
|
34
|
+
|
35
|
+
# create an object with which to manipulate an integer as a set of boolean values
|
36
|
+
#
|
37
|
+
# arguments
|
38
|
+
# masks:: a Hash where the key is the boolean attribute and the value is the bitmask.
|
39
|
+
# { :some_attribute => 0b0001, :another_attribute => 0b0010 }
|
40
|
+
# arg:: can be a Hash or Integer
|
41
|
+
def initialize(masks, arg)
|
42
|
+
@masks = masks
|
43
|
+
case arg
|
44
|
+
when Hash
|
45
|
+
@data = 0
|
46
|
+
arg.each do |attr, value|
|
47
|
+
set attr, value
|
48
|
+
end
|
49
|
+
else
|
50
|
+
@data = arg.to_i
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
attr_reader :masks
|
55
|
+
|
56
|
+
def to_i
|
57
|
+
@data
|
58
|
+
end
|
59
|
+
|
60
|
+
def to_a
|
61
|
+
@masks.keys.sort { |a,b| @masks[a] <=> @masks[b] }.collect { |k| [k, get(k)] }
|
62
|
+
end
|
63
|
+
|
64
|
+
def each(&blk)
|
65
|
+
to_a.each(&blk)
|
66
|
+
end
|
67
|
+
|
68
|
+
def to_h
|
69
|
+
@masks.keys.inject({}) { |h, k| h[k] = get k; h }
|
70
|
+
end
|
71
|
+
|
72
|
+
# returns boolean value
|
73
|
+
def get(attr)
|
74
|
+
(@data & @masks[attr]) == @masks[attr]
|
75
|
+
end
|
76
|
+
|
77
|
+
# expects a boolean value
|
78
|
+
def set(attr, value)
|
79
|
+
raise ArgumentError, "unknown attribute: #{attr}" unless @masks[attr]
|
80
|
+
case value
|
81
|
+
when true
|
82
|
+
@data |= @masks[attr]
|
83
|
+
when false
|
84
|
+
@data &= ~@masks[attr]
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
def set_array(array)
|
89
|
+
@masks.each do |attr, value|
|
90
|
+
set attr, array.include?(attr)
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
@@ -0,0 +1,94 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class BitmaskTest < Test::Unit::TestCase
|
4
|
+
TEST_MASKS = {
|
5
|
+
:phone => 0b0000001,
|
6
|
+
:name => 0b0000010,
|
7
|
+
:gender => 0b0000100,
|
8
|
+
:email => 0b0001000,
|
9
|
+
:birthday => 0b0100000,
|
10
|
+
:location => 0b1000000,
|
11
|
+
}
|
12
|
+
# Replace this with your real tests.
|
13
|
+
def test_get
|
14
|
+
bitmask = Bitmask.new TEST_MASKS, :phone => true, :name => true, :email => true
|
15
|
+
assert bitmask.get(:phone)
|
16
|
+
assert bitmask.get(:name)
|
17
|
+
assert bitmask.get(:email)
|
18
|
+
assert !bitmask.get(:gender)
|
19
|
+
assert !bitmask.get(:birthday)
|
20
|
+
assert !bitmask.get(:location)
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_set
|
24
|
+
bitmask = Bitmask.new TEST_MASKS, 0
|
25
|
+
assert !bitmask.get(:phone)
|
26
|
+
bitmask.set(:phone, true)
|
27
|
+
assert bitmask.get(:phone)
|
28
|
+
assert !bitmask.get(:email)
|
29
|
+
bitmask.set(:email, true)
|
30
|
+
assert bitmask.get(:email)
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_to_h
|
34
|
+
bitmask = Bitmask.new TEST_MASKS, :phone => true, :name => true, :email => true
|
35
|
+
assert_equal({:phone => true, :name => true, :email => true, :gender => false, :birthday => false, :location => false}, bitmask.to_h)
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_to_a
|
39
|
+
bitmask = Bitmask.new TEST_MASKS, :phone => true, :name => true, :email => true
|
40
|
+
assert_equal([
|
41
|
+
[:phone, true ],
|
42
|
+
[:name, true ],
|
43
|
+
[:gender, false],
|
44
|
+
[:email, true ],
|
45
|
+
[:birthday, false],
|
46
|
+
[:location, false]
|
47
|
+
], bitmask.to_a)
|
48
|
+
end
|
49
|
+
|
50
|
+
def test_defaults
|
51
|
+
bitmask = Bitmask.new(TEST_MASKS, nil || {:phone => true, :email => true})
|
52
|
+
assert bitmask.get(:phone)
|
53
|
+
assert !bitmask.get(:name)
|
54
|
+
assert bitmask.get(:email)
|
55
|
+
assert !bitmask.get(:gender)
|
56
|
+
assert !bitmask.get(:birthday)
|
57
|
+
assert !bitmask.get(:location)
|
58
|
+
end
|
59
|
+
|
60
|
+
def test_to_i_and_create_from_integer
|
61
|
+
bitmask = Bitmask.new TEST_MASKS, :phone => true, :name => true, :email => true
|
62
|
+
bitmask_two = Bitmask.new(TEST_MASKS, bitmask.to_i)
|
63
|
+
assert_equal(bitmask_two.to_h, {:phone => true, :name => true, :email => true, :gender => false, :birthday => false, :location => false})
|
64
|
+
end
|
65
|
+
|
66
|
+
def test_equality
|
67
|
+
bitmask_one = Bitmask.new TEST_MASKS, :phone => true, :name => true, :email => true
|
68
|
+
bitmask_two = Bitmask.new TEST_MASKS, :phone => true, :name => true, :email => true
|
69
|
+
bitmask_three = Bitmask.new TEST_MASKS, :phone => true, :name => true, :email => false
|
70
|
+
assert bitmask_one == bitmask_two
|
71
|
+
assert bitmask_two != bitmask_three
|
72
|
+
end
|
73
|
+
|
74
|
+
def test_set_array
|
75
|
+
bitmask = Bitmask.new TEST_MASKS, :phone => true, :name => true
|
76
|
+
bitmask.set_array [:phone, :email]
|
77
|
+
assert_equal(bitmask.to_h, {:phone => true, :name => false, :email => true, :gender => false, :birthday => false, :location => false})
|
78
|
+
end
|
79
|
+
|
80
|
+
def test_set_raises
|
81
|
+
bitmask = Bitmask.new TEST_MASKS, 0
|
82
|
+
assert_raises ArgumentError do
|
83
|
+
bitmask.set :foo, true
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
def test_set_array_doesnt_raise
|
88
|
+
bitmask = Bitmask.new TEST_MASKS, :phone => true, :name => true
|
89
|
+
assert_nothing_raised do
|
90
|
+
bitmask.set_array [:phone, :email, :foodebar]
|
91
|
+
end
|
92
|
+
assert_equal(bitmask.to_h, {:phone => true, :name => false, :email => true, :gender => false, :birthday => false, :location => false})
|
93
|
+
end
|
94
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: bitmask
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Amiel Martin
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-05-01 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Represents a set of boolean values as an Integer
|
15
|
+
email:
|
16
|
+
- amiel@carnesmedia.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- .gitignore
|
22
|
+
- Gemfile
|
23
|
+
- LICENSE
|
24
|
+
- README.md
|
25
|
+
- Rakefile
|
26
|
+
- bitmask.gemspec
|
27
|
+
- lib/bitmask.rb
|
28
|
+
- lib/bitmask/version.rb
|
29
|
+
- test/bitmask_test.rb
|
30
|
+
- test/test_helper.rb
|
31
|
+
homepage: https://github.com/amiel/bitmask
|
32
|
+
licenses: []
|
33
|
+
post_install_message:
|
34
|
+
rdoc_options: []
|
35
|
+
require_paths:
|
36
|
+
- lib
|
37
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
38
|
+
none: false
|
39
|
+
requirements:
|
40
|
+
- - ! '>='
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '0'
|
43
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
44
|
+
none: false
|
45
|
+
requirements:
|
46
|
+
- - ! '>='
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
49
|
+
requirements: []
|
50
|
+
rubyforge_project:
|
51
|
+
rubygems_version: 1.8.10
|
52
|
+
signing_key:
|
53
|
+
specification_version: 3
|
54
|
+
summary: Bitmask represents a set of boolean values as an Integer using bitwise arithmetic. This
|
55
|
+
allows, for example, for a set of boolean settings to be stored in one integer column
|
56
|
+
in a database.
|
57
|
+
test_files:
|
58
|
+
- test/bitmask_test.rb
|
59
|
+
- test/test_helper.rb
|