bit_magic 0.1.1 → 0.1.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: 82716dcf8e54d3738278f98af111dfedb4cd1884
4
- data.tar.gz: a6385df6cdea437a50a9244be17591510b8fd56d
3
+ metadata.gz: 01c52e3c36c41e083befe5e264586a2616648c46
4
+ data.tar.gz: b35adc6e8c1bd6e9147967839f7df871bcfa4b09
5
5
  SHA512:
6
- metadata.gz: c8943a196330e9d762e2905bf3a2e8f4559a72496981b3af10b0135462c7b881b42003be8fb1b79634c2f74d80773279a82d9acefff24e9346e2033a7f3b83a9
7
- data.tar.gz: c5f535ecfb9a38c144be10efc1635820f4a10193bd8f73332e36f0313e19a8a13b5341e2e3148ee4bbc383491ad8f2e438fcdfd91432b0c8fe44f4cc324850bf
6
+ metadata.gz: c56c3f10c054a1eb5380d59be289cc96879ad8eb281133e2506784e4060ddaac0c7c818594262b9de179abba2259a62fbdf34ae1b8b3b233077f0dd54ef57a20
7
+ data.tar.gz: 3cb3f8a3c216ebb17d6fe301d77c9251561bf900943bbc0b116e41b29fdb9f7c83ef5823a5f409386dccdb9437df8a467d0540131366c6e6417f94f138d1efab
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- bit_magic (0.1.1)
4
+ bit_magic (0.1.2)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -80,8 +80,11 @@ field.read_field(1, 3, 2)
80
80
 
81
81
  Bits Generator is a generator for Integer bit representations or arrays of values that match certain bitwise criteria. The criteria are:
82
82
 
83
- | Name / Alias | Criteria |
84
- |------------------------ | ----------------- |
83
+ | Name / Alias | Criteria /Action |
84
+ |------------------------ | ------------------------ |
85
+ | bits_for(\*field\_names)| returns list of bits for given names |
86
+ | each_value(each\_bits = nil, &block) | yields block for all available values from combinations of bits, optional: specify different list of bits |
87
+ | all_values(each\_bits = nil) | returns an array with all available values from combinations of bits, optional: specify different list of bits |
85
88
  | with\_any / any\_of | any of these bits specified are true |
86
89
  | with\_all / all\_of | all of these bits are true |
87
90
  | without\_any / none\_of | none of these bits are true |
@@ -90,7 +93,9 @@ Bits Generator is a generator for Integer bit representations or arrays of value
90
93
 
91
94
  These all have a corresponding *_number method that returns an integer. (equal\_to's version is named equal\_to\_numbers and returns an array of two numbers, one for bit values 1 and the other for bit values 0).
92
95
 
93
- * Warning: Although bitwise operations are fast, when using this class, you need to be careful when you use lots of bits (more than 20) to return arrays because memory usage grows exponentially! 2**20 is over 1 million, that's 8 megabytes of memory for the array on a 64-bit OS, with 24 bits, it explodes to 134 megabytes!
96
+ * Warning: Although bitwise operations are fast, when using this class, you need to be careful when you use lots of bits (more than 16) to return arrays because memory usage grows exponentially! 2**20 is over 1 million, that's 8 megabytes of memory for the array on a 64-bit OS, with 24 bits, it explodes to 134 megabytes!
97
+
98
+ * Warning: Also, because we use combinations to iterate over the combinations of all possible bit values, the time complexity is O(n * 2^(n-1)) for `each_value` and `all_values`. Please carefully benchmark your use case for time and memory usage if you have more than 16 bits.
94
99
 
95
100
  Example:
96
101
 
@@ -163,29 +168,29 @@ The bit field definition above will define methods based on the name given. For
163
168
  total bits in the field)
164
169
  This is equivalent to the conditional: `field[0] = value[0] and field[1] = value[1] ...`
165
170
  * YourModel.settings\_notify [1]
