bit_settings 0.2.0 → 1.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/{LICENSE.txt → LICENSE} +0 -0
- data/README.md +12 -3
- 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 +68 -13
- data/bin/console +0 -14
- data/bin/setup +0 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bbe6929010f46355a8411c24e71ad634dfabf91e
|
4
|
+
data.tar.gz: d51338d7219015ad4a65ea6ea4151e9c12174c94
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ff7742c8333aca311fa384826f234452cd9dea8429bb788775024f220521e8ce0b3155032ca0e7aca8fde28b14d79286d4e47ac9e8235929821b1a5e6b116738
|
7
|
+
data.tar.gz: 47d86235107e3f348eaf632fd28c6402bc34e83747900b23368c89f06a7586af868e28330f14ee6ee60c9038ac73155c168f206932b5ffc9537e02279ddc4096
|
data/{LICENSE.txt → LICENSE}
RENAMED
File without changes
|
data/README.md
CHANGED
@@ -39,7 +39,7 @@ Then in your model:
|
|
39
39
|
```
|
40
40
|
class User extends ActiveRecord::Base
|
41
41
|
include BitSettings
|
42
|
-
add_settings
|
42
|
+
add_settings :disable_notifications, :help_tour_shown
|
43
43
|
end
|
44
44
|
```
|
45
45
|
|
@@ -52,11 +52,19 @@ user.disable_notifications = true # => true
|
|
52
52
|
user.save
|
53
53
|
```
|
54
54
|
|
55
|
-
|
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`:
|
56
64
|
|
57
65
|
```
|
58
66
|
class User extends ActiveRecord::Base
|
59
|
-
add_settings
|
67
|
+
add_settings :disable_notifications, :help_tour_shown, column: :my_settings, prefix: :setting
|
60
68
|
end
|
61
69
|
```
|
62
70
|
* `:column` specify the name of the column to use (default is `settings`)
|
@@ -64,6 +72,7 @@ end
|
|
64
72
|
|
65
73
|
```
|
66
74
|
user.setting_disable_notifications? # => false
|
75
|
+
users = User.with_settings(setting_disable_notifications: false)
|
67
76
|
```
|
68
77
|
|
69
78
|
## Development
|
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", "~> 1.
|
23
|
-
spec.add_development_dependency "rake", "~>
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.16"
|
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, -> (h) do
|
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
|
+
end
|
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,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bit_settings
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2
|
4
|
+
version: 1.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- pioz
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2018-02-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -16,59 +16,114 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '1.
|
19
|
+
version: '1.16'
|
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: '1.
|
26
|
+
version: '1.16'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rake
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
31
|
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '
|
33
|
+
version: '12.3'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
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
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '5.11'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
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
|
41
83
|
- !ruby/object:Gem::Dependency
|
42
84
|
name: activemodel
|
43
85
|
requirement: !ruby/object:Gem::Requirement
|
44
86
|
requirements:
|
45
87
|
- - ">="
|
46
88
|
- !ruby/object:Gem::Version
|
47
|
-
version:
|
89
|
+
version: 4.2.0
|
90
|
+
type: :runtime
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: 4.2.0
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: activerecord
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::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:
|
@@ -90,7 +145,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
90
145
|
version: '0'
|
91
146
|
requirements: []
|
92
147
|
rubyforge_project:
|
93
|
-
rubygems_version: 2.6.
|
148
|
+
rubygems_version: 2.6.13
|
94
149
|
signing_key:
|
95
150
|
specification_version: 4
|
96
151
|
summary: BitSettings is a plugin for ActiveRecord that transform a column of your
|
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
|