active_record_bitmask 0.0.2 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c280de0aa76c1faf1a0eedaa01e79e1cd573409e0fe213e0ec2ec2a386fd6905
4
- data.tar.gz: a12217033318fbcc0de0fa377fb008d873efb4f715229df96d9f311f86be3d9b
3
+ metadata.gz: 92ef09e4ac0ca5c2da0db1869dc52776f5d990292e9c921bf02e364f1535925c
4
+ data.tar.gz: 5e48672686d4d15b10f547a2fccd0c017799dd259bf36f2d1184da9261bce2c8
5
5
  SHA512:
6
- metadata.gz: 4e2b1d0f778ef4cfb1ebd4ee438b5ea3777b86dcea8f25bfd686a1ce06439d5a62778376f624dd4cd036f6aa2fc083226458b0632867a2e5d661c0bf68afb266
7
- data.tar.gz: 39be7a07fca3f6ab7b4e3e3c806e4f63aba832451ddb2acfd9ef0710a6951499365ae0e9b6f72322d7e65cf770797646fa36de01effa6657abd521cf4dc6a60f
6
+ metadata.gz: 99696c27cf5fdcfede8e64be3791da418276f6dbd7b1edcb960b5dec585aa14587e82bf686e635d60cbccd0fbc4ab0c88e39150a24504c98e974b07048a55825
7
+ data.tar.gz: d3dfbe52f15833aada4cbe70559f93c3493511872a1af6f024c1f54d77c8697dab77f396debed19be074014bbe3e077442cfadb2e0a2da414a0126eecf89c07c
data/.gitignore CHANGED
@@ -7,3 +7,5 @@
7
7
  /pkg/
8
8
  /spec/reports/
9
9
  /tmp/
10
+ /gemfiles/*.lock
11
+ /gemfiles/.bundle
data/.rubocop.yml CHANGED
@@ -1,6 +1,7 @@
1
1
  AllCops:
2
2
  Exclude:
3
3
  - '.bundle/**/*'
4
+ - 'gemfiles/**/*'
4
5
  - Gemfile
5
6
  TargetRubyVersion: 2.6
6
7
  DisplayCopNames: true
data/.travis.yml CHANGED
@@ -1,5 +1,31 @@
1
1
  sudo: false
2
2
  language: ruby
3
3
  rvm:
4
- - 2.4.0
5
- before_install: gem install bundler -v 1.13.7
4
+ - 2.5.8
5
+ - 2.6.6
6
+ - 2.7.2
7
+ - 3.0.0
8
+ before_install: gem install bundler
9
+ gemfile:
10
+ - gemfiles/5.0_stable.gemfile
11
+ - gemfiles/5.1_stable.gemfile
12
+ - gemfiles/5.2_stable.gemfile
13
+ - gemfiles/6.0_stable.gemfile
14
+ - gemfiles/6.1_stable.gemfile
15
+ matrix:
16
+ exclude:
17
+ - rvm: 2.5.8
18
+ gemfile: gemfiles/6.1_stable.gemfile
19
+ - rvm: 2.6.6
20
+ gemfile: gemfiles/6.1_stable.gemfile
21
+ - rvm: 2.7.2
22
+ gemfile: gemfiles/6.1_stable.gemfile
23
+ - rvm: 3.0.0
24
+ gemfile: gemfiles/5.0_stable.gemfile
25
+ - rvm: 3.0.0
26
+ gemfile: gemfiles/5.1_stable.gemfile
27
+ - rvm: 3.0.0
28
+ gemfile: gemfiles/5.2_stable.gemfile
29
+ script:
30
+ - bundle exec rspec
31
+ - bundle exec rubocop
data/Appraisals ADDED
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ appraise '5.0-stable' do
4
+ gem 'activerecord', '~> 5.0.0'
5
+ gem 'sqlite3', '~> 1.3.6'
6
+ end
7
+
8
+ appraise '5.1-stable' do
9
+ gem 'activerecord', '~> 5.1.0'
10
+ end
11
+
12
+ appraise '5.2-stable' do
13
+ gem 'activerecord', '~> 5.2.0'
14
+ end
15
+
16
+ appraise '6.0-stable' do
17
+ gem 'activerecord', '~> 6.0.0'
18
+ end
19
+
20
+ appraise '6.1-stable' do
21
+ gem 'activerecord', '~> 6.1.0.rc1'
22
+ end
23
+
24
+ appraise '7.0-stable' do
25
+ gem 'activerecord', '7.0.0.alpha2'
26
+ end
data/CHANGELOG.md ADDED
@@ -0,0 +1,30 @@
1
+ ## 0.0.6
2
+
3
+ * Deprecate `#attribute?(*args)` in favor of `#attribute_bitmask?(*args)`. #17
4
+ * Support Rails 7.0.0.alpha1 #18
5
+
6
+ ## 0.0.5
7
+
8
+ * Support Rails 6.1.0 #14
9
+
10
+ ## 0.0.4
11
+
12
+ * Fixes security alert #9
13
+
14
+ * Query methods supports string arguments #12
15
+
16
+ ## 0.0.3
17
+
18
+ * Enumerize ActiveRecordBitmask::Map #4
19
+
20
+ * Support old rails #5
21
+
22
+ * Run RuboCop on CI. #6
23
+
24
+ ## 0.0.2
25
+
26
+ * Support blank values for checkbox #3
27
+
28
+ ## 0.0.1
29
+
30
+ * first release.
data/Gemfile CHANGED
@@ -3,8 +3,9 @@ source 'https://rubygems.org'
3
3
  gemspec