166
- shorthand for `settings\_with\_all(:notify)`
171
+ shorthand for `settings_with_all(:notify)`
167
172
  * YourModel.settings\_not\_notify [1]
168
- shorthand for `settings\_without\_all(:notify)`
173
+ shorthand for `settings_without_all(:notify)`
169
174
  * YourModel.settings\_max\_backlog [1]
170
- shorthand for `settings\_with\_all(:max\_backlog)`
175
+ shorthand for `settings_with_all(:max_backlog)`
171
176
  note that because this is a field with 3 bits, it's equivalent to max\_backlog=4
172
177
  * YourModel.settings\_not\_max\_backlog [1]
173
- shorthand for `settings\_without\_all(:max\_backlog)`
178
+ shorthand for `settings_without_all(:max_backlog)`
174
179
  * YourModel.settings\_max\_backlog\_equals(val) [1]
175
180
  returns a query where max\_backlog equals the value. Note: Only compares bit up to 3 bits because that's how big max_backlog is.
176
- * YourModel.settings\_disabled [1] - shorthand for `settings\_with\_all(:disabled)`
177
- * YourModel.settings\_not\_disabled [1] - shorthand for `settings\_without\_all(:disabled)`
181
+ * YourModel.settings\_disabled [1] - shorthand for `settings_with_all(:disabled)`
182
+ * YourModel.settings\_not\_disabled [1] - shorthand for `settings_without_all(:disabled)`
178
183
  * Instance Methods
179
184
  * YourModel#settings - returns a Bits object for you to work with fields directly
180
- * YourModel#settings\_enabled?(\*field\_names) - shorthand for `settings.enabled?(\*field\_names)`
181
- * YourModel#settings\_disabled?(\*field\_names) - shorthand for `settings.disabled?(\*field\_names)`
185
+ * YourModel#settings\_enabled?(\*field\_names) - shorthand for `settings.enabled?(*field_names)`
186
+ * YourModel#settings\_disabled?(\*field\_names) - shorthand for `settings.disabled?(*field_names)`
182
187
  * YourModel#notify [2] - returns 1 or 0, shorthand for `settings.read(:notify)`
183
188
  * YourModel#notify? [2] - returns true or false, shorthand for `settings.read(:notify) == 1`
184
189
  * YourModel#notify=(val) [2] - sets value for notify flag, shorthand for `settings.write(:notify, val)`
185
190
  * YourModel#max\_backlog [2] - returns a number from 0 to 7 (3 bits), shorthand for `settings.read(:max_backlog)`
186
191
  * YourModel#max\_backlog=(val) [2]
187
192
  sets value for max\_backlog, only cares about the last 3 bits, so numbers larger than 3 bits are truncated.
188
- shorthand for `settings.write(:max\_backlog, val)`
193
+ shorthand for `settings.write(:max_backlog, val)`
189
194
  * YourModel#disabled [2] - returns 1 or 0, shorthand for `settings.read(:disabled)`
190
195
  * YourModel#disabled? [2] - returns true or false, shorthand for `settings.read(:disabled) == 1`
191
196
  * YourModel#disabled=(val) [2] - sets value for disabled
@@ -217,19 +222,38 @@ ActiveRecord::Base.include BitMagic::Adapters::ActiveRecordAdapter
217
222
  Otherwise, you'll need to include it on every model where you want to use bit_magic:
218
223
 
219
224
  ```ruby
220
- class YourModel < ActiveRecord::Base
225
+ class ArExample < ActiveRecord::Base
221
226
  # the include below can be removed if activated globally above
222
227
  include BitMagic::Adapters::ActiveRecordAdapter
223
228
 
224
- # this defines the bits we will be using for our bitfield.
225
- # total usable bits is based off what int you are using in the database.
229
+ # This defines the bits we will be using for our bitfield.
230
+ # Total usable bits is based off what int you are using in the table.
226
231
  bit_magic :example, 0 => :is_odd, 1 => :one, 2 => :two, 3 => :wiggle, [4, 5, 6] => :my_shoe
227
232
  end
228
233
  ```
