banditmask 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b0ba2ce5f3058aca2b5b0aea9b05215a6b76026f
4
+ data.tar.gz: 5b41de6797071fc9e82a73da1bd341855a5e5594
5
+ SHA512:
6
+ metadata.gz: 6fbe95e064c17ddf564ef77d757df53c5fab34f648974d412e29e2e8fddf42abc945e17bc74f053419d7d04529b662c173113e35ae64329810c71e4e9c658829
7
+ data.tar.gz: 60c69509a022d36d2a1d33fa72e94ea41e503ea37b5198a126cf9f2be04ec8ff7dce4addcc33d2d1d2e576ba9672f1ad94cc394724278a0daf3c94725df4b5c4
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.6
4
+ - 2.2.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in banditmask.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 John Parker
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,77 @@
1
+ # BanditMask
2
+
3
+ BanditMask provides a generic wrapper for working with bitmasks.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'banditmask'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install banditmask
20
+
21
+ ## Usage
22
+
23
+ Create a class which inherits from BanditMask and declare the available bit
24
+ names and their corresponding values.
25
+
26
+ ```ruby
27
+ class ChmodMask < BanditMask
28
+ bit :read, 0b001
29
+ bit :write, 0b010
30
+ bit :execute, 0b100
31
+ end
32
+
33
+ ChmodMask.bits # => { :read => 1, :write => 2, :execute => 4 }
34
+ ```
35
+
36
+ To instantiate a new mask class:
37
+
38
+ ```ruby
39
+ mask = ChmodMask.new
40
+ # or
41
+ current_bitmask = 0b001 | 0b010
42
+ mask = ChmodMask.new current_bitmask
43
+ ```
44
+
45
+ Enable bits by name.
46
+
47
+ ```ruby
48
+ mask << :read << :write
49
+ ```
50
+
51
+ Ask whether specific bits are enabled.
52
+
53
+ ```ruby
54
+ mask.include? :read # => true
55
+ mask.include? :write # => true
56
+ mask.include? :execute # => false
57
+ ```
58
+
59
+ Retrieve a list of all currently enabled bits.
60
+
61
+ ```ruby
62
+ mask.names # => [:read, :write]
63
+ ```
64
+
65
+ ## Development
66
+
67
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
68
+
69
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
70
+
71
+ ## Contributing
72
+
73
+ 1. Fork it ( https://github.com/[my-github-username]/banditmask/fork )
74
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
75
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
76
+ 4. Push to the branch (`git push origin my-new-feature`)
77
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs << 'test'
6
+ t.pattern = 'test/**/test_*.rb'
7
+ end
8
+
9
+ task default: :test
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'banditmask/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "banditmask"
8
+ spec.version = BanditMask::VERSION
9
+ spec.authors = ["John Parker"]
10
+ spec.email = ["jparker@urgetopunt.com"]
11
+
12
+ spec.summary = %q{Generic implementation of a bitmask.}
13
+ spec.description = %q{BanditMask provides a generic wrapper class to manage a bitmask attribute.}
14
+ spec.homepage = "https://github.com/jparker/banditmask"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.required_ruby_version = '>= 2.1'
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.9"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency 'minitest', '~> 5.7'
27
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "banditmask"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,3 @@
1
+ class BanditMask
2
+ VERSION = "0.1.0"
3
+ end
data/lib/banditmask.rb ADDED
@@ -0,0 +1,103 @@
1
+ require "banditmask/version"
2
+
3
+ class BanditMask
4
+ attr_reader :mask
5
+
6
+ def initialize(mask = 0b0) # :nodoc:
7
+ @mask = mask
8
+ end
9
+
10
+ ##
11
+ # Returns a Hash mapping all defined names to their respective bits.
12
+ #
13
+ # class BanditMask
14
+ # bit :read, 0b01
15
+ # bit :write, 0b10
16
+ # end
17
+ #
18
+ # BanditMask.bits # => { :read => 1, :write => 2 }
19
+ def self.bits
20
+ @bits ||= {}
21
+ end
22
+
23
+ ##
24
+ # Maps +name+ to +value+ in the global list of defined bits.
25
+ #
26
+ # class BanditMask
27
+ # bit :read, 0b001
28
+ # bit :write, 0b010
29
+ # bit :execute, 0b100
30
+ # end
31
+ def self.bit(name, value)
32
+ bits.update name => value
33
+ end
34
+
35
+ ##
36
+ # Clears all defined bits.
37
+ def self.reset_bits!
38
+ @bits = {}
39
+ end
40
+
41
+ ##
42
+ # Returns integer value of current bitmask.
43
+ def to_i
44
+ mask
45
+ end
46
+
47
+ ##
48
+ # Returns an array of names of the currently enabled bits.
49
+ #
50
+ # class BanditMask
51
+ # bit :read, 0b01
52
+ # bit :write, 0b10
53
+ # end
54
+ #
55
+ # mask = BanditMask.new 0b01
56
+ # mask.names # => [:read]
57
+ def names
58
+ self.class.bits.select { |name, _| include? name }.keys
59
+ end
60
+ alias_method :to_a, :names
61
+
62
+ ##
63
+ # Enables the bit named +name+. Returns +self+, so calles to #<< can be
64
+ # chained (think Array#<<). Raises +ArgumentError+ if +name+ does not
65
+ # correspond to a bit that was previously declared with BanditMask.bit.
66
+ #
67
+ # class BanditMask
68
+ # bit :read, 0b01
69
+ # bit :write, 0b10
70
+ # end
71
+ #
72
+ # mask = BanditMask.new
73
+ # mask << :read << :write
74
+ def <<(name)
75
+ @mask |= name_to_bit(name)
76
+ self
77
+ end
78
+
79
+ ##
80
+ # Returns true if +name+ is among the currently enabled bits. Returns false
81
+ # otherwise. Raises +ArgumentError+ if +name+ does not correspond to a bit
82
+ # that was previously declared with BanditMask.bit.
83
+ #
84
+ # class BanditMask
85
+ # bit :read, 0b01
86
+ # bit :write, 0b10
87
+ # end
88
+ #
89
+ # mask = BanditMask.new 0b01
90
+ # mask.include? :read # => true
91
+ # mask.include? :write # => false
92
+ def include?(name)
93
+ @mask & name_to_bit(name) != 0
94
+ end
95
+
96
+ private
97
+
98
+ def name_to_bit(name)
99
+ self.class.bits.fetch(name) do
100
+ raise ArgumentError, "undefined bit: #{name.inspect}"
101
+ end
102
+ end
103
+ end
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: banditmask
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - John Parker
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-07-06 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.9'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.9'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.7'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.7'
55
+ description: BanditMask provides a generic wrapper class to manage a bitmask attribute.
56
+ email:
57
+ - jparker@urgetopunt.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".travis.yml"
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - banditmask.gemspec
69
+ - bin/console
70
+ - bin/setup
71
+ - lib/banditmask.rb
72
+ - lib/banditmask/version.rb
73
+ homepage: https://github.com/jparker/banditmask
74
+ licenses:
75
+ - MIT
76
+ metadata: {}
77
+ post_install_message:
78
+ rdoc_options: []
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: '2.1'
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ requirements: []
92
+ rubyforge_project:
93
+ rubygems_version: 2.4.8
94
+ signing_key:
95
+ specification_version: 4
96
+ summary: Generic implementation of a bitmask.
97
+ test_files: []