4
4
 
5
5
  gem 'pry'
6
- gem 'rubocop'
7
-
8
- group :test do
9
- gem 'sqlite3'
10
- end
6
+ # Don't update rubocop automatically because sometimes it fails test by breaking changes.
7
+ gem 'rubocop', '0.86.0'
8
+ gem 'appraisal'
9
+ gem 'rake'
10
+ gem 'rspec'
11
+ gem 'sqlite3'
data/README.md CHANGED
@@ -24,6 +24,8 @@ Simply declare an existing integer column as a bitmask.
24
24
 
25
25
  ```ruby
26
26
  class Post < ApplicationRecord
27
+ # bitmask attribute is integer column.
28
+ # `t.integer :roles, null: false, default: 0`
27
29
  bitmask(roles: [:administrator, :provider, :guest])
28
30
  end
29
31
  ```
@@ -41,8 +43,8 @@ You can check bitmask
41
43
 
42
44
  ```ruby
43
45
  post = Post.create(roles: [:provider, :guest])
44
- post.roles?(:provider) #=> false
45
- post.roles?(:guest, :provider) #=> true
46
+ post.roles_bitmask?(:provider) #=> false
47
+ post.roles_bitmask?(:guest, :provider) #=> true
46
48
  ```
47
49
 
48
50
  You can get the definition of bitmask
@@ -55,15 +57,45 @@ map.values #=> [1, 2, 4]
55
57
 
56
58
  ### Scopes
57
59
 
58
- #### `with_roles`
60
+ Named scopes are defined when you call `.bitmask(attribute)`.
59
61
 
60
- #### `with_any_roles`
62
+ #### `.with_attribute`
61
63
 
62
- #### `without_roles`
64
+ ```ruby
65
+ # all users with roles
66
+ User.with_roles
67
+
68
+ # all administrators
69
+ User.with_roles(:administrator)
70
+
71
+ # all users who are both administrator and partner
72
+ User.with_roles(:administrator, :partner)
73
+ ```
74
+
75
+ #### `.with_any_attribute`
76
+
77
+ ```ruby
78
+ # all users who are administrator or partner
79
+ User.with_any_roles(:administrator, :partner)
80
+ ```
63
81
 
64
- #### `with_exact_roles`
82
+ #### `.without_attribute`, `.no_attribute`
65
83
 