229
234
 
230
235
  You must have an integer column or attribute in the table to use as the bit field container. By default, we assume the column is named `flags`. It should be `NOT NULL` and have a default set.
231
236
 
232
- After defining `bit_magic :name`, you can use the methods signature described above.
237
+ After defining `bit_magic :name`, you can use the methods signature described above. Below is an small example based on the definition above.
238
+
239
+ ```ruby
240
+ ArExample.create(:is_odd => true, :wiggle => true, :my_shoe => 7)
241
+ current = ArExample.example_my_shoe_equals(7).last
242
+ current.is_odd?
243
+ #=> true
244
+ current.two?
245
+ #=> false
246
+ current.wiggle
247
+ #=> 1
248
+ current.my_shoe
249
+ #=> 7
250
+ current.flags
251
+ #=> 121
252
+ current.is_odd = false
253
+ current.two = true
254
+ current.flags
255
+ #=> 124
256
+ ```
233
257
 
234
258
  #### Mongoid
235
259
 
@@ -243,13 +267,13 @@ Mongoid::Document.include BitMagic::Adapters::MongoidAdapter
243
267
  Otherwise, you'll need to include it on every model where you want to use bit_magic:
244
268
 
245
269
  ```ruby
246
- class YourModel
270
+ class MongoExample
247
271
  include Mongoid::Document
248
272
 
249
- # the include below can be removed if activated globally above
273
+ # The include below can be removed if activated globally above
250
274
  include BitMagic::Adapters::MongoidAdapter
251
275
 
252
- # this defines the bits we will be using for our bitfield.
276
+ # This defines the bits we will be using for our bitfield.
253
277
  bit_magic :example, 0 => :is_odd, 1 => :one, 2 => :two, 3 => :wiggle, [4, 5, 6] => :my_shoe
254
278
  field :flags, type: Integer, default: 0
255
279
  end
@@ -257,6 +281,25 @@ end
257
281
 
258
282
  You must have an integer field or attribute in the table to use as the bit field container. By default, we assume the column is named `flags`.
259
283
 
284
+ ```ruby
285
+ MongoExample.create(:is_odd => true, :wiggle => true, :my_shoe => 7)
286
+ current = MongoExample.example_my_shoe_equals(7).last
287
+ current.is_odd?
288
+ #=> true
289
+ current.two?
290
+ #=> false
291
+ current.wiggle
292
+ #=> 1
293
+ current.my_shoe
294
+ #=> 7
295
+ current.flags
296
+ #=> 121
297
+ current.is_odd = false
298
+ current.two = true
299
+ current.flags
300
+ #=> 124
301
+ ```
302
+
260
303
  ### Custom Integrations
261
304
 
262
305
  You can make your own custom adapter for your own use-case. At its core, all integrations boil down to:
@@ -86,8 +86,9 @@ module BitMagic
86
86
  # grows to 2**20 = 1048576. At 32 bits, 2**32 = 4294967296.
87
87
  #
88
88
  # Warning 2: We're using combinations to generate each individual number, so
89
- # there's additional overhead causing O(n!) complexity. Use carefully when
90
- # you have large bit lists (more than 16 bits total). Check timing and memory.
89
+ # there's additional overhead causing O(n * 2^(n-1)) time complexity. Carefully
90
+ # benchmark when you have large bit lists (more than 16 bits total) and
91
+ # check both timing and memory for your use case.
91
92
  #
92
93
  # @param [optional, Array<Integer>] each_bits a list of bits used to generate
93
94
  # the combination list. default: the list of bits given during initialization
@@ -1,5 +1,5 @@
1
1
  module BitMagic
2
- VERSION = "0.1.1".freeze
2
+ VERSION = "0.1.2".freeze
3
3
 
4
4
  def self.version
5
5
  VERSION
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bit_magic
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kia Kroas
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-09-12 00:00:00.000000000 Z
11
+ date: 2018-09-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler