bitfields 0.10.0 → 0.11.0

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: 0e8b9ea867dfd7ea37a4ad699397315239dce854
4
- data.tar.gz: f4263c4fe4dfc049937899bac0bf3c84082ab6ee
3
+ metadata.gz: dc824cf59452e64b1e34422e7416920dd725ea7d
4
+ data.tar.gz: 78a184dd4eda1d311ef1246f089babc00711a22b
5
5
  SHA512:
6
- metadata.gz: fde65c3f487dc300d4b872b84f8d060698e56b19a46e239ba5982133958390cdf1c2128a2fef999978f9878778f4c1439c3ac6adf4d9d95dfa8c5d30b211269f
7
- data.tar.gz: ed44379c4755e78d2481073960c2943f0954d4285fc4aad1f8ff6a48e4d377104e7522fef50219779e4dd1d7ddee41bb6599c9d9dbbd986dfd261ca839585eea
6
+ metadata.gz: 1e3d053623af2a5c60eb9d1818849d72b9e1d66801d2c9bb07fa12a63462770614f3a87e7b802470ca5cfd7609a0aab167d5fa2b3032c2477bdc7c48c86653ae
7
+ data.tar.gz: c45ced97d18462bf25ddfa2b4221b26e69ea1b75f293c6b200c457a415697c144f14d0c00e72dd781a24214f5e3b7e16236397103a8329a482a6a75eb4219726
data/Readme.md CHANGED
@@ -15,6 +15,9 @@ user.my_bits # => 3
15
15
 
16
16
  - records bitfield_changes `user.bitfield_changes # => {"seller" => [false, true], "insane" => [false, true]}` (also `seller_was` / `seller_change` / `seller_changed?` / `seller_became_true?` / `seller_became_false?`)
17
17
  - Individual added methods (i.e, `seller_was`, `seller_changed?`, etc..) can be deactivated with `bitfield ..., added_instance_methods: false`
18
+ - **Note**: ActiveRecord 5.2 changes the behavior of `_was` and `_changed?` methods when used in the context of an `after_save` callback.
19
+ - ActiveRecord 5.1 will use the use the values that were _just_ changed.
20
+ - ActiveRecord 5.2, however, will return `nil` for `_was` and `false` for `_changed?` since the previous changes have been persisted.
18
21
  - adds scopes `User.seller.sensible.first` (deactivate with `bitfield ..., scopes: false`)
19
22
  - builds sql `User.bitfield_sql(insane: true, sensible: false) # => '(users.my_bits & 6) = 1'`
20
23
  - builds sql with OR condition `User.bitfield_sql({ insane: true, sensible: true }, query_mode: :bit_operator_or) # => '(users.my_bits & 2) = 2 OR (users.bits & 4) = 4'`
@@ -22,6 +25,18 @@ user.my_bits # => 3
22
25
  - builds update sql `User.set_bitfield_sql(insane: true, sensible: false) == 'my_bits = (my_bits | 6) - 4'`
23
26
  - **faster sql than any other bitfield lib** through combination of multiple bits into a single sql statement
24
27
  - gives access to bits `User.bitfields[:my_bits][:sensible] # => 4`
28
+ - [`ActiveRecord::AttributeMethods::Dirty`](https://api.rubyonrails.org/v5.1.7/classes/ActiveRecord/AttributeMethods/Dirty.html) and [`ActiveModel::Dirty`](https://api.rubyonrails.org/v5.1.7/classes/ActiveModel/Dirty.html) methods can be used on each bitfield:
29
+ ```ruby
30
+ user = User.new(seller: false)
31
+ user.seller = true
32
+ user.will_save_change_to_seller? # => true
33
+ user.seller_change_to_be_saved # => [false, true]
34
+
35
+ user.save
36
+
37
+ user.seller_before_last_save # => false
38
+ user.saved_change_to_seller # => [false, true]
39
+ ```
25
40
 
26
41
  Install
27
42
  =======
@@ -50,7 +65,7 @@ User.seller.not_sensible.update_all(User.set_bitfield_sql(seller: true, insane:
50
65
  Delete the shop when a user is no longer a seller
51
66
 
52
67
  ```ruby
53
- before_save :delete_shop, if: -> { |u| u.seller_change == [true, false]}
68
+ before_save :delete_shop, if: -> { |u| u.seller_change == [true, false] }
54
69
  ```
55
70
 
56
71
  List fields and their respective values
@@ -104,6 +119,7 @@ Authors
104
119
  - [Justin Aiken](https://github.com/JustinAiken)
105
120
  - [szTheory](https://github.com/szTheory)
106
121
  - [Reed G. Law](https://github.com/reedlaw)
122
+ - [Rael Gugelmin Cunha](https://github.com/reedlaw)
107
123
 
108
124
  [Michael Grosser](http://grosser.it)<br/>
109
125
  michael@grosser.it<br/>
@@ -82,22 +82,19 @@ module Bitfields
82
82
  def add_bitfield_methods(column, options)
83
83
  bitfields[column].keys.each do |bit_name|
84
84
  if options[:added_instance_methods] != false
85
+ attribute bit_name, :boolean, default: false
86
+
85
87
  define_method(bit_name) { bitfield_value(bit_name) }
86
88
  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
89
+ define_method("#{bit_name}=") { |value| super(value); set_bitfield_value(bit_name, value) }
90
+
94
91
  define_method("#{bit_name}_became_true?") do
95
92
  value = bitfield_value(bit_name)
96
- value && bitfield_value_was(bit_name) != value
93
+ value && send("#{bit_name}_was") != value
97
94
  end
98
95
  define_method("#{bit_name}_became_false?") do
99
96
  value = bitfield_value(bit_name)
100
- !value && bitfield_value_was(bit_name) != value
97
+ !value && send("#{bit_name}_was") != value
101
98
  end
102
99
  end
103
100
 
@@ -1,3 +1,3 @@
1
1
  module Bitfields
2
- Version = VERSION = "0.10.0"
2
+ Version = VERSION = "0.11.0"
3
3
  end
metadata CHANGED
@@ -1,31 +1,31 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bitfields
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.10.0
4
+ version: 0.11.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: 2019-06-07 00:00:00.000000000 Z
11
+ date: 2019-09-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: wwtd
14
+ name: activerecord
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '0'
20
- type: :development
19
+ version: '5.1'
20
+ type: :runtime
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: '0'
26
+ version: '5.1'
27
27
  - !ruby/object:Gem::Dependency
28
- name: activerecord
28
+ name: wwtd
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - ">="