bitsy 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.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/.rspec +2 -0
- data/.travis.yml +3 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +79 -0
- data/Rakefile +6 -0
- data/bitsy.gemspec +24 -0
- data/lib/bitsy.rb +125 -0
- data/lib/bitsy/mask.rb +45 -0
- data/lib/bitsy/version.rb +3 -0
- data/spec/bitsy_spec.rb +206 -0
- data/spec/mask_spec.rb +76 -0
- data/spec/spec_helper.rb +2 -0
- metadata +103 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 9f9a51f778b9f04814432b25a2819b69cbef25e1
|
4
|
+
data.tar.gz: e024e744aab53793e2893440c732f7113ac0c8c3
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e96f0fe2a70cd9a3435e245bb35c1f63c0fbb9dc413c3e8898eed4eb0c6deb18a48ca83b15a59ec173c5a085c8660e48e8a1d01de457413fadd9d9709a8656b7
|
7
|
+
data.tar.gz: 53a169d20b938c007ae658c8e128d60cdacd03f9748dc6e07418405cbd273482f467ea29cf0d00a6666653c86506af250647ae832dd9c8bd528b756001e07e4d
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Oliver Nightingale
|
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,79 @@
|
|
1
|
+
# Bitsy
|
2
|
+
|
3
|
+
A simple bitmask in ruby.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'bitsy'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install bitsy
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
Create a bit mask class:
|
22
|
+
|
23
|
+
class UserRoles < Bitsy
|
24
|
+
flags :admin, :moderator, :author, :banned
|
25
|
+
end
|
26
|
+
|
27
|
+
Then use this class to track which flags are set.
|
28
|
+
|
29
|
+
user_roles = UserRoles.new
|
30
|
+
user_roles.has_admin? # false
|
31
|
+
user_roles << :admin
|
32
|
+
user_roles.has_admin? # true
|
33
|
+
|
34
|
+
### Checking for flags
|
35
|
+
|
36
|
+
You can check for roles with the generated methods, e.g.
|
37
|
+
|
38
|
+
user_roles.has_admin_and_moderator_and_author
|
39
|
+
user_roles.has_admin_or_author
|
40
|
+
|
41
|
+
You can also use the underlying methods `every` and `some`:
|
42
|
+
|
43
|
+
user_roles.every(:admin, :moderator, :author)
|
44
|
+
user_roles.some(:admin, :author)
|
45
|
+
|
46
|
+
### Manipulating flags
|
47
|
+
|
48
|
+
Flags can be set, unset and toggled:
|
49
|
+
|
50
|
+
user_roles.set(:admin)
|
51
|
+
user_roles.unset(:author)
|
52
|
+
user_roles.toggle(:moderator)
|
53
|
+
|
54
|
+
user_roles << :banned
|
55
|
+
|
56
|
+
### Masks
|
57
|
+
|
58
|
+
Masks are automatically created for the flags you specify:
|
59
|
+
|
60
|
+
UserRoles::ADMIN
|
61
|
+
UserRoles::MODERATOR
|
62
|
+
UserRoles::AUTHOR
|
63
|
+
UserRoles::BANNED
|
64
|
+
|
65
|
+
Each of these is a `Bitsy::Mask`. These can be used to manipulate the flags directly, e.g. to set the `:banned` flag we can do:
|
66
|
+
|
67
|
+
user_roles |= UserRoles::BANNED
|
68
|
+
|
69
|
+
Masks can be combined to form composite masks:
|
70
|
+
|
71
|
+
UserRoles::ADMIN_AND_AUTHOR = UserRoles::ADMIN | UserRoles::AUTHOR
|
72
|
+
|
73
|
+
## Contributing
|
74
|
+
|
75
|
+
1. Fork it
|
76
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
77
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
78
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
79
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/bitsy.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'bitsy/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "bitsy"
|
8
|
+
spec.version = Bitsy::VERSION
|
9
|
+
spec.authors = ["Oliver Nightingale"]
|
10
|
+
spec.email = ["oliver.nightingale1@gmail.com"]
|
11
|
+
spec.description = %q{Simple Bitmask}
|
12
|
+
spec.summary = %q{Simple Bitmask}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
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.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "rspec"
|
24
|
+
end
|
data/lib/bitsy.rb
ADDED
@@ -0,0 +1,125 @@
|
|
1
|
+
require "bitsy/version"
|
2
|
+
require "bitsy/mask"
|
3
|
+
|
4
|
+
class Bitsy
|
5
|
+
class InvalidFlagError < StandardError ; end
|
6
|
+
|
7
|
+
def self.flags(*flags)
|
8
|
+
if flags.empty?
|
9
|
+
@flags
|
10
|
+
else
|
11
|
+
@flags = flags
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.masks
|
16
|
+
@masks ||= flags.inject({}) do |memo, flag|
|
17
|
+
mask = Bitsy::Mask.with_index(flag, self.flags.index(flag))
|
18
|
+
self.const_set(flag.upcase, mask)
|
19
|
+
memo[flag] = mask
|
20
|
+
memo
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def initialize(val = 0)
|
25
|
+
self.value = val
|
26
|
+
end
|
27
|
+
|
28
|
+
def to_i
|
29
|
+
value
|
30
|
+
end
|
31
|
+
|
32
|
+
def to_a
|
33
|
+
self.class.masks.each_with_object([]) do |(_, mask), memo|
|
34
|
+
memo << mask.flag unless (value & mask.value).zero?
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def method_missing(name, *args, &block)
|
39
|
+
super unless name.match(/^has_/)
|
40
|
+
|
41
|
+
if name.match(/_or_/)
|
42
|
+
some(*name.to_s.gsub(/^has_/, '').split(/_or_/))
|
43
|
+
else
|
44
|
+
every(*name.to_s.gsub(/^has_/, '').split(/_and_/))
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def every(*flags)
|
49
|
+
flags.inject(true) do |memo, flag|
|
50
|
+
mask = self.class.masks.fetch(flag.to_sym, nil)
|
51
|
+
raise InvalidFlagError.new(flag) unless mask
|
52
|
+
memo && !(value & mask.value).zero?
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def some(*flags)
|
57
|
+
flags.inject(false) do |memo, flag|
|
58
|
+
mask = self.class.masks.fetch(flag.to_sym, nil)
|
59
|
+
raise InvalidFlagError.new(flag) unless mask
|
60
|
+
memo || !(value & mask.value).zero?
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def set(*flags)
|
65
|
+
self.value = flags
|
66
|
+
end
|
67
|
+
|
68
|
+
alias_method :<<, :set
|
69
|
+
|
70
|
+
def unset(*flags)
|
71
|
+
masks_for_flags(flags) do |mask|
|
72
|
+
self.value = value & ~mask.value
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
def toggle(*flags)
|
77
|
+
masks_for_flags(flags) do |mask|
|
78
|
+
self.value = value ^ mask.value
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
def &(other)
|
83
|
+
self.class.new(value & other.to_i)
|
84
|
+
end
|
85
|
+
|
86
|
+
def |(other)
|
87
|
+
self.class.new(value | other.to_i)
|
88
|
+
end
|
89
|
+
|
90
|
+
def ^(other)
|
91
|
+
self.class.new(value ^ other.to_i)
|
92
|
+
end
|
93
|
+
|
94
|
+
private
|
95
|
+
|
96
|
+
attr_reader :value
|
97
|
+
|
98
|
+
def masks_for_flags(flags)
|
99
|
+
flags.each do |flag|
|
100
|
+
mask = self.class.masks.fetch(flag.to_sym, nil)
|
101
|
+
raise InvalidFlagError unless mask
|
102
|
+
yield mask
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
def value=(val)
|
107
|
+
if val.is_a? Integer
|
108
|
+
@value = val
|
109
|
+
|
110
|
+
elsif val.is_a? Array
|
111
|
+
@value = 0 if @value.nil?
|
112
|
+
|
113
|
+
val.each do |flag|
|
114
|
+
mask = self.class.masks.fetch(flag.to_sym, nil)
|
115
|
+
raise InvalidFlagError unless mask
|
116
|
+
|
117
|
+
@value |= mask.value
|
118
|
+
end
|
119
|
+
|
120
|
+
else
|
121
|
+
raise ArgumentError
|
122
|
+
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
data/lib/bitsy/mask.rb
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
class Bitsy
|
2
|
+
class Mask
|
3
|
+
|
4
|
+
attr_reader :flag, :value
|
5
|
+
|
6
|
+
def self.with_index(flag, idx)
|
7
|
+
self.new(flag, (1 << idx))
|
8
|
+
end
|
9
|
+
|
10
|
+
def initialize(flag, value)
|
11
|
+
@flag = flag
|
12
|
+
@value = value
|
13
|
+
end
|
14
|
+
|
15
|
+
def to_i
|
16
|
+
value
|
17
|
+
end
|
18
|
+
|
19
|
+
def &(other)
|
20
|
+
self.class.new(combine_flag(other, 'AND'), value & other.to_i)
|
21
|
+
end
|
22
|
+
|
23
|
+
def |(other)
|
24
|
+
self.class.new(combine_flag(other, 'OR'), value | other.to_i)
|
25
|
+
end
|
26
|
+
|
27
|
+
def ^(other)
|
28
|
+
self.class.new(combine_flag(other, 'XOR'), value ^ other.to_i)
|
29
|
+
end
|
30
|
+
|
31
|
+
def ~
|
32
|
+
self.class.new("NOT_#{flag}".to_sym, ~value)
|
33
|
+
end
|
34
|
+
|
35
|
+
private
|
36
|
+
|
37
|
+
def combine_flag(other, operation)
|
38
|
+
if other.respond_to?(:flag)
|
39
|
+
"#{flag}_#{operation}_#{other.flag}".to_sym
|
40
|
+
else
|
41
|
+
"#{flag}_#{operation}_#{other}".to_sym
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
data/spec/bitsy_spec.rb
ADDED
@@ -0,0 +1,206 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
class Prefs < Bitsy
|
4
|
+
flags :create, :update, :blocked, :donedone, :complete, :destroy
|
5
|
+
end
|
6
|
+
|
7
|
+
describe Bitsy do
|
8
|
+
|
9
|
+
describe "#initialize" do
|
10
|
+
context "no value" do
|
11
|
+
subject { Prefs.new }
|
12
|
+
its(:to_i) { should == 0 }
|
13
|
+
its(:to_a) { should be_empty }
|
14
|
+
end
|
15
|
+
|
16
|
+
context "integer value" do
|
17
|
+
subject { Prefs.new(2) }
|
18
|
+
its(:to_i) { should == 2 }
|
19
|
+
its(:to_a) { should == [:update] }
|
20
|
+
end
|
21
|
+
|
22
|
+
context "array value" do
|
23
|
+
context "empty array" do
|
24
|
+
subject { Prefs.new([]) }
|
25
|
+
its(:to_i) { should == 0 }
|
26
|
+
its(:to_a) { should be_empty }
|
27
|
+
end
|
28
|
+
|
29
|
+
context "non empty array" do
|
30
|
+
context "valid flags" do
|
31
|
+
let(:flags) { [:create, :update] }
|
32
|
+
subject { Prefs.new(flags) }
|
33
|
+
its(:to_i) { should == 3 }
|
34
|
+
its(:to_a) { should == flags }
|
35
|
+
end
|
36
|
+
|
37
|
+
context "invalid flags" do
|
38
|
+
let(:flags) { [:invalid] }
|
39
|
+
|
40
|
+
it "should raise a InvalidFlagError" do
|
41
|
+
expect {
|
42
|
+
Prefs.new(flags)
|
43
|
+
}.to raise_error(Bitsy::InvalidFlagError)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
describe "mask constants" do
|
51
|
+
it "should set a constant for each flag" do
|
52
|
+
Prefs.flags.each do |flag|
|
53
|
+
Prefs.const_defined?(flag.upcase, false).should be_true
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
describe ".masks" do
|
59
|
+
subject { Prefs.masks }
|
60
|
+
its(:size) { should == 6 }
|
61
|
+
its([:create]) { should be_a Bitsy::Mask }
|
62
|
+
end
|
63
|
+
|
64
|
+
describe "#has_*" do
|
65
|
+
subject { Prefs.new([:create, :update]) }
|
66
|
+
|
67
|
+
context "single flag" do
|
68
|
+
its(:has_create) { should be_true }
|
69
|
+
its(:has_donedone) { should be_false }
|
70
|
+
end
|
71
|
+
|
72
|
+
context "multiple and flag" do
|
73
|
+
its(:has_create_and_donedone) { should be_false }
|
74
|
+
its(:has_create_and_update) { should be_true }
|
75
|
+
end
|
76
|
+
|
77
|
+
context "multiple or flag" do
|
78
|
+
its(:has_create_or_donedone) { should be_true }
|
79
|
+
its(:has_create_or_update) { should be_true }
|
80
|
+
its(:has_blocked_or_donedone) { should be_false }
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
describe "#set" do
|
85
|
+
context "flag is unset" do
|
86
|
+
subject { Prefs.new }
|
87
|
+
before { subject.set(:create) }
|
88
|
+
its(:has_create) { should be_true }
|
89
|
+
end
|
90
|
+
|
91
|
+
context "flag is set" do
|
92
|
+
subject { Prefs.new([:create]) }
|
93
|
+
before { subject.set(:create) }
|
94
|
+
its(:has_create) { should be_true }
|
95
|
+
end
|
96
|
+
|
97
|
+
context "unrecognised flag" do
|
98
|
+
let(:prefs) { Prefs.new }
|
99
|
+
|
100
|
+
it "should raise InvalidFlagError" do
|
101
|
+
expect {
|
102
|
+
prefs.set(:invalid)
|
103
|
+
}.to raise_error(Bitsy::InvalidFlagError)
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
describe "#unset" do
|
109
|
+
context "flag is unset" do
|
110
|
+
subject { Prefs.new }
|
111
|
+
before { subject.unset(:create) }
|
112
|
+
its(:has_create) { should_not be_true }
|
113
|
+
end
|
114
|
+
|
115
|
+
context "flag is set" do
|
116
|
+
subject { Prefs.new([:create]) }
|
117
|
+
before { subject.unset(:create) }
|
118
|
+
its(:has_create) { should_not be_true }
|
119
|
+
end
|
120
|
+
|
121
|
+
context "unrecognised flag" do
|
122
|
+
let(:prefs) { Prefs.new }
|
123
|
+
|
124
|
+
it "should raise InvalidFlagError" do
|
125
|
+
expect {
|
126
|
+
prefs.unset(:invalid)
|
127
|
+
}.to raise_error(Bitsy::InvalidFlagError)
|
128
|
+
end
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
describe "#toggle" do
|
133
|
+
context "flag is unset" do
|
134
|
+
subject { Prefs.new }
|
135
|
+
before { subject.toggle(:create) }
|
136
|
+
its(:has_create) { should be_true }
|
137
|
+
end
|
138
|
+
|
139
|
+
context "flag is set" do
|
140
|
+
subject { Prefs.new([:create]) }
|
141
|
+
before { subject.toggle(:create) }
|
142
|
+
its(:has_create) { should_not be_true }
|
143
|
+
end
|
144
|
+
|
145
|
+
context "unrecognised flag" do
|
146
|
+
let(:prefs) { Prefs.new }
|
147
|
+
|
148
|
+
it "should raise InvalidFlagError" do
|
149
|
+
expect {
|
150
|
+
prefs.toggle(:invalid)
|
151
|
+
}.to raise_error(Bitsy::InvalidFlagError)
|
152
|
+
end
|
153
|
+
end
|
154
|
+
end
|
155
|
+
|
156
|
+
describe "#&" do
|
157
|
+
let(:prefs) { Prefs.new(1) }
|
158
|
+
|
159
|
+
context "with an integer" do
|
160
|
+
subject { prefs & 1 }
|
161
|
+
its(:to_i) { should == 1 }
|
162
|
+
it { should be_kind_of Prefs }
|
163
|
+
end
|
164
|
+
|
165
|
+
context "with a mask" do
|
166
|
+
let(:mask) { Bitsy::Mask.new(:flag, 1) }
|
167
|
+
subject { prefs & mask }
|
168
|
+
its(:to_i) { should == 1 }
|
169
|
+
it { should be_kind_of Prefs }
|
170
|
+
end
|
171
|
+
end
|
172
|
+
|
173
|
+
describe "#|" do
|
174
|
+
let(:prefs) { Prefs.new(1) }
|
175
|
+
|
176
|
+
context "with an integer" do
|
177
|
+
subject { prefs | 2 }
|
178
|
+
its(:to_i) { should == 3 }
|
179
|
+
it { should be_kind_of Prefs }
|
180
|
+
end
|
181
|
+
|
182
|
+
context "with a mask" do
|
183
|
+
let(:mask) { Bitsy::Mask.new(:flag, 2) }
|
184
|
+
subject { prefs | mask }
|
185
|
+
its(:to_i) { should == 3 }
|
186
|
+
it { should be_kind_of Prefs }
|
187
|
+
end
|
188
|
+
end
|
189
|
+
|
190
|
+
describe "#^" do
|
191
|
+
let(:prefs) { Prefs.new(2) }
|
192
|
+
|
193
|
+
context "with an integer" do
|
194
|
+
subject { prefs ^ 2 }
|
195
|
+
its(:to_i) { should == 0 }
|
196
|
+
it { should be_kind_of Prefs }
|
197
|
+
end
|
198
|
+
|
199
|
+
context "with a mask" do
|
200
|
+
let(:mask) { Bitsy::Mask.new(:flag, 2) }
|
201
|
+
subject { prefs ^ mask }
|
202
|
+
its(:to_i) { should == 0 }
|
203
|
+
it { should be_kind_of Prefs }
|
204
|
+
end
|
205
|
+
end
|
206
|
+
end
|
data/spec/mask_spec.rb
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Bitsy::Mask do
|
4
|
+
|
5
|
+
describe ".with_index" do
|
6
|
+
subject { Bitsy::Mask.with_index(:foo, 4) }
|
7
|
+
|
8
|
+
its(:value) { should == 16 }
|
9
|
+
its(:flag) { should == :foo }
|
10
|
+
its(:to_i) { should == 16 }
|
11
|
+
end
|
12
|
+
|
13
|
+
describe "#&" do
|
14
|
+
let(:mask) { Bitsy::Mask.new(:foo, 1) }
|
15
|
+
|
16
|
+
context "with an integer" do
|
17
|
+
subject { mask & 1 }
|
18
|
+
its(:to_i) { should == 1 }
|
19
|
+
its(:flag) { should == :foo_AND_1 }
|
20
|
+
it { should be_kind_of Bitsy::Mask }
|
21
|
+
end
|
22
|
+
|
23
|
+
context "with a mask" do
|
24
|
+
let(:other_mask) { Bitsy::Mask.new(:flag, 1) }
|
25
|
+
subject { mask & other_mask }
|
26
|
+
its(:to_i) { should == 1 }
|
27
|
+
its(:flag) { should == :foo_AND_flag }
|
28
|
+
it { should be_kind_of Bitsy::Mask }
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
describe "#|" do
|
33
|
+
let(:mask) { Bitsy::Mask.new(:foo, 1) }
|
34
|
+
|
35
|
+
context "with an integer" do
|
36
|
+
subject { mask | 2 }
|
37
|
+
its(:to_i) { should == 3 }
|
38
|
+
its(:flag) { should == :foo_OR_2 }
|
39
|
+
it { should be_kind_of Bitsy::Mask }
|
40
|
+
end
|
41
|
+
|
42
|
+
context "with a mask" do
|
43
|
+
let(:other_mask) { Bitsy::Mask.new(:flag, 2) }
|
44
|
+
subject { mask | other_mask }
|
45
|
+
its(:to_i) { should == 3 }
|
46
|
+
its(:flag) { should == :foo_OR_flag }
|
47
|
+
it { should be_kind_of Bitsy::Mask }
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
describe "#^" do
|
52
|
+
let(:mask) { Bitsy::Mask.new(:foo, 2) }
|
53
|
+
|
54
|
+
context "with an integer" do
|
55
|
+
subject { mask ^ 2 }
|
56
|
+
its(:to_i) { should == 0 }
|
57
|
+
its(:flag) { should == :foo_XOR_2 }
|
58
|
+
it { should be_kind_of Bitsy::Mask }
|
59
|
+
end
|
60
|
+
|
61
|
+
context "with a mask" do
|
62
|
+
let(:other_mask) { Bitsy::Mask.new(:flag, 2) }
|
63
|
+
subject { mask ^ other_mask }
|
64
|
+
its(:to_i) { should == 0 }
|
65
|
+
its(:flag) { should == :foo_XOR_flag }
|
66
|
+
it { should be_kind_of Bitsy::Mask }
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
describe "#~" do
|
71
|
+
let(:mask) { Bitsy::Mask.new(:foo, 1) }
|
72
|
+
subject { ~mask }
|
73
|
+
its(:to_i) { should == -2 }
|
74
|
+
its(:flag) { should == :NOT_foo }
|
75
|
+
end
|
76
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,103 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: bitsy
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Oliver Nightingale
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-06-03 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.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
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
|
+
description: Simple Bitmask
|
56
|
+
email:
|
57
|
+
- oliver.nightingale1@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- .gitignore
|
63
|
+
- .rspec
|
64
|
+
- .travis.yml
|
65
|
+
- Gemfile
|
66
|
+
- LICENSE.txt
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- bitsy.gemspec
|
70
|
+
- lib/bitsy.rb
|
71
|
+
- lib/bitsy/mask.rb
|
72
|
+
- lib/bitsy/version.rb
|
73
|
+
- spec/bitsy_spec.rb
|
74
|
+
- spec/mask_spec.rb
|
75
|
+
- spec/spec_helper.rb
|
76
|
+
homepage: ''
|
77
|
+
licenses:
|
78
|
+
- MIT
|
79
|
+
metadata: {}
|
80
|
+
post_install_message:
|
81
|
+
rdoc_options: []
|
82
|
+
require_paths:
|
83
|
+
- lib
|
84
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - '>='
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0'
|
89
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
90
|
+
requirements:
|
91
|
+
- - '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
requirements: []
|
95
|
+
rubyforge_project:
|
96
|
+
rubygems_version: 2.0.0
|
97
|
+
signing_key:
|
98
|
+
specification_version: 4
|
99
|
+
summary: Simple Bitmask
|
100
|
+
test_files:
|
101
|
+
- spec/bitsy_spec.rb
|
102
|
+
- spec/mask_spec.rb
|
103
|
+
- spec/spec_helper.rb
|