bitfields 0.7.0 → 0.8.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Readme.md +15 -13
- data/lib/bitfields.rb +14 -12
- data/lib/bitfields/version.rb +1 -1
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 233507e37878a02a78985dee4d2eb03d2cbeaa56
|
4
|
+
data.tar.gz: c78ab2f292b25e1b630d669b2d97771781a4d057
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b24a6df7be0f2a1c76d9e19aa8aebd34c72220dc6bfe91d1aa03242daa2921d401fa652587024c43ef10c64844413d3fb34bdab03328695c6df8440515329f37
|
7
|
+
data.tar.gz: 629bf10a3e5b9476c4b96d316b0c84ab79f4ced827d0d25bd7aa9dbc45a4554b390267bcdb5e4593ea5d56c3a37d8c47924dd6375b79acc5b7e26c0ae8ff0fa2
|
data/Readme.md
CHANGED
@@ -4,22 +4,23 @@ e.g. true-false-false = 1, false-true-false = 2, true-false-true = 5 (1,2,4,8,.
|
|
4
4
|
```ruby
|
5
5
|
class User < ActiveRecord::Base
|
6
6
|
include Bitfields
|
7
|
-
bitfield :my_bits, 1 => :seller, 2 => :insane, 4 => :
|
7
|
+
bitfield :my_bits, 1 => :seller, 2 => :insane, 4 => :sensible
|
8
8
|
end
|
9
9
|
|
10
|
-
user = User.new(
|
11
|
-
user.seller
|
12
|
-
user.
|
13
|
-
user.my_bits
|
10
|
+
user = User.new(seller: true, insane: true)
|
11
|
+
user.seller # => true
|
12
|
+
user.sensible? # => false
|
13
|
+
user.my_bits # => 3
|
14
14
|
```
|
15
15
|
|
16
|
-
- records bitfield_changes `user.bitfield_changes
|
17
|
-
|
18
|
-
-
|
19
|
-
- builds
|
20
|
-
- builds
|
16
|
+
- records bitfield_changes `user.bitfield_changes # => {"seller" => [false, true], "insane" => [false, true]}` (also `seller_was` / `seller_change` / `seller_changed?` / `seller_became_true?`)
|
17
|
+
- Individual added methods (i.e, `seller_was`, `seller_changed?`, etc..) can be deactivated with `bitfield ..., added_instance_methods: false`
|
18
|
+
- adds scopes `User.seller.sensible.first` (deactivate with `bitfield ..., scopes: false`)
|
19
|
+
- builds sql `User.bitfield_sql(insane: true, sensible: false) # => '(users.my_bits & 6) = 1'`
|
20
|
+
- builds index-using sql with `bitfield ... , query_mode: :in_list` and `User.bitfield_sql(insane: true, sensible: false) # => 'users.my_bits IN (2, 3)'` (2 and 1+2) often slower than :bit_operator sql especially for high number of bits
|
21
|
+
- builds update sql `User.set_bitfield_sql(insane: true, sensible: false) == 'my_bits = (my_bits | 6) - 4'`
|
21
22
|
- **faster sql than any other bitfield lib** through combination of multiple bits into a single sql statement
|
22
|
-
- gives access to bits `User.bitfields[:my_bits][:
|
23
|
+
- gives access to bits `User.bitfields[:my_bits][:sensible] # => 4`
|
23
24
|
|
24
25
|
Install
|
25
26
|
=======
|
@@ -42,7 +43,7 @@ Examples
|
|
42
43
|
Update all users
|
43
44
|
|
44
45
|
```ruby
|
45
|
-
User.seller.
|
46
|
+
User.seller.not_sensible.update_all(User.set_bitfield_sql(seller: true, insane: true))
|
46
47
|
```
|
47
48
|
|
48
49
|
Delete the shop when a user is no longer a seller
|
@@ -55,7 +56,7 @@ List fields and their respective values
|
|
55
56
|
|
56
57
|
```ruby
|
57
58
|
user = User.new(insane: true)
|
58
|
-
user.bitfield_values(:my_bits)
|
59
|
+
user.bitfield_values(:my_bits) # => { seller: false, insane: true, sensible: false }
|
59
60
|
```
|
60
61
|
|
61
62
|
TIPS
|
@@ -99,6 +100,7 @@ Authors
|
|
99
100
|
- [John Wilkinson](https://github.com/jcwilk)
|
100
101
|
- [PeppyHeppy](https://github.com/peppyheppy)
|
101
102
|
- [kmcbride](https://github.com/kmcbride)
|
103
|
+
- [Justin Aiken](https://github.com/JustinAiken)
|
102
104
|
|
103
105
|
[Michael Grosser](http://grosser.it)<br/>
|
104
106
|
michael@grosser.it<br/>
|
data/lib/bitfields.rb
CHANGED
@@ -81,18 +81,20 @@ module Bitfields
|
|
81
81
|
|
82
82
|
def add_bitfield_methods(column, options)
|
83
83
|
bitfields[column].keys.each do |bit_name|
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
84
|
+
if options[:added_instance_methods] != false
|
85
|
+
define_method(bit_name) { bitfield_value(bit_name) }
|
86
|
+
define_method("#{bit_name}?") { bitfield_value(bit_name) }
|
87
|
+
define_method("#{bit_name}=") { |value| set_bitfield_value(bit_name, value) }
|
88
|
+
define_method("#{bit_name}_was") { bitfield_value_was(bit_name) }
|
89
|
+
define_method("#{bit_name}_changed?") { bitfield_value_was(bit_name) != bitfield_value(bit_name) }
|
90
|
+
define_method("#{bit_name}_change") do
|
91
|
+
values = [bitfield_value_was(bit_name), bitfield_value(bit_name)]
|
92
|
+
values unless values[0] == values[1]
|
93
|
+
end
|
94
|
+
define_method("#{bit_name}_became_true?") do
|
95
|
+
value = bitfield_value(bit_name)
|
96
|
+
value && bitfield_value_was(bit_name) != value
|
97
|
+
end
|
96
98
|
end
|
97
99
|
|
98
100
|
if options[:scopes] != false
|
data/lib/bitfields/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bitfields
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.8.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Michael Grosser
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2018-03-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: wwtd
|
@@ -116,7 +116,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
116
116
|
requirements:
|
117
117
|
- - ">="
|
118
118
|
- !ruby/object:Gem::Version
|
119
|
-
version:
|
119
|
+
version: 2.0.0
|
120
120
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
121
121
|
requirements:
|
122
122
|
- - ">="
|
@@ -124,7 +124,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
124
124
|
version: '0'
|
125
125
|
requirements: []
|
126
126
|
rubyforge_project:
|
127
|
-
rubygems_version: 2.
|
127
|
+
rubygems_version: 2.6.11
|
128
128
|
signing_key:
|
129
129
|
specification_version: 4
|
130
130
|
summary: Save migrations and columns by storing multiple booleans in a single integer
|