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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d97ce2d0cab3c7fd6b3d21d91b95961ec0f01b2d
4
- data.tar.gz: cd53f0020540f8d34514c6f99aca1fc1bcdcb285
3
+ metadata.gz: 233507e37878a02a78985dee4d2eb03d2cbeaa56
4
+ data.tar.gz: c78ab2f292b25e1b630d669b2d97771781a4d057
5
5
  SHA512:
6
- metadata.gz: 938985833d6fac258ea5ecd54b11880781fc3b1047deec846414d9d7e7890bacefb56c227bf1c5b62088d8501282318c1d3ce8f98c3915982754e49599f11999
7
- data.tar.gz: 946f21fa6884985c602c60ee8f535a97b53ca8effe044e812f5bdd02d1eaaf872afb779d68b667d7f82867db76521026eda4fa7b74d687d9ac27ce9d4f9f5876
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 => :stupid
7
+ bitfield :my_bits, 1 => :seller, 2 => :insane, 4 => :sensible
8
8
  end
9
9
 
10
- user = User.new(foo: true, insane: true)
11
- user.seller == true
12
- user.stupid? == false
13
- user.my_bits == 3
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 == {foo: [false, true]}` (also `foo_was` / `foo_change` / `foo_changed?` / `foo_became_true?`)
17
- - adds scopes `User.foo.stupid.first` (deactivate with `bitfield ..., scopes: false`)
18
- - builds sql `User.bitfield_sql(insane: true, stupid: false) == '(users.my_bits & 3) = 1'`
19
- - builds index-using sql with `bitfield ... , query_mode: :in_list` and `User.bitfield_sql(insane: true, stupid: false) == 'users.my_bits IN (2, 3)'` (2 and 1+2), often slower than :bit_operator sql especially for high number of bits
20
- - builds update sql `User.set_bitfield_sql(insane: true, stupid: false) == 'my_bits = (my_bits | 6) - 4'`
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][:stupid] == 4`
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.not_stupid.update_all(User.set_bitfield_sql(seller: true, insane: true))
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) == { seller: false, insane: true, stupid: false }
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/>
@@ -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
- define_method(bit_name) { bitfield_value(bit_name) }
85
- define_method("#{bit_name}?") { bitfield_value(bit_name) }
86
- define_method("#{bit_name}=") { |value| set_bitfield_value(bit_name, value) }
87
- define_method("#{bit_name}_was") { bitfield_value_was(bit_name) }
88
- define_method("#{bit_name}_changed?") { bitfield_value_was(bit_name) != bitfield_value(bit_name) }
89
- define_method("#{bit_name}_change") do
90
- values = [bitfield_value_was(bit_name), bitfield_value(bit_name)]
91
- values unless values[0] == values[1]
92
- end
93
- define_method("#{bit_name}_became_true?") do
94
- value = bitfield_value(bit_name)
95
- value && bitfield_value_was(bit_name) != value
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
@@ -1,3 +1,3 @@
1
1
  module Bitfields
2
- Version = VERSION = "0.7.0"
2
+ Version = VERSION = "0.8.0"
3
3
  end
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.7.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: 2016-02-25 00:00:00.000000000 Z
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: 1.9.3
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.4.5.1
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