bit_settings 0.1.0 → 1.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/{LICENSE.txt → LICENSE} +0 -0
- data/README.md +17 -7
- data/Rakefile +9 -1
- data/bit_settings.gemspec +8 -4
- data/lib/bit_settings/boolean_value.rb +21 -0
- data/lib/bit_settings/version.rb +1 -1
- data/lib/bit_settings.rb +46 -17
- metadata +75 -21
- data/bin/console +0 -14
- data/bin/setup +0 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 22cd583daf1f3c6e6515394137361db2365632c24c62ac6a1527633a467badca
|
4
|
+
data.tar.gz: c076a8a41b3b4f9544e2f86dac2aea93fb5abdd6352d129c7b850c9e17c41a70
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a2233d4f0fc3fb9ed2a438a4f281f1c6742c125be8a0579ee21b35750456bfc5d5b550515d85710ad16edfc429331dc787531a9ef7dee67002201b6ccab79ea8
|
7
|
+
data.tar.gz: 257e99f707aecd320968bf52e60eb6295b85be6603bd1bbde0c8934d847e51b3975139a6eab186a6264f042c8fc49643a487ca711bf7dff2aab4d9a0dfb87209
|
data/{LICENSE.txt → LICENSE}
RENAMED
File without changes
|
data/README.md
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
# BitSettings
|
2
2
|
|
3
3
|
BitSettings is a plugin for ActiveRecord that transform a column of your model in a set of boolean settings.
|
4
|
+
You can have up to 32 boolean values stored in a single database column.
|
4
5
|
|
5
6
|
## Installation
|
6
7
|
|
@@ -22,7 +23,6 @@ Or install it yourself as:
|
|
22
23
|
|
23
24
|
First of all add a unsigned int column for your boolean settings with `rails g migration add_settings_to_users settings:integer` and then edit like follow:
|
24
25
|
|
25
|
-
|
26
26
|
```
|
27
27
|
class AddSettingsToUsers < ActiveRecord::Migration
|
28
28
|
def change
|
@@ -30,6 +30,7 @@ class AddSettingsToUsers < ActiveRecord::Migration
|
|
30
30
|
end
|
31
31
|
end
|
32
32
|
```
|
33
|
+
|
33
34
|
The max unsigned integer in 4 bytes is `2^32-1 = 4294967295` so with a column you can have max 32 settings.
|
34
35
|
If you want that the default value of your settings is 1 (true) set the default value of the column to `2^32-1`.
|
35
36
|
|
@@ -38,7 +39,7 @@ Then in your model:
|
|
38
39
|
```
|
39
40
|
class User extends ActiveRecord::Base
|
40
41
|
include BitSettings
|
41
|
-
add_settings
|
42
|
+
add_settings :disable_notifications, :help_tour_shown
|
42
43
|
end
|
43
44
|
```
|
44
45
|
|
@@ -51,18 +52,28 @@ user.disable_notifications = true # => true
|
|
51
52
|
user.save
|
52
53
|
```
|
53
54
|
|
54
|
-
|
55
|
+
You can also use the scope `with_settings` which takes a hash as parameter like the example below:
|
56
|
+
|
57
|
+
```
|
58
|
+
# Get all users with the value of setting disable_notifications equal to true
|
59
|
+
# and the settings help_tour_shown equal to false.
|
60
|
+
users = User.with_settings(disable_notifications: true, help_tour_shown: false)
|
61
|
+
```
|
62
|
+
|
63
|
+
Available options are `:column` and `:prefix`:
|
55
64
|
|
56
65
|
```
|
57
66
|
class User extends ActiveRecord::Base
|
58
|
-
add_settings
|
67
|
+
add_settings :disable_notifications, :help_tour_shown, column: :my_settings, prefix: :setting
|
59
68
|
end
|
60
69
|
```
|
61
|
-
|
62
|
-
|
70
|
+
|
71
|
+
- `:column` specify the name of the column to use (default is `settings`)
|
72
|
+
- `:prefix` add a prefix to dynamic methods
|
63
73
|
|
64
74
|
```
|
65
75
|
user.setting_disable_notifications? # => false
|
76
|
+
users = User.with_settings(setting_disable_notifications: false)
|
66
77
|
```
|
67
78
|
|
68
79
|
## Development
|
@@ -75,7 +86,6 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
75
86
|
|
76
87
|
Bug reports and pull requests are welcome on GitHub at https://github.com/pioz/bit_settings.
|
77
88
|
|
78
|
-
|
79
89
|
## License
|
80
90
|
|
81
91
|
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
CHANGED
data/bit_settings.gemspec
CHANGED
@@ -7,7 +7,7 @@ Gem::Specification.new do |spec|
|
|
7
7
|
spec.name = "bit_settings"
|
8
8
|
spec.version = BitSettings::VERSION
|
9
9
|
spec.authors = ["pioz"]
|
10
|
-
spec.email = ["
|
10
|
+
spec.email = ["epilotto@gmx.com"]
|
11
11
|
|
12
12
|
spec.summary = %q{BitSettings is a plugin for ActiveRecord that transform a column of your model in a set of boolean settings.}
|
13
13
|
spec.description = %q{BitSettings is a plugin for ActiveRecord that transform a column of your model in a set of boolean settings.}
|
@@ -19,8 +19,12 @@ Gem::Specification.new do |spec|
|
|
19
19
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
20
|
spec.require_paths = ["lib"]
|
21
21
|
|
22
|
-
spec.add_development_dependency "bundler", "
|
23
|
-
spec.add_development_dependency "rake", "
|
22
|
+
spec.add_development_dependency "bundler", ">= 2.1.0"
|
23
|
+
spec.add_development_dependency "rake", ">= 12.3"
|
24
|
+
spec.add_development_dependency "sqlite3", ">= 1.3"
|
25
|
+
spec.add_development_dependency "minitest", ">= 5.11"
|
24
26
|
|
25
|
-
spec.add_dependency "
|
27
|
+
spec.add_dependency "activesupport", ">= 4.2.0"
|
28
|
+
spec.add_dependency "activemodel", ">= 4.2.0"
|
29
|
+
spec.add_dependency "activerecord", ">= 4.2.0"
|
26
30
|
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module BooleanValue
|
2
|
+
|
3
|
+
FALSE_VALUES = [false, 0, '0', 'f', 'F', 'false', 'FALSE', 'off', 'OFF']
|
4
|
+
|
5
|
+
def self.cast_to_boolean_func
|
6
|
+
|
7
|
+
major, minor, _ = ActiveModel.version.to_s.split('.').map(&:to_i)
|
8
|
+
case major
|
9
|
+
# Rails 5
|
10
|
+
when 5
|
11
|
+
return ->(value) { ActiveModel::Type::Boolean.new.cast(value) }
|
12
|
+
# Rails 4.2
|
13
|
+
when 4
|
14
|
+
if minor >= 2
|
15
|
+
return ->(value) { ActiveRecord::Type::Boolean.new.type_cast_from_user(value) }
|
16
|
+
end
|
17
|
+
end
|
18
|
+
return ->(value) { value == '' ? nil : !FALSE_VALUES.include?(value) }
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
data/lib/bit_settings/version.rb
CHANGED
data/lib/bit_settings.rb
CHANGED
@@ -1,30 +1,59 @@
|
|
1
1
|
require 'bit_settings/version'
|
2
|
+
require 'bit_settings/boolean_value'
|
2
3
|
|
3
4
|
module BitSettings
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
included do
|
8
|
+
class_attribute :bit_settings
|
9
|
+
|
10
|
+
scope :with_settings, lambda { |h|
|
11
|
+
available = self.bit_settings.values.flatten
|
12
|
+
invalid = h.keys - available
|
13
|
+
raise "Settings #{invalid.inspect} do not exist for #{self} model: available settings are #{available.inspect}" if invalid.any?
|
14
|
+
scope = current_scope
|
15
|
+
self.bit_settings.each do |column, settings|
|
16
|
+
true_mask = h.select{|k, v| settings.include?(k) && v}.keys.reduce(0) {|memo, x| memo | (1 << settings.index(x))}
|
17
|
+
false_mask = h.select{|k, v| settings.include?(k) && !v}.keys.reduce(0) {|memo, x| memo | (1 << settings.index(x))}
|
18
|
+
scope = scope.where("#{table_name}.#{column} & #{true_mask} = #{true_mask}") if true_mask > 0
|
19
|
+
scope = scope.where("#{table_name}.#{column} & #{false_mask} = 0") if false_mask > 0
|
20
|
+
end
|
21
|
+
return scope
|
22
|
+
}
|
4
23
|
|
5
|
-
def self.included(base)
|
6
|
-
base.extend(self)
|
7
24
|
end
|
8
25
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
26
|
+
module ClassMethods
|
27
|
+
|
28
|
+
def add_settings(*settings, column: :settings, prefix: nil)
|
29
|
+
prefix = prefix ? "#{prefix}_" : ''
|
30
|
+
if settings.size > 32
|
31
|
+
raise 'You can NOT have more than 32 settings (max unsigned int with 4 bytes is 2^32-1)'
|
32
|
+
else
|
33
|
+
|
34
|
+
self.bit_settings ||= {}
|
35
|
+
self.bit_settings[column] = settings.map{|x| "#{prefix}#{x}".to_sym}
|
36
|
+
|
37
|
+
cast_to_boolean = BooleanValue.cast_to_boolean_func
|
38
|
+
|
39
|
+
settings.each_with_index do |setting, i|
|
40
|
+
define_method "#{prefix}#{setting}" do
|
41
|
+
self.send(column) & (1 << i) > 0
|
42
|
+
end
|
43
|
+
alias_method "#{prefix}#{setting}?", "#{prefix}#{setting}"
|
44
|
+
|
45
|
+
define_method "#{prefix}#{setting}=" do |value|
|
46
|
+
if cast_to_boolean.call(value)
|
47
|
+
self.send("#{column}=", self.send(column) | (1 << i))
|
48
|
+
else
|
49
|
+
self.send("#{column}=", self.send(column) & ~(1 << i))
|
50
|
+
end
|
24
51
|
end
|
25
52
|
end
|
53
|
+
|
26
54
|
end
|
27
55
|
end
|
56
|
+
|
28
57
|
end
|
29
58
|
|
30
59
|
end
|
metadata
CHANGED
@@ -1,80 +1,135 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bit_settings
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 1.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- pioz
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-10-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - "
|
17
|
+
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
19
|
+
version: 2.1.0
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- - "
|
24
|
+
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version:
|
26
|
+
version: 2.1.0
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rake
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- - "
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '12.3'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '12.3'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: sqlite3
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.3'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.3'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: minitest
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
32
60
|
- !ruby/object:Gem::Version
|
33
|
-
version: '
|
61
|
+
version: '5.11'
|
34
62
|
type: :development
|
35
63
|
prerelease: false
|
36
64
|
version_requirements: !ruby/object:Gem::Requirement
|
37
65
|
requirements:
|
38
|
-
- - "
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '5.11'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: activesupport
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 4.2.0
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 4.2.0
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: activemodel
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 4.2.0
|
90
|
+
type: :runtime
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
39
95
|
- !ruby/object:Gem::Version
|
40
|
-
version:
|
96
|
+
version: 4.2.0
|
41
97
|
- !ruby/object:Gem::Dependency
|
42
98
|
name: activerecord
|
43
99
|
requirement: !ruby/object:Gem::Requirement
|
44
100
|
requirements:
|
45
101
|
- - ">="
|
46
102
|
- !ruby/object:Gem::Version
|
47
|
-
version:
|
103
|
+
version: 4.2.0
|
48
104
|
type: :runtime
|
49
105
|
prerelease: false
|
50
106
|
version_requirements: !ruby/object:Gem::Requirement
|
51
107
|
requirements:
|
52
108
|
- - ">="
|
53
109
|
- !ruby/object:Gem::Version
|
54
|
-
version:
|
110
|
+
version: 4.2.0
|
55
111
|
description: BitSettings is a plugin for ActiveRecord that transform a column of your
|
56
112
|
model in a set of boolean settings.
|
57
113
|
email:
|
58
|
-
-
|
114
|
+
- epilotto@gmx.com
|
59
115
|
executables: []
|
60
116
|
extensions: []
|
61
117
|
extra_rdoc_files: []
|
62
118
|
files:
|
63
119
|
- ".gitignore"
|
64
120
|
- Gemfile
|
65
|
-
- LICENSE
|
121
|
+
- LICENSE
|
66
122
|
- README.md
|
67
123
|
- Rakefile
|
68
|
-
- bin/console
|
69
|
-
- bin/setup
|
70
124
|
- bit_settings.gemspec
|
71
125
|
- lib/bit_settings.rb
|
126
|
+
- lib/bit_settings/boolean_value.rb
|
72
127
|
- lib/bit_settings/version.rb
|
73
128
|
homepage: https://github.com/pioz/bit_settings
|
74
129
|
licenses:
|
75
130
|
- MIT
|
76
131
|
metadata: {}
|
77
|
-
post_install_message:
|
132
|
+
post_install_message:
|
78
133
|
rdoc_options: []
|
79
134
|
require_paths:
|
80
135
|
- lib
|
@@ -89,9 +144,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
89
144
|
- !ruby/object:Gem::Version
|
90
145
|
version: '0'
|
91
146
|
requirements: []
|
92
|
-
|
93
|
-
|
94
|
-
signing_key:
|
147
|
+
rubygems_version: 3.2.29
|
148
|
+
signing_key:
|
95
149
|
specification_version: 4
|
96
150
|
summary: BitSettings is a plugin for ActiveRecord that transform a column of your
|
97
151
|
model in a set of boolean settings.
|
data/bin/console
DELETED
@@ -1,14 +0,0 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
|
3
|
-
require "bundler/setup"
|
4
|
-
require "bit_settings"
|
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
|