66
- #### `no_roles`
84
+ ```ruby
85
+ # all users without role
86
+ User.without_roles
87
+ User.no_roles
88
+
89
+ # all users who are not administrator
90
+ User.without_roles(:administrator)
91
+ ```
92
+
93
+ #### `.with_exact_attribute`
94
+
95
+ ```ruby
96
+ # all users who are both administrator and partner and nothing else
97
+ User.with_exact_roles(:administrator, :partner)
98
+ ```
67
99
 
68
100
  ## Development
69
101
 
@@ -14,13 +14,17 @@ Gem::Specification.new do |spec|
14
14
  spec.summary = 'Simple bitmask attribute support for ActiveRecord'
15
15
  spec.homepage = 'https://github.com/alpaca-tc/active_record_bitmask'
16
16
 
17
- spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ spec.metadata = {
18
+ 'homepage_uri' => 'https://github.com/alpaca-tc/active_record_bitmask',
19
+ 'changelog_uri' => 'https://github.com/alpaca-tc/active_record_bitmask/blob/master/CHANGELOG.md',
20
+ 'source_code_uri' => 'https://github.com/alpaca-tc/active_record_bitmask/',
21
+ 'bug_tracker_uri' => 'https://github.com/alpaca-tc/active_record_bitmask/issues'
22
+ }
23
+
24
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
25
  f.match(%r{^(test|spec|features)/})
19
26
  end
20
27
  spec.require_paths = ['lib']
21
28
 
22
29
  spec.add_dependency 'activerecord', '>= 5.0'
23
- spec.add_development_dependency 'bundler'
24
- spec.add_development_dependency 'rake'
25
- spec.add_development_dependency 'rspec'
26
30
  end
@@ -0,0 +1,13 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "pry"
6
+ gem "rubocop", "0.86.0"
7
+ gem "appraisal"
8
+ gem "rake"
9
+ gem "rspec"
10
+ gem "sqlite3", "~> 1.3.6"
11
+ gem "activerecord", "~> 5.0.0"
12
+
13
+ gemspec path: "../"
@@ -0,0 +1,13 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "pry"
6
+ gem "rubocop", "0.86.0"
7
+ gem "appraisal"
8
+ gem "rake"
9
+ gem "rspec"
10
+ gem "sqlite3"
11
+ gem "activerecord", "~> 5.1.0"
12
+
13
+ gemspec path: "../"
@@ -0,0 +1,13 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "pry"
6
+ gem "rubocop", "0.86.0"
7
+ gem "appraisal"
8
+ gem "rake"
9
+ gem "rspec"
10
+ gem "sqlite3"
11
+ gem "activerecord", "~> 5.2.0"
12
+
13
+ gemspec path: "../"
@@ -0,0 +1,13 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "pry"
6
+ gem "rubocop", "0.86.0"
7
+ gem "appraisal"
8
+ gem "rake"
9
+ gem "rspec"
10
+ gem "sqlite3"
11
+ gem "activerecord", "~> 6.0.0"
12
+
13
+ gemspec path: "../"
@@ -0,0 +1,13 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "pry"
6
+ gem "rubocop", "0.86.0"
7
+ gem "appraisal"
8
+ gem "rake"
9
+ gem "rspec"
10
+ gem "sqlite3"
11
+ gem "activerecord", "~> 6.1.0.rc1"
12
+
13
+ gemspec path: "../"
@@ -0,0 +1,13 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "pry"
6
+ gem "rubocop", "0.86.0"
7
+ gem "appraisal"
8
+ gem "rake"
9
+ gem "rspec"
10
+ gem "sqlite3"
11
+ gem "activerecord", "7.0.0.alpha2"
12
+
13
+ gemspec path: "../"
@@ -1,24 +1,45 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'active_record'
4
+ require 'active_support/concern'
5
+
3
6
  module ActiveRecordBitmask
4
7
  module AttributeMethods
5
8
  module Query
9
+ extend ActiveSupport::Concern
10
+
11
+ included do
12
+ attribute_method_suffix('_bitmask?')
13
+ end
14
+
6
15
  private
7
16
 
8
17
  def bitmask_attribute?(attribute_name)
9
18
  self.class.bitmasks.key?(attribute_name.to_sym)
