bitwise_attribute 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: 02de993668b47710ed8674dd89e0f31df73b5fe10a0f54791cfede73927f6801
4
+ data.tar.gz: bcc2a3e1916287a32029746e9878a3951adb6ca08a28e76d2eefb2cb713b1080
5
+ SHA512:
6
+ metadata.gz: 35a58ec03084936782bf04ac7e634783d427861ce7a82fb7088ede9f162e8fd7b7d2acb387387454271e194e821709d6e1c6047a630556c2b6eb31fb44d5ee51
7
+ data.tar.gz: 5b9a4e19f46ec7082baa56f0a6c48e9b10fd8b82d31bca08f3672463d53b760fc71a4f68d18e37dfd4abab29f397cd37444f8b3b5cce76eff6064228d03bf3ad
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,4 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
4
+ --order random
@@ -0,0 +1,18 @@
1
+ Metrics/AbcSize:
2
+ Max: 20
3
+
4
+ Style/Documentation:
5
+ Exclude:
6
+ - 'spec/**/*'
7
+ - 'test/**/*'
8
+ - 'lib/bitwise_attribute.rb'
9
+ - 'lib/bitwise_attribute/values_list.rb'
10
+
11
+ Metrics/LineLength:
12
+ Max: 100
13
+
14
+ Metrics/MethodLength:
15
+ Max: 20
16
+
17
+ Metrics/BlockLength:
18
+ Max: 45
@@ -0,0 +1 @@
1
+ 2.5.0
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.5.0
5
+ before_install: gem install bundler -v 1.16.1
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in bitwise_attribute.gemspec
6
+ gemspec
@@ -0,0 +1,82 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ bitwise_attribute (0.1.0)
5
+ activesupport (~> 5.1.5)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activesupport (5.1.5)
11
+ concurrent-ruby (~> 1.0, >= 1.0.2)
12
+ i18n (~> 0.7)
13
+ minitest (~> 5.1)
14
+ tzinfo (~> 1.1)
15
+ ast (2.4.0)
16
+ byebug (10.0.1)
17
+ coderay (1.1.2)
18
+ concurrent-ruby (1.0.5)
19
+ diff-lcs (1.3)
20
+ docile (1.1.5)
21
+ i18n (0.9.5)
22
+ concurrent-ruby (~> 1.0)
23
+ json (2.1.0)
24
+ method_source (0.9.0)
25
+ minitest (5.11.3)
26
+ parallel (1.12.1)
27
+ parser (2.5.0.5)
28
+ ast (~> 2.4.0)
29
+ powerpack (0.1.1)
30
+ pry (0.11.3)
31
+ coderay (~> 1.1.0)
32
+ method_source (~> 0.9.0)
33
+ pry-byebug (3.6.0)
34
+ byebug (~> 10.0)
35
+ pry (~> 0.10)
36
+ rainbow (3.0.0)
37
+ rake (10.5.0)
38
+ rspec (3.7.0)
39
+ rspec-core (~> 3.7.0)
40
+ rspec-expectations (~> 3.7.0)
41
+ rspec-mocks (~> 3.7.0)
42
+ rspec-core (3.7.1)
43
+ rspec-support (~> 3.7.0)
44
+ rspec-expectations (3.7.0)
45
+ diff-lcs (>= 1.2.0, < 2.0)
46
+ rspec-support (~> 3.7.0)
47
+ rspec-mocks (3.7.0)
48
+ diff-lcs (>= 1.2.0, < 2.0)
49
+ rspec-support (~> 3.7.0)
50
+ rspec-support (3.7.1)
51
+ rubocop (0.54.0)
52
+ parallel (~> 1.10)
53
+ parser (>= 2.5)
54
+ powerpack (~> 0.1)
55
+ rainbow (>= 2.2.2, < 4.0)
56
+ ruby-progressbar (~> 1.7)
57
+ unicode-display_width (~> 1.0, >= 1.0.1)
58
+ ruby-progressbar (1.9.0)
59
+ simplecov (0.15.1)
60
+ docile (~> 1.1.0)
61
+ json (>= 1.8, < 3)
62
+ simplecov-html (~> 0.10.0)
63
+ simplecov-html (0.10.2)
64
+ thread_safe (0.3.6)
65
+ tzinfo (1.2.5)
66
+ thread_safe (~> 0.1)
67
+ unicode-display_width (1.3.0)
68
+
69
+ PLATFORMS
70
+ ruby
71
+
72
+ DEPENDENCIES
73
+ bitwise_attribute!
74
+ bundler (~> 1.16)
75
+ pry-byebug
76
+ rake (~> 10.0)
77
+ rspec (~> 3.0)
78
+ rubocop (~> 0.54.0)
79
+ simplecov (~> 0.15.1)
80
+
81
+ BUNDLED WITH
82
+ 1.16.1
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Ricardo Otero
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.
@@ -0,0 +1,72 @@
1
+ [![Dependency Status](https://gemnasium.com/rikas/bitwise_attribute.svg)](https://gemnasium.com/rikas/bitwise_attribute)
2
+ [![Build Status](https://travis-ci.org/rikas/bitwise_attribute.svg?branch=master)](https://travis-ci.org/rikas/bitwise_attribute)
3
+
4
+ # BitwiseAttribute
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'bitwise_attribute'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install bitwise_attribute
21
+
22
+ ## Usage
23
+
24
+ Include `BitwiseAttribute` and then define your attribute with `attr_bitwise`. By default it will
25
+ use the singularized name with `_mask`.
26
+
27
+ For the `roles` attribute you need to have `role_mask` column in your model, so add the migration:
28
+
29
+ ```ruby
30
+ class AddRoleMaskToUsers < ActiveRecord::Migration
31
+ def change
32
+ add_column :users, :role_mask, :integer, default: 0
33
+ end
34
+ end
35
+ ```
36
+
37
+ ```ruby
38
+ class User < ActiveRecord::Base
39
+ include BitwiseAttribute
40
+
41
+ attr_bitwise :roles, values: %i[user moderator admin]
42
+ end
43
+ ```
44
+
45
+ You can then use
46
+
47
+ ```ruby
48
+ user = User.first
49
+ user.roles #=> []
50
+ user.roles = [:user, :admin] #=> [:user, :admin]
51
+ user.save
52
+ user.role_mask #=> 5
53
+ ```
54
+
55
+ ## Development
56
+
57
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run
58
+ the tests. You can also run `bin/console` for an interactive prompt that will allow you to
59
+ experiment.
60
+
61
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new
62
+ version, update the version number in `version.rb`, and then run `bundle exec rake release`, which
63
+ will create a git tag for the version, push git commits and tags, and push the `.gem` file
64
+ to [rubygems.org](https://rubygems.org).
65
+
66
+ ## Contributing
67
+
68
+ Bug reports and pull requests are welcome on GitHub at https://github.com/rikas/bitwise_attribute.
69
+
70
+ ## License
71
+
72
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'bitwise_attribute'
5
+
6
+ require 'pry'
7
+ Pry.start
@@ -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,33 @@
1
+ lib = File.expand_path('lib', __dir__)
2
+
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ require 'bitwise_attribute/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'bitwise_attribute'
9
+ spec.version = BitwiseAttribute::VERSION
10
+ spec.authors = ['Ricardo Otero']
11
+ spec.email = ['oterosantos@gmail.com']
12
+
13
+ spec.summary = 'Bitwise attribute for ruby class and Rails model.'
14
+ spec.description = 'Bitwise attribute for ruby class and Rails model.'
15
+ spec.homepage = 'https://github.com/rikas/bitwise_attribute'
16
+ spec.license = 'MIT'
17
+
18
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
19
+ f.match(%r{^(test|spec|features)/})
20
+ end
21
+ spec.bindir = 'exe'
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = %w[lib]
24
+
25
+ spec.add_dependency 'activesupport', '~> 5.1.5'
26
+
27
+ spec.add_development_dependency 'bundler', '~> 1.16'
28
+ spec.add_development_dependency 'pry-byebug'
29
+ spec.add_development_dependency 'rake', '~> 10.0'
30
+ spec.add_development_dependency 'rspec', '~> 3.0'
31
+ spec.add_development_dependency 'rubocop', '~> 0.54.0'
32
+ spec.add_development_dependency 'simplecov', '~> 0.15.1'
33
+ end
@@ -0,0 +1,101 @@
1
+ require 'active_support'
2
+ require 'active_support/concern'
3
+ require 'active_support/core_ext'
4
+ require 'bitwise_attribute/version'
5
+ require 'bitwise_attribute/values_list'
6
+
7
+ module BitwiseAttribute
8
+ extend ActiveSupport::Concern
9
+
10
+ class_methods do
11
+ def attr_bitwise(name, column_name: nil, values:)
12
+ column_name ||= "#{name.to_s.singularize}_mask"
13
+
14
+ # Check if the values is an array or hash with valid values. Raise ArgumentError otherwise.
15
+ validate_values!(values)
16
+
17
+ # Sets the constant <name.upcase>_MAPPING
18
+ mapping = build_mapping(name, values)
19
+
20
+ @mapping ||= {}
21
+ @mapping[name] = mapping
22
+
23
+ define_singleton_method(name) do
24
+ @mapping[name]
25
+ end
26
+
27
+ define_method(name) do
28
+ roles = value_getter(column_name, mapping)
29
+
30
+ # This special array will call self.<name>= after changed
31
+ ValuesList.new(name, self, roles)
32
+ end
33
+
34
+ define_method("#{name}=") do |new_values|
35
+ value_setter(column_name, Array(new_values), mapping)
36
+ end
37
+
38
+ # Adds a boolean method for each key (ex: admin?)
39
+ mapping.keys.each do |key|
40
+ define_method("#{key}?") do
41
+ value?(column_name, mapping[key])
42
+ end
43
+ end
44
+ end
45
+
46
+ # Builds internal bitwise key-value mapping it add a zero value, needed for bits operations.
47
+ # Each sym get a power of 2 value
48
+ def build_mapping(name, values)
49
+ {}.tap do |hash|
50
+ if values.is_a?(Hash)
51
+ hash.merge!(values.sort_by { |_, value| value }.to_h)
52
+ else
53
+ values.each_with_index { |key, index| hash[key] = 2**index }
54
+ end
55
+
56
+ hash[:empty] = 0 # the default one
57
+ end
58
+ end
59
+
60
+ # Validates the numeric values for each key. If it's not a power of 2 then raise ArgumentError.
61
+ def validate_values!(values)
62
+ return true if values.is_a?(Array)
63
+
64
+ values.reject { |_, value| (Math.log2(value) % 1.0).zero? }.tap do |invalid_options|
65
+ if invalid_options.any?
66
+ raise(ArgumentError, "Values should be a power of 2 (#{invalid_options})")
67
+ end
68
+ end
69
+ end
70
+ end
71
+
72
+ private
73
+
74
+ # Return current value to symbols array
75
+ # Ex: 16 => [:slots, :credits]
76
+ def value_getter(mask_column, mapping)
77
+ mapping.values.reject { |value| (send(mask_column) & value).zero? }.map do |value|
78
+ mapping.invert[value]
79
+ end
80
+ end
81
+
82
+ # Set current values from values array
83
+ def value_setter(mask_column, values, mapping)
84
+ send("#{mask_column}=", 0)
85
+
86
+ values.each do |value|
87
+ raise(ArgumentError, "Unknown value #{value}!") unless mapping[value]
88
+
89
+ add_value(mask_column, mapping[value])
90
+ end
91
+ end
92
+
93
+ # Return if value presents in mask (raw value)
94
+ def value?(mask_column, val)
95
+ send(mask_column) & val != 0
96
+ end
97
+
98
+ def add_value(mask_column, val)
99
+ send("#{mask_column}=", send(mask_column) | val)
100
+ end
101
+ end
@@ -0,0 +1,16 @@
1
+ module BitwiseAttribute
2
+ class ValuesList < Array
3
+ def initialize(field, record, values)
4
+ @field = field
5
+ @record = record
6
+
7
+ concat(values)
8
+ end
9
+
10
+ def <<(value)
11
+ concat(Array(value))
12
+
13
+ @record.send("#{@field}=", self)
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,3 @@
1
+ module BitwiseAttribute
2
+ VERSION = '0.1.0'.freeze
3
+ end
metadata ADDED
@@ -0,0 +1,158 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bitwise_attribute
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ricardo Otero
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-03-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 5.1.5
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 5.1.5
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.16'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.16'
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry-byebug
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
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubocop
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 0.54.0
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 0.54.0
97
+ - !ruby/object:Gem::Dependency
98
+ name: simplecov
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: 0.15.1
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: 0.15.1
111
+ description: Bitwise attribute for ruby class and Rails model.
112
+ email:
113
+ - oterosantos@gmail.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - ".rspec"
120
+ - ".rubocop.yml"
121
+ - ".ruby-version"
122
+ - ".travis.yml"
123
+ - Gemfile
124
+ - Gemfile.lock
125
+ - LICENSE.txt
126
+ - README.md
127
+ - Rakefile
128
+ - bin/console
129
+ - bin/setup
130
+ - bitwise_attribute.gemspec
131
+ - lib/bitwise_attribute.rb
132
+ - lib/bitwise_attribute/values_list.rb
133
+ - lib/bitwise_attribute/version.rb
134
+ homepage: https://github.com/rikas/bitwise_attribute
135
+ licenses:
136
+ - MIT
137
+ metadata: {}
138
+ post_install_message:
139
+ rdoc_options: []
140
+ require_paths:
141
+ - lib
142
+ required_ruby_version: !ruby/object:Gem::Requirement
143
+ requirements:
144
+ - - ">="
145
+ - !ruby/object:Gem::Version
146
+ version: '0'
147
+ required_rubygems_version: !ruby/object:Gem::Requirement
148
+ requirements:
149
+ - - ">="
150
+ - !ruby/object:Gem::Version
151
+ version: '0'
152
+ requirements: []
153
+ rubyforge_project:
154
+ rubygems_version: 2.7.3
155
+ signing_key:
156
+ specification_version: 4
157
+ summary: Bitwise attribute for ruby class and Rails model.
158
+ test_files: []