bitfieldable 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: bf3a1d7ed90f38d1356a6249e6746de19ee8192b18b6845abe0576ea78bcf589
4
+ data.tar.gz: b864d734fad1eab3d42bd8e1e168c919fba3818d7772039d6e095b017ffa681d
5
+ SHA512:
6
+ metadata.gz: 0ddac27962ae67733bb2d670b5523a1022623f4886e5f844e21447ac8ae35aa1f13bb9e5de5e11196973ab194ad32ed1e17a24ec251de46654df3499467946ba
7
+ data.tar.gz: f0526cd7a9cce57cc97afa5e7b556becef8263945623e78fdb80a2736343c55a13af4426267d4404c171b2f36863f447ee93630990c960de413f56a4fa0c554f
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
@@ -0,0 +1,52 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.7.1
3
+ NewCops: enable
4
+
5
+ Metrics/ClassLength:
6
+ Max: 1500
7
+
8
+ Metrics/AbcSize:
9
+ Enabled: false
10
+
11
+ Metrics/CyclomaticComplexity:
12
+ Enabled: false
13
+
14
+ Style/AsciiComments:
15
+ Enabled: false
16
+
17
+ Style/Documentation:
18
+ Enabled: false
19
+
20
+ Style/TrailingCommaInHashLiteral:
21
+ EnforcedStyleForMultiline: comma
22
+
23
+ Style/TrailingCommaInArguments:
24
+ EnforcedStyleForMultiline: comma
25
+
26
+ Style/TrailingCommaInArrayLiteral:
27
+ EnforcedStyleForMultiline: comma
28
+
29
+ Layout/DotPosition:
30
+ EnforcedStyle: leading
31
+
32
+ Layout/EmptyLineAfterGuardClause:
33
+ Enabled: false
34
+
35
+ Layout/EndOfLine:
36
+ EnforcedStyle: lf
37
+
38
+ Layout/MultilineOperationIndentation:
39
+ EnforcedStyle: indented
40
+
41
+ Layout/MultilineAssignmentLayout:
42
+ EnforcedStyle: same_line
43
+
44
+ Layout/CaseIndentation:
45
+ EnforcedStyle: end
46
+
47
+ Layout/EndAlignment:
48
+ EnforcedStyleAlignWith: variable
49
+
50
+ Metrics/BlockLength:
51
+ ExcludedMethods:
52
+ - class_methods
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in bitfieldable.gemspec
6
+ gemspec
7
+
8
+ gem 'rake', '~> 12.0'
@@ -0,0 +1,70 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ bitfieldable (0.1.0)
5
+ activesupport
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activesupport (6.0.3.2)
11
+ concurrent-ruby (~> 1.0, >= 1.0.2)
12
+ i18n (>= 0.7, < 2)
13
+ minitest (~> 5.1)
14
+ tzinfo (~> 1.1)
15
+ zeitwerk (~> 2.2, >= 2.2.2)
16
+ ast (2.4.1)
17
+ concurrent-ruby (1.1.6)
18
+ diff-lcs (1.4.3)
19
+ i18n (1.8.3)
20
+ concurrent-ruby (~> 1.0)
21
+ minitest (5.14.1)
22
+ parallel (1.19.2)
23
+ parser (2.7.1.4)
24
+ ast (~> 2.4.1)
25
+ rainbow (3.0.0)
26
+ rake (12.3.3)
27
+ regexp_parser (1.7.1)
28
+ rexml (3.2.4)
29
+ rspec (3.9.0)
30
+ rspec-core (~> 3.9.0)
31
+ rspec-expectations (~> 3.9.0)
32
+ rspec-mocks (~> 3.9.0)
33
+ rspec-core (3.9.2)
34
+ rspec-support (~> 3.9.3)
35
+ rspec-expectations (3.9.2)
36
+ diff-lcs (>= 1.2.0, < 2.0)
37
+ rspec-support (~> 3.9.0)
38
+ rspec-mocks (3.9.1)
39
+ diff-lcs (>= 1.2.0, < 2.0)
40
+ rspec-support (~> 3.9.0)
41
+ rspec-support (3.9.3)
42
+ rubocop (0.88.0)
43
+ parallel (~> 1.10)
44
+ parser (>= 2.7.1.1)
45
+ rainbow (>= 2.2.2, < 4.0)
46
+ regexp_parser (>= 1.7)
47
+ rexml
48
+ rubocop-ast (>= 0.1.0, < 1.0)
49
+ ruby-progressbar (~> 1.7)
50
+ unicode-display_width (>= 1.4.0, < 2.0)
51
+ rubocop-ast (0.2.0)
52
+ parser (>= 2.7.0.1)
53
+ ruby-progressbar (1.10.1)
54
+ thread_safe (0.3.6)
55
+ tzinfo (1.2.7)
56
+ thread_safe (~> 0.1)
57
+ unicode-display_width (1.7.0)
58
+ zeitwerk (2.4.0)
59
+
60
+ PLATFORMS
61
+ ruby
62
+
63
+ DEPENDENCIES
64
+ bitfieldable!
65
+ rake (~> 12.0)
66
+ rspec (~> 3.2)
67
+ rubocop
68
+
69
+ BUNDLED WITH
70
+ 2.1.4
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2020 Yasyf Mohamedali
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 all
13
+ 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 THE
21
+ SOFTWARE.
@@ -0,0 +1,57 @@
1
+ # Bitfieldable
2
+
3
+ The `Bitfieldable` gem allows you to define a set of boolean flag attributes that are backed by a single integer bitfield.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'bitfieldable'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install bitfieldable
20
+
21
+ ## Usage
22
+
23
+ For example, create a `User` model with an `int NOT NULL` field called `flags`, which defaults to 0.
24
+
25
+ ```ruby
26
+ class User
27
+ include Bitfieldable::Concern
28
+
29
+ bitfields flags: %i[admin special shadowbanned]
30
+ end
31
+
32
+ user = User.new
33
+
34
+ user.admin? # false
35
+ user.admin!
36
+ user.admin? # true
37
+ user.flags # 1
38
+
39
+ user.flip_special!
40
+ user.special? # true
41
+ user.flags # 3
42
+
43
+ user.shadowbanned = true
44
+ user.shadowbanned? # true
45
+ user.flags # 7
46
+ ```
47
+
48
+ ## Development
49
+
50
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
51
+
52
+ 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`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
53
+
54
+ ## Contributing
55
+
56
+ Bug reports and pull requests are welcome on GitHub at https://github.com/yasyf/bitfieldable.
57
+
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ task default: :spec
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # frozen_string_literal: true
4
+
5
+ require 'bundler/setup'
6
+ require 'bitfieldable'
7
+
8
+ # You can add fixtures and/or initialization code here to make experimenting
9
+ # with your gem easier. You can also use a different console, if you like.
10
+
11
+ # (If you use this, don't forget to add pry to your Gemfile!)
12
+ # require "pry"
13
+ # Pry.start
14
+
15
+ require 'irb'
16
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/bitfieldable/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'bitfieldable'
7
+ spec.version = Bitfieldable::VERSION
8
+ spec.authors = ['Yasyf Mohamedali']
9
+ spec.email = ['yasyfm@gmail.com']
10
+
11
+ spec.summary = 'Implement boolean flags as integer bitfields.'
12
+ spec.description = 'The `Bitfieldable` gem allows you to define a set of boolean flag attributes that are backed by a single integer bitfield.'
13
+ spec.homepage = 'https://github.com/yasyf/bitfieldable'
14
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.3.0')
15
+
16
+ spec.metadata['homepage_uri'] = spec.homepage
17
+ spec.metadata['source_code_uri'] = 'https://github.com/yasyf/bitfieldable'
18
+
19
+ # Specify which files should be added to the gem when it is released.
20
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
21
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
22
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
23
+ end
24
+ spec.bindir = 'exe'
25
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
26
+ spec.require_paths = ['lib']
27
+
28
+ spec.add_development_dependency 'rspec', '~> 3.2'
29
+ spec.add_development_dependency 'rubocop'
30
+
31
+ spec.add_dependency 'activesupport'
32
+ end
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bitfieldable/version'
4
+ require 'bitfieldable/concern'
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/core_ext'
4
+ require 'active_support/concern'
5
+
6
+ module Bitfieldable
7
+ module Concern
8
+ extend ActiveSupport::Concern
9
+
10
+ class_methods do
11
+ def parse_spec(spec)
12
+ spec.each_with_object({}) do |(attr, fields), hsh|
13
+ fields.each_with_index do |field, idx|
14
+ hsh[field] = {
15
+ attribute: attr,
16
+ index: idx,
17
+ }
18
+ end
19
+ end.to_h.with_indifferent_access
20
+ end
21
+
22
+ def bitfields(**spec)
23
+ return if spec.blank?
24
+ @bitfields = parse_spec spec
25
+ define_bitfield_methods!
26
+ end
27
+
28
+ def bitfield_spec
29
+ instance_variable_defined?(:@bitfields) ? @bitfields : {}
30
+ end
31
+
32
+ def bitfield_keys
33
+ bitfield_spec.keys.map(&:to_sym)
34
+ end
35
+
36
+ def define_bitfield_methods!
37
+ bitfield_spec.each do |field, spec|
38
+ define_method(field) { read_bitfield(spec) }
39
+ define_method("#{field}?") { read_bitfield(spec) }
40
+ define_method("#{field}!") { write_bitfield(spec, true) }
41
+ define_method("flip_#{field}!") { write_bitfield(spec, !read_bitfield(spec)) }
42
+ define_method("#{field}=") { |value| write_bitfield(spec, value) }
43
+ end
44
+ end
45
+ end
46
+
47
+ private
48
+
49
+ def read_bitfield(spec)
50
+ value = send(spec[:attribute]) || 0
51
+ value[spec[:index]].positive?
52
+ end
53
+
54
+ def write_bitfield(spec, new_value)
55
+ value = send(spec[:attribute]) || 0
56
+ if new_value
57
+ value |= 1 << spec[:index]
58
+ else
59
+ value &= ~(1 << spec[:index])
60
+ end
61
+ send("#{spec[:attribute]}=", value)
62
+ value.positive?
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Bitfieldable
4
+ VERSION = '0.1.0'
5
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bitfieldable
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Yasyf Mohamedali
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-07-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.2'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rubocop
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: activesupport
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: The `Bitfieldable` gem allows you to define a set of boolean flag attributes
56
+ that are backed by a single integer bitfield.
57
+ email:
58
+ - yasyfm@gmail.com
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - ".rubocop.yml"
65
+ - Gemfile
66
+ - Gemfile.lock
67
+ - LICENSE
68
+ - README.md
69
+ - Rakefile
70
+ - bin/console
71
+ - bin/setup
72
+ - bitfieldable.gemspec
73
+ - lib/bitfieldable.rb
74
+ - lib/bitfieldable/concern.rb
75
+ - lib/bitfieldable/version.rb
76
+ homepage: https://github.com/yasyf/bitfieldable
77
+ licenses: []
78
+ metadata:
79
+ homepage_uri: https://github.com/yasyf/bitfieldable
80
+ source_code_uri: https://github.com/yasyf/bitfieldable
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: 2.3.0
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ requirements: []
96
+ rubygems_version: 3.1.2
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: Implement boolean flags as integer bitfields.
100
+ test_files: []