10
19
  end
11
20
 
12
- def attribute?(attribute_name, *values)
21
+ if ActiveRecord.gem_version < Gem::Version.create('7.0.0.alpha1')
22
+ # In Rails 7.0.0, calling attribute? with arguments is not working.
23
+ def attribute?(attribute_name, *values)
24
+ if bitmask_attribute?(attribute_name) && values.present?
25
+ ActiveSupport::Deprecation.warn("`#{attribute_name}?(*args)` is deprecated and will be removed in next major version. Call `#{attribute_name}_bitmask?(*args)` instead.")
26
+ attribute_bitmask?(attribute_name, *values)
27
+ else
28
+ super
29
+ end
30
+ end
31
+ end
32
+
33
+ def attribute_bitmask?(attribute_name, *values)
13
34
  if bitmask_attribute?(attribute_name) && values.present?
14
35
  # assert bitmask values
15
36
  map = self.class.bitmask_for(attribute_name)
16
- map.attributes_to_bitmask(values)
37
+ expected_value = map.attributes_to_bitmask(values)
38
+ current_value = map.attributes_to_bitmask(attribute(attribute_name))
17
39
 
18
- current_value = attribute(attribute_name)
19
- values.all? { |value| current_value.include?(value) }
40
+ (current_value & expected_value) == expected_value
20
41
  else
21
- super
42
+ attribute?(attribute_name)
22
43
  end
23
44
  end
24
45
  end
@@ -1,7 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'active_model'
4
-
5
3
  module ActiveRecordBitmask
6
4
  class BitmaskType < ActiveModel::Type::Value
7
5
  # @param name [String]
@@ -1,7 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'active_support/core_ext/module/delegation'
4
+
3
5
  module ActiveRecordBitmask
4
6
  class Map
7
+ include Enumerable
8
+
9
+ delegate(:each, :keys, :values, to: :mapping)
10
+
5
11
  # Default blank values for checkbox
6
12
  BLANK_VALUES = [:'', :'0'].freeze
7
13
 
@@ -64,16 +70,6 @@ module ActiveRecordBitmask
64
70
  end
65
71
  end
66
72
 
67
- # @return [Array<Symbol>]
68
- def keys
69
- mapping.keys
70
- end
71
-
72
- # @return [Array<Integer>]
73
- def values
74
- mapping.values
75
- end
76
-
77
73
  private
78
74
 
79
75
  def reject_blank_attributes(attributes)
@@ -9,7 +9,10 @@ module ActiveRecordBitmask
9
9
 
10
10
  included do
11
11
  include ActiveRecordBitmask::AttributeMethods::Query
12
- class_attribute(:bitmasks, instance_accessor: false, default: {})
12
+ class_attribute(:bitmasks, instance_accessor: false)
13
+
14
+ # `class_attribute` on Rails 5.0 doesn't support default option.
15
+ self.bitmasks = {}
13
16
  end
14
17
 
15
18
  class_methods do
@@ -48,11 +51,25 @@ module ActiveRecordBitmask
48
51
  private
49
52
 
50
53
  def define_bitmask_attribute(attribute, map)
51
- decorate_attribute_type(attribute, :bitmask) do |subtype|
52
- ActiveRecordBitmask::BitmaskType.new(attribute, map, subtype)
54
+ if ActiveRecord.gem_version >= Gem::Version.new('7.0.0.alpha1')
55
+ # Greater than or equal to 7.0.0
56
+ attribute(attribute) do |subtype|
57
+ ActiveRecordBitmask::BitmaskType.new(attribute, map, subtype)
58
+ end
59
+ elsif ActiveRecord.gem_version >= Gem::Version.new('6.1.0')
60
+ # Equal to 6.1.0
61
+ decorate_attribute_type(attribute) do |subtype|
62
+ ActiveRecordBitmask::BitmaskType.new(attribute, map, subtype)
63
+ end
64
+ else
65
+ # Less than 6.1.0
66
+ decorate_attribute_type(attribute, :bitmask) do |subtype|
67
+ ActiveRecordBitmask::BitmaskType.new(attribute, map, subtype)
68
+ end
53
69
  end
