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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 464f34cd7657504d7fa8894323e46c91428fe92d
4
- data.tar.gz: 80bcee5ca0e69975aecceb30ee6332ff505f1106
3
+ metadata.gz: bbe6929010f46355a8411c24e71ad634dfabf91e
4
+ data.tar.gz: d51338d7219015ad4a65ea6ea4151e9c12174c94
5
5
  SHA512:
6
- metadata.gz: b386cc6c1f42e24a2a85d0d80dee94559b6426f4c4dc3f552f727ccf3b49ac56a7e6d943aee553fd036b6d56cce5958243ed76a66a4534ed139196d7cfc83aa8
7
- data.tar.gz: 7413b61584d468010e5953a894e9439d6bb9126462af5c8205050e604f7caa854f006de32cf0fd801453b15084bf9675ab767574674477bf6f1c6bac49dda3c1
6
+ metadata.gz: ff7742c8333aca311fa384826f234452cd9dea8429bb788775024f220521e8ce0b3155032ca0e7aca8fde28b14d79286d4e47ac9e8235929821b1a5e6b116738
7
+ data.tar.gz: 47d86235107e3f348eaf632fd28c6402bc34e83747900b23368c89f06a7586af868e28330f14ee6ee60c9038ac73155c168f206932b5ffc9537e02279ddc4096
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 settings: [:disable_notifications, :help_tour_shown]
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
- 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`:
56
64
 
57
65
  ```
58
66
  class User extends ActiveRecord::Base
59
- 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
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
@@ -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", "~> 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 "activemodel", ">= 5.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.2.0'
2
+ VERSION = '1.0.2'
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, -> (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
- 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 ActiveModel::Type::Boolean.new.cast(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,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.0
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: 2017-02-02 00:00:00.000000000 Z
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.12'
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.12'
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: '10.0'
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: '10.0'
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: 5.0.0
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: 5.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:
@@ -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.8
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
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