bithex 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 4bf95c38d17be6a0a1a3c2f71935b82d1d5add9886950a0f46bea231bcb82b6c
4
+ data.tar.gz: ad9055a08bcc1b54ed373ca56ae19ebbd90d71d9c956c9f4fc9d8d8d678f6f10
5
+ SHA512:
6
+ metadata.gz: 4273bf78339a029633f7d2904b371c094048b510dbff139245f6a8007fd3b404ec7f0d6a3ae29134049eb4512f7cf47ea0a65a9bede814983eeed8c5b3210f9a
7
+ data.tar.gz: 120ad8343b393a0bc4af815ea1e8a3a3c0de73bb1b58f402cf15de89ffe9188fba8c45815bedc4ae76fd878c5d871456726989eb96debf0dfa657d99c4e04251
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ /Gemfile.lock
10
+ .DS_Store
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,11 @@
1
+ language: ruby
2
+
3
+ sudo: required
4
+
5
+ rvm:
6
+ - "2.5.3"
7
+
8
+ cache:
9
+ bundler: true
10
+
11
+ script: 'bundle exec rspec'
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in bithex.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Rustam Sharshenov
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,56 @@
1
+ # Bithex [![Build Status](https://travis-ci.org/sharshenov/bithex.svg?branch=master)](https://travis-ci.org/sharshenov/bithex) [![Gem Version](https://badge.fury.io/rb/bithex.svg)](https://badge.fury.io/rb/bithex)
2
+
3
+ Store hex strings as [PostgreSQL bit strings](https://edgeguides.rubyonrails.org/active_record_postgresql.html#bit-string-types) to reduce column size 2x.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'bithex'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install bithex
20
+
21
+ ## Usage
22
+
23
+ Create column
24
+
25
+ ```
26
+ create_table :uploads do |t|
27
+ t.column :digest, "bit(160)" # For SHA1 limit is 160, for MD5 limit is 128
28
+ end
29
+ ```
30
+
31
+ Define bithex attributes
32
+
33
+ ```ruby
34
+ class Upload < ActiveRecord::Base
35
+ bithex :digest
36
+
37
+ # if you have multiple attributes to store as bit string
38
+ # bithex :foo, :bar, :baz
39
+ end
40
+ ```
41
+
42
+ Treat you bithex attribute as usual string, but remember: string length is hardcoded by limit of bit column (40 for SHA1, 32 for MD5)
43
+
44
+ ```ruby
45
+ upload = Upload.new
46
+ upload.digest = Digest::SHA1.hexdigest('foo bar baz') # c7567e8b39e2428e38bf9c9226ac68de4c67dc39
47
+ upload.save
48
+ ```
49
+
50
+ ## Contributing
51
+
52
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/bithex.
53
+
54
+ ## License
55
+
56
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler/gem_tasks'
2
+ task default: :spec
data/bithex.gemspec ADDED
@@ -0,0 +1,27 @@
1
+ lib = File.expand_path('lib', __dir__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'bithex/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'bithex'
7
+ spec.version = Bithex::VERSION
8
+ spec.authors = ['Rustam Sharshenov']
9
+ spec.email = ['rustam@sharshenov.com']
10
+
11
+ spec.summary = 'Store hex strings as bit strings'
12
+ spec.description = 'Store hex strings as bit strings in PostgreSQL'
13
+ spec.homepage = 'https://github.com/sharshenov/bithex'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
17
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(spec)/}) }
18
+ end
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_dependency 'activerecord', '>= 4.2'
22
+
23
+ spec.add_development_dependency 'bundler', '~> 1.16'
24
+ spec.add_development_dependency 'pg'
25
+ spec.add_development_dependency 'rake', '~> 10.0'
26
+ spec.add_development_dependency 'rspec'
27
+ end
@@ -0,0 +1,22 @@
1
+ module Bithex
2
+ # It is supposed to be included in ActiveRecord::Base
3
+ module ActiveRecord
4
+ extend ActiveSupport::Concern
5
+
6
+ # Bithex attributes are defined like this:
7
+ # class Upload < ActiveRecord::Base
8
+ # bithex :foo, :bar, :baz
9
+ # end
10
+ module ClassMethods
11
+ def bithex(*definitions)
12
+ definitions.each do |name|
13
+ attr = attribute_alias?(name) ? attribute_alias(name) : name
14
+
15
+ decorate_attribute_type(attr, :bithex) do |subtype|
16
+ Bithex::Type.new(attr, subtype)
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,59 @@
1
+ module Bithex
2
+ # Serializes hex string to bit string and vice versa
3
+ class Type < ActiveRecord::Type::Value
4
+ attr_reader :attr,
5
+ :subtype
6
+
7
+ def initialize(attr, subtype)
8
+ @attr = attr
9
+ @subtype = subtype
10
+ validate_subtype
11
+ end
12
+
13
+ def deserialize(value)
14
+ return if value.blank?
15
+
16
+ format("%0#{hex_value_length}x", value.to_i(2))
17
+ end
18
+
19
+ def serialize(value)
20
+ return if value.blank?
21
+
22
+ [value.to_s].pack('H*').unpack1('B*')
23
+ end
24
+
25
+ def assert_valid_value(value)
26
+ return if value.blank?
27
+
28
+ validate_format(value)
29
+ validate_length(value)
30
+ end
31
+
32
+ private
33
+
34
+ def hex_value_length
35
+ subtype.limit / 4
36
+ end
37
+
38
+ def validate_subtype
39
+ return if subtype.is_a?(::ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Bit)
40
+
41
+ raise ::ActiveRecord::SerializationTypeMismatch,
42
+ "Expected '#{attr}' to be a bit string. Check DB column type."
43
+ end
44
+
45
+ def validate_format(value)
46
+ return if value.match?(/\h+/)
47
+
48
+ raise ArgumentError,
49
+ "Expected '#{value}' to be a hex string"
50
+ end
51
+
52
+ def validate_length(value)
53
+ return if value.to_s.length == hex_value_length
54
+
55
+ raise ArgumentError,
56
+ "Expected length of '#{value}' to be equal to #{hex_value_length}"
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,3 @@
1
+ module Bithex
2
+ VERSION = '0.1.0'.freeze
3
+ end
data/lib/bithex.rb ADDED
@@ -0,0 +1,7 @@
1
+ require 'active_record'
2
+
3
+ require 'bithex/type'
4
+ require 'bithex/active_record'
5
+ require 'bithex/version'
6
+
7
+ ActiveRecord::Base.include Bithex::ActiveRecord
metadata ADDED
@@ -0,0 +1,126 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bithex
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Rustam Sharshenov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-12-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '4.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '4.2'
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.16'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.16'
41
+ - !ruby/object:Gem::Dependency
42
+ name: pg
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
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
+ description: Store hex strings as bit strings in PostgreSQL
84
+ email:
85
+ - rustam@sharshenov.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".travis.yml"
93
+ - Gemfile
94
+ - LICENSE.txt
95
+ - README.md
96
+ - Rakefile
97
+ - bithex.gemspec
98
+ - lib/bithex.rb
99
+ - lib/bithex/active_record.rb
100
+ - lib/bithex/type.rb
101
+ - lib/bithex/version.rb
102
+ homepage: https://github.com/sharshenov/bithex
103
+ licenses:
104
+ - MIT
105
+ metadata: {}
106
+ post_install_message:
107
+ rdoc_options: []
108
+ require_paths:
109
+ - lib
110
+ required_ruby_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ required_rubygems_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ requirements: []
121
+ rubyforge_project:
122
+ rubygems_version: 2.7.7
123
+ signing_key:
124
+ specification_version: 4
125
+ summary: Store hex strings as bit strings
126
+ test_files: []