has_blob_bit_field 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +69 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/has_blob_bit_field.gemspec +29 -0
- data/lib/has_blob_bit_field.rb +8 -0
- data/lib/has_blob_bit_field/accessor.rb +76 -0
- data/lib/has_blob_bit_field/extension.rb +22 -0
- data/lib/has_blob_bit_field/version.rb +3 -0
- metadata +142 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 5a4b7e6ba65aac5f80f706712c97b16456023f06
|
4
|
+
data.tar.gz: 2f52dae5f3e2fe587bf3dbb1509f8cce48204cd2
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f9aaf3bb20548a3148d0e92d8f1a0039e75a304516ab8222d000c408fec5021d63cc159f81ea8e239bd8fd66ab633e4389ea22a9449974291516d10d846ef4ea
|
7
|
+
data.tar.gz: 203935e84e9f56f9485c0fc325380c283dbbee70fe1de92aeed93a907f2da8c435238e875553b58b1baa994c1816eb658a38468dfdc6a495700bd81f4cc99d7b
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Marc-Andre Lafortune
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
# HasBlobBitField
|
2
|
+
|
3
|
+
A Rails extension to treat a binary column (blob) as a sequence of `true`/`false` flags (bits)
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'has_blob_bit_field'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install has_blob_bit_field
|
20
|
+
|
21
|
+
## Example Usage
|
22
|
+
|
23
|
+
```
|
24
|
+
# Migration:
|
25
|
+
def change
|
26
|
+
add_column :whatevers, :many_flags_blob, :binary
|
27
|
+
end
|
28
|
+
|
29
|
+
# Model:
|
30
|
+
|
31
|
+
class YourModel < ActiveRecord::Base
|
32
|
+
has_blob_field :many_flags
|
33
|
+
end
|
34
|
+
|
35
|
+
# Usage:
|
36
|
+
|
37
|
+
o = YourModel.new
|
38
|
+
o.many_flags.size # => 0
|
39
|
+
o.many_flags.size = 666 # Can be as big as 8 times the binary limit of your column
|
40
|
+
o.many_flags[42] # => false
|
41
|
+
o.many_flags[42] = true
|
42
|
+
o.many_flags[700] # => IndexError
|
43
|
+
```
|
44
|
+
|
45
|
+
## Notes:
|
46
|
+
|
47
|
+
* The convention is that your column has the same name as your accessing method with a `'_blob'` suffix, but you can specify the column to use when calling `has_blob_field`.
|
48
|
+
|
49
|
+
* The first flag is stored using the highest bit of the first byte.
|
50
|
+
|
51
|
+
* The size is always rounded up to a multiple of 8.
|
52
|
+
|
53
|
+
* Accessing out of bounds indices raises an `IndexError`, but code could be adapted easily to return `nil` instead.
|
54
|
+
|
55
|
+
## Development
|
56
|
+
|
57
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
58
|
+
|
59
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
60
|
+
|
61
|
+
## Contributing
|
62
|
+
|
63
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/marcandre/has_blob_bit_field.
|
64
|
+
|
65
|
+
|
66
|
+
## License
|
67
|
+
|
68
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
69
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "has_blob_bit_field"
|
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
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'has_blob_bit_field/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "has_blob_bit_field"
|
8
|
+
spec.version = HasBlobBitField::VERSION
|
9
|
+
spec.authors = ["Marc-Andre Lafortune"]
|
10
|
+
spec.email = ["github@marc-andre.ca"]
|
11
|
+
|
12
|
+
spec.summary = %q{Acces a binary column as a sequence of true/false flags}
|
13
|
+
spec.description = spec.summary
|
14
|
+
spec.homepage = "https://github.com/marcandre/has_blob_bit_field"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.bindir = "exe"
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_dependency 'activemodel'
|
23
|
+
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.12"
|
25
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
26
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
27
|
+
spec.add_development_dependency "rspec-its"
|
28
|
+
spec.add_development_dependency "pry"
|
29
|
+
end
|
@@ -0,0 +1,76 @@
|
|
1
|
+
module HasBlobBitField
|
2
|
+
class Accessor
|
3
|
+
attr_reader :record, :column
|
4
|
+
|
5
|
+
def initialize record, column
|
6
|
+
@record = record
|
7
|
+
@column = column
|
8
|
+
end
|
9
|
+
|
10
|
+
def [](index)
|
11
|
+
val = byte(index)
|
12
|
+
val & flag(index) != 0
|
13
|
+
end
|
14
|
+
|
15
|
+
def []=(index, set)
|
16
|
+
set = !!set # force to true/false
|
17
|
+
val = byte(index)
|
18
|
+
mask = flag(index)
|
19
|
+
if (val & mask != 0) != set
|
20
|
+
notify_of_mutation
|
21
|
+
raw_value.setbyte(index >> 3, val ^ mask)
|
22
|
+
end
|
23
|
+
set
|
24
|
+
end
|
25
|
+
|
26
|
+
def size
|
27
|
+
raw_value.size << 3
|
28
|
+
end
|
29
|
+
|
30
|
+
def size=(size_in_bits)
|
31
|
+
raise IndexError unless size_in_bits >= 0
|
32
|
+
size_in_bytes = (size_in_bits + 7) >> 3
|
33
|
+
s = raw_value(init_nil: true)
|
34
|
+
|
35
|
+
if s.size != size_in_bytes
|
36
|
+
notify_of_mutation
|
37
|
+
if s.size > size_in_bytes
|
38
|
+
s[size_in_bytes..s.size] = ''
|
39
|
+
else
|
40
|
+
s << Array.new(size_in_bytes - s.size, 0).pack('C*')
|
41
|
+
end
|
42
|
+
end
|
43
|
+
size_in_bits
|
44
|
+
end
|
45
|
+
|
46
|
+
alias_method :length, :size
|
47
|
+
alias_method :length=, :size=
|
48
|
+
|
49
|
+
def raw_value(init_nil: false)
|
50
|
+
@record.public_send(@column) ||
|
51
|
+
(init_nil && notify_of_mutation && @record.public_send(:"#{@column}=", ''.b)) ||
|
52
|
+
''.b
|
53
|
+
end
|
54
|
+
|
55
|
+
|
56
|
+
private
|
57
|
+
def notify_of_mutation
|
58
|
+
@record.public_send :"#{@column}_will_change!"
|
59
|
+
self
|
60
|
+
end
|
61
|
+
|
62
|
+
def flag(index)
|
63
|
+
0b1000_0000 >> (index & 0b111)
|
64
|
+
end
|
65
|
+
|
66
|
+
def byte(index)
|
67
|
+
out_of_bound(index) unless index >= 0
|
68
|
+
raw_value.getbyte(index >> 3) || out_of_bound(index)
|
69
|
+
end
|
70
|
+
|
71
|
+
def out_of_bound(index)
|
72
|
+
raise IndexError, "#{index} is our of range for #{@column} which has size #{size}. Record: #{@record.inspect}}"
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'active_support'
|
2
|
+
|
3
|
+
module HasBlobBitField
|
4
|
+
module Extension
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
class_methods do
|
8
|
+
def has_blob_bit_field field, column: :"#{field}_blob"
|
9
|
+
class_eval <<-EVAL, __FILE__, __LINE__
|
10
|
+
def #{field}
|
11
|
+
Accessor.new self, :#{column}
|
12
|
+
end
|
13
|
+
|
14
|
+
def #{field}=
|
15
|
+
raise "Don't set the pseudo field #{field} directly, use []= on it"
|
16
|
+
end
|
17
|
+
EVAL
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
end
|
metadata
ADDED
@@ -0,0 +1,142 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: has_blob_bit_field
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Marc-Andre Lafortune
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-08-20 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activemodel
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.12'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.12'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec-its
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: pry
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
description: Acces a binary column as a sequence of true/false flags
|
98
|
+
email:
|
99
|
+
- github@marc-andre.ca
|
100
|
+
executables: []
|
101
|
+
extensions: []
|
102
|
+
extra_rdoc_files: []
|
103
|
+
files:
|
104
|
+
- ".gitignore"
|
105
|
+
- ".rspec"
|
106
|
+
- ".travis.yml"
|
107
|
+
- Gemfile
|
108
|
+
- LICENSE.txt
|
109
|
+
- README.md
|
110
|
+
- Rakefile
|
111
|
+
- bin/console
|
112
|
+
- bin/setup
|
113
|
+
- has_blob_bit_field.gemspec
|
114
|
+
- lib/has_blob_bit_field.rb
|
115
|
+
- lib/has_blob_bit_field/accessor.rb
|
116
|
+
- lib/has_blob_bit_field/extension.rb
|
117
|
+
- lib/has_blob_bit_field/version.rb
|
118
|
+
homepage: https://github.com/marcandre/has_blob_bit_field
|
119
|
+
licenses:
|
120
|
+
- MIT
|
121
|
+
metadata: {}
|
122
|
+
post_install_message:
|
123
|
+
rdoc_options: []
|
124
|
+
require_paths:
|
125
|
+
- lib
|
126
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
127
|
+
requirements:
|
128
|
+
- - ">="
|
129
|
+
- !ruby/object:Gem::Version
|
130
|
+
version: '0'
|
131
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
132
|
+
requirements:
|
133
|
+
- - ">="
|
134
|
+
- !ruby/object:Gem::Version
|
135
|
+
version: '0'
|
136
|
+
requirements: []
|
137
|
+
rubyforge_project:
|
138
|
+
rubygems_version: 2.2.2
|
139
|
+
signing_key:
|
140
|
+
specification_version: 4
|
141
|
+
summary: Acces a binary column as a sequence of true/false flags
|
142
|
+
test_files: []
|