54
70
  end
55
71
 
72
+ # rubocop:disable Metrics/CyclomaticComplexity
56
73
  def define_bitmask_scopes(attribute)
57
74
  blank = [0, nil].freeze
58
75
 
@@ -109,6 +126,7 @@ module ActiveRecordBitmask
109
126
  where(attribute => blank)
110
127
  }
111
128
  end
129
+ # rubocop:enable Metrics/CyclomaticComplexity
112
130
  end
113
131
  end
114
132
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActiveRecordBitmask
4
- VERSION = '0.0.2'
4
+ VERSION = '0.0.6'
5
5
  end
@@ -5,11 +5,11 @@ require 'active_support/lazy_load_hooks'
5
5
  module ActiveRecordBitmask
6
6
  require 'active_record_bitmask/version'
7
7
  require 'active_record_bitmask/attribute_methods'
8
- require 'active_record_bitmask/bitmask_type'
9
8
  require 'active_record_bitmask/map'
10
9
  end
11
10
 
12
11
  ActiveSupport.on_load :active_record do
13
12
  require 'active_record_bitmask/model'
13
+ require 'active_record_bitmask/bitmask_type'
14
14
  include(ActiveRecordBitmask::Model)
15
15
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_record_bitmask
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - alpaca-tc
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-01-22 00:00:00.000000000 Z
11
+ date: 2021-09-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -24,48 +24,6 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '5.0'
27
- - !ruby/object:Gem::Dependency
28
- name: bundler
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: rake
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: rspec
57
- requirement: !ruby/object:Gem::Requirement
58
- requirements:
59
- - - ">="
60
- - !ruby/object:Gem::Version
61
- version: '0'
62
- type: :development
63
- prerelease: false
64
- version_requirements: !ruby/object:Gem::Requirement
65
- requirements:
66
- - - ">="
67
- - !ruby/object:Gem::Version
68
- version: '0'
69
27
  description:
70
28
  email:
71
29
  - alpaca-tc@alpaca.tc
@@ -77,10 +35,18 @@ files:
77
35
  - ".rspec"
78
36
  - ".rubocop.yml"
79
37
  - ".travis.yml"
38
+ - Appraisals
39
+ - CHANGELOG.md
80
40
  - Gemfile
81
41
  - README.md
82
42
  - Rakefile
83
43
  - active_record_bitmask.gemspec
44
+ - gemfiles/5.0_stable.gemfile
45
+ - gemfiles/5.1_stable.gemfile
46
+ - gemfiles/5.2_stable.gemfile
47
+ - gemfiles/6.0_stable.gemfile
48
+ - gemfiles/6.1_stable.gemfile
49
+ - gemfiles/7.0_stable.gemfile
84
50
  - lib/active_record_bitmask.rb
85
51
  - lib/active_record_bitmask/attribute_methods.rb
86
52
  - lib/active_record_bitmask/attribute_methods/query.rb
@@ -91,7 +57,11 @@ files:
91
57
  homepage: https://github.com/alpaca-tc/active_record_bitmask
92
58
  licenses:
93
59
  - MIT
94
- metadata: {}
60
+ metadata:
61
+ homepage_uri: https://github.com/alpaca-tc/active_record_bitmask
62
+ changelog_uri: https://github.com/alpaca-tc/active_record_bitmask/blob/master/CHANGELOG.md
63
+ source_code_uri: https://github.com/alpaca-tc/active_record_bitmask/
64
+ bug_tracker_uri: https://github.com/alpaca-tc/active_record_bitmask/issues
95
65
  post_install_message:
96
66
  rdoc_options: []
97
67
  require_paths:
@@ -107,7 +77,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
107
77
  - !ruby/object:Gem::Version
108
78
  version: '0'
109
79
  requirements: []
110
- rubygems_version: 3.0.3
80
+ rubygems_version: 3.1.6
111
81
  signing_key:
112
82
  specification_version: 4
113
83
  summary: Simple bitmask attribute support for ActiveRecord