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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 512e7b80ce38905f3f4cc93067101dcab4c95944
4
- data.tar.gz: fb88a885f6fea1017186bc276c12d01fe322f4c2
2
+ SHA256:
3
+ metadata.gz: 22cd583daf1f3c6e6515394137361db2365632c24c62ac6a1527633a467badca
4
+ data.tar.gz: c076a8a41b3b4f9544e2f86dac2aea93fb5abdd6352d129c7b850c9e17c41a70
5
5
  SHA512:
6
- metadata.gz: 3d2cdf43903e2aaeae374886fcea7605bc253e91ef90fe1b24df9de2b9c3294d3bdcde32e67634b6c7def5ff9b4f5c3505a05737e864ec80c0e3d95894ed76bf
7
- data.tar.gz: 8e352ee9e476c829624ffd7b129b474dfebc77f0c7d8fd18c37b85c4ecd67a30dc280405ff16de2d31fb4abe762c75b841c58ee84c4b8bf605ccb43bd071d536
6
+ metadata.gz: a2233d4f0fc3fb9ed2a438a4f281f1c6742c125be8a0579ee21b35750456bfc5d5b550515d85710ad16edfc429331dc787531a9ef7dee67002201b6ccab79ea8
7
+ data.tar.gz: 257e99f707aecd320968bf52e60eb6295b85be6603bd1bbde0c8934d847e51b3975139a6eab186a6264f042c8fc49643a487ca711bf7dff2aab4d9a0dfb87209
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 settings: [:disable_notifications, :help_tour_shown]
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
- Other options are `:column` and `:prefix`:
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 settings: [:disable_notifications, :help_tour_shown], column: :my_settings, prefix: :setting
67
+ add_settings :disable_notifications, :help_tour_shown, column: :my_settings, prefix: :setting
59
68
  end
60
69
  ```
61
- * `:column` specify the name of the column to use (default is `settings`)
62
- * `:prefix` add a prefix to dynamic methods
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
@@ -1,2 +1,10 @@
1
1
  require "bundler/gem_tasks"
2
- task :default => :spec
2
+
3
+ require 'rake/testtask'
4
+ Rake::TestTask.new do |t|
5
+ t.libs << 'test' << 'lib'
6
+ t.test_files = FileList['test/**/*_test.rb']
7
+ t.verbose = true
8
+ end
9
+
10
+ task default: :test
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 = ["enrico@megiston.it"]
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.12"
23
- spec.add_development_dependency "rake", "~> 10.0"
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 "activerecord", ">= 1.0.0"
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
@@ -1,3 +1,3 @@
1
1
  module BitSettings
2
- VERSION = '0.1.0'
2
+ VERSION = '1.0.4'
3
3
  end
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
- def add_settings(settings:, column: :settings, prefix: nil)
10
- prefix = prefix ? "#{prefix}_" : ''
11
- if settings.size > 32
12
- raise 'You can NOT have more than 32 settings (max unsigned int with 4 bytes is 2^32-1)'
13
- else
14
- settings.each_with_index do |setting, i|
15
- define_method "#{prefix}#{setting}" do
16
- self.send(column) & (1 << i) > 0
17
- end
18
- alias_method "#{prefix}#{setting}?", "#{prefix}#{setting}"
19
- define_method "#{prefix}#{setting}=" do |value|
20
- if ActiveRecord::ConnectionAdapters::Column::TRUE_VALUES.include?(value)
21
- self.send("#{column}=", self.send(column) | (1 << i))
22
- else
23
- self.send("#{column}=", self.send(column) & ~(1 << i))
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: 0.1.0
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: 2016-07-29 00:00:00.000000000 Z
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: '1.12'
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: '1.12'
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: '10.0'
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: '10.0'
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: 1.0.0
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: 1.0.0
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
- - enrico@megiston.it
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.txt
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
- rubyforge_project:
93
- rubygems_version: 2.5.1
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
data/bin/setup DELETED
@@ -1,8 +0,0 @@
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