integer_hash 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 69f1b9fe5811e8e3b473bf259c936f194ce92f5e
4
+ data.tar.gz: 16239f6487e78623853fbfb25e1d64eb3d13ecb8
5
+ SHA512:
6
+ metadata.gz: e4cb473dc98b39435842bf5fb8c92ec08ca9066212824ef978af6861006409f0eb1b4c70b15ef8e6d62a8290cc656a5e1c631bf148d4f0d791e9b7f6c1cda932
7
+ data.tar.gz: 0917e5cad6993ce2de2f4e0031b2e6eb19e6498172b0c333595bdba62690acb68ee9d17a5278db13e615a26e7abd888bc6013737cd6f819a54c851a6f483e2e5
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,12 @@
1
+ language: ruby
2
+ cache: bundler
3
+
4
+ rvm:
5
+ - 2.2.3
6
+ - 2.1.7
7
+ - 2.0.0-p647
8
+ - 1.9.3-p551
9
+
10
+ script: 'bundle exec rake'
11
+
12
+ before_install: gem install bundler -v 1.10.6
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in integer_hash.gemspec
4
+ gemspec
5
+
6
+ gem "codeclimate-test-reporter", group: :test, require: nil
7
+ gem "sqlite3", group: :test, require: nil
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Florin Popa
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.
@@ -0,0 +1,69 @@
1
+ # IntegerHash
2
+
3
+ [![Build Status](https://travis-ci.org/icehero/integer_hash.svg?branch=master)](https://travis-ci.org/icehero/integer_hash)
4
+ [![Code Climate](https://codeclimate.com/github/icehero/integer_hash/badges/gpa.svg)](https://codeclimate.com/github/icehero/integer_hash)
5
+ [![Test Coverage](https://codeclimate.com/github/icehero/integer_hash/badges/coverage.svg)](https://codeclimate.com/github/icehero/integer_hash/coverage)
6
+
7
+ A small Ruby gem that implements Knuth's multiplication hashing algorithm with the following properties: fast, reversable and zero collisions.
8
+
9
+ This is a port from PHP of the [jenssegers/optimus](https://github.com/jenssegers/optimus) package.
10
+
11
+
12
+ ## Installation
13
+
14
+ Add this line to your application's Gemfile:
15
+
16
+ ```ruby
17
+ gem 'integer_hash'
18
+ ```
19
+
20
+ And then execute:
21
+
22
+ $ bundle
23
+
24
+ Or install it yourself as:
25
+
26
+ $ gem install integer_hash
27
+
28
+ ## Configure
29
+
30
+ Write the defaults taken from `IntegerHash::Generator.generate_configuration` in `config/initializers/integer_hash.rb`:
31
+
32
+ ```ruby
33
+ IntegerHash.configure do |config|
34
+ config.prime =
35
+ config.inverse_integer =
36
+ config.random_integer =
37
+ end
38
+ ```
39
+
40
+ ## Usage
41
+
42
+ ```ruby
43
+ $ IntegerHash.encode(42)
44
+ => 1591777047
45
+ $ IntegerHash.decode(1591777047)
46
+ => 42
47
+ ```
48
+
49
+ ## Alternatives
50
+
51
+ There are only two alternatives to this gem written in Ruby. ['hashids'](https://github.com/peterhellberg/hashids.rb) gem and ['obfuscate_id'](https://github.com/namick/obfuscate_id) gem which both offer the same main feature: encoding integers so that you don't expose database ids to the user.
52
+
53
+
54
+ ## Development
55
+
56
+ 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.
57
+
58
+ 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).
59
+
60
+
61
+ ## Contributing
62
+
63
+ Bug reports and pull requests are welcome on GitHub at https://github.com/icehero/integer_hash.
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
+
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,50 @@
1
+ begin
2
+ require 'bundler/inline'
3
+ rescue LoadError => e
4
+ $stderr.puts 'Bundler version 1.10 or later is required. Please update your Bundler'
5
+ raise e
6
+ end
7
+
8
+ gemfile(true) do
9
+ source 'https://rubygems.org'
10
+ gem 'rails', github: 'rails/rails'
11
+ gem 'arel', github: 'rails/arel'
12
+ gem 'rack', github: 'rack/rack'
13
+ gem 'sqlite3'
14
+ end
15
+
16
+ require 'active_record'
17
+ require 'minitest/autorun'
18
+ require 'logger'
19
+
20
+ # This connection will do for database-independent bug reports.
21
+ ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
22
+ ActiveRecord::Base.logger = Logger.new(STDOUT)
23
+
24
+ ActiveRecord::Schema.define do
25
+ create_table :posts, force: true do |t|
26
+ end
27
+
28
+ create_table :comments, force: true do |t|
29
+ t.integer :post_id
30
+ end
31
+ end
32
+
33
+ class Post < ActiveRecord::Base
34
+ has_many :comments
35
+ end
36
+
37
+ class Comment < ActiveRecord::Base
38
+ belongs_to :post
39
+ end
40
+
41
+ class BugTest < Minitest::Test
42
+ def test_association_stuff
43
+ post = Post.create!
44
+ post.comments << Comment.create!
45
+
46
+ assert_equal 1, post.comments.count
47
+ assert_equal 1, Comment.count
48
+ assert_equal post.id, Comment.first.post.id
49
+ end
50
+ end
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "integer_hash"
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
@@ -0,0 +1,5 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'integer_hash/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "integer_hash"
8
+ spec.version = IntegerHash::VERSION
9
+ spec.authors = ["Florin Popa"]
10
+ spec.email = ["pp.flrn@gmail.com"]
11
+
12
+ spec.summary = %q{Integer hashing based on Knuth's multiplicative hashing algorithm.}
13
+ spec.homepage = "https://github.com/icehero/integer_hash"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.10"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec"
24
+ spec.add_development_dependency "pry"
25
+
26
+ spec.add_runtime_dependency "rails", "~> 4.2"
27
+ end
@@ -0,0 +1,19 @@
1
+ require 'integer_hash/generator'
2
+
3
+ class IntegerHashGenerator < Rails::Generators::Base
4
+ def create_new_configuration
5
+ IntegerHash::Generator.generate_configuration
6
+ end
7
+
8
+ def create_initializer_file
9
+ configuration = create_new_configuration
10
+
11
+ create_file "config/initializers/integer_hash.rb", <<-FILE
12
+ IntegerHash.configure do |config|
13
+ config.prime = #{configuration.prime}
14
+ config.inverse_integer = #{configuration.inverse_integer}
15
+ config.random_integer = #{configuration.random_integer}
16
+ end
17
+ FILE
18
+ end
19
+ end
@@ -0,0 +1,31 @@
1
+ require "integer_hash/version"
2
+ require "integer_hash/configuration"
3
+ require "integer_hash/rails"
4
+
5
+ module IntegerHash
6
+ def self.encode(value)
7
+ ((value * prime) & max_integer) ^ random_integer
8
+ end
9
+
10
+ def self.decode(value)
11
+ ((value ^ random_integer) * inverse_integer) & max_integer
12
+ end
13
+
14
+ protected
15
+
16
+ def self.prime
17
+ configuration.prime
18
+ end
19
+
20
+ def self.inverse_integer
21
+ configuration.inverse_integer
22
+ end
23
+
24
+ def self.random_integer
25
+ configuration.random_integer
26
+ end
27
+
28
+ def self.max_integer
29
+ configuration.max_integer
30
+ end
31
+ end
@@ -0,0 +1,27 @@
1
+ module IntegerHash
2
+ class Configuration
3
+ attr_accessor :prime
4
+ attr_accessor :inverse_integer
5
+ attr_accessor :random_integer
6
+ attr_accessor :max_integer
7
+
8
+ def initialize values = {}
9
+ @prime = values[:prime]
10
+ @inverse_integer = values[:inverse_integer]
11
+ @random_integer = values[:random_integer]
12
+ @max_integer = 2**(values[:size] || 31)-1
13
+ end
14
+ end
15
+
16
+ def self.configuration
17
+ @configuration ||= Configuration.new
18
+ end
19
+
20
+ def self.configuration=(config)
21
+ @configuration = config
22
+ end
23
+
24
+ def self.configure
25
+ yield configuration
26
+ end
27
+ end
@@ -0,0 +1,34 @@
1
+ module IntegerHash
2
+ class Generator
3
+ def self.generate_configuration(size = 31, prime = nil)
4
+ return Configuration.new(Generator.new(size: size, prime: prime).generate_configuration)
5
+ end
6
+
7
+ def initialize(values = {})
8
+ @size = values[:size]
9
+ @prime = values[:prime]
10
+ #TODO BN requires it to be > 16 or the bites are too small
11
+ raise ArgumentError if @size < 16
12
+ end
13
+
14
+ def generate_configuration
15
+ @prime ||= generate_prime
16
+ inverse_integer = generate_inverse
17
+ random_integer = generate_random
18
+
19
+ {prime: @prime.to_i, inverse_integer: inverse_integer.to_i, random_integer: random_integer.to_i}
20
+ end
21
+
22
+ def generate_prime
23
+ OpenSSL::BN.generate_prime(@size)
24
+ end
25
+
26
+ def generate_inverse
27
+ OpenSSL::BN.new(@prime.to_s).mod_inverse(2**@size)
28
+ end
29
+
30
+ def generate_random
31
+ OpenSSL::BN.rand(@size)
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,62 @@
1
+ require 'integer_hash'
2
+ require 'active_record'
3
+
4
+ module IntegerHash
5
+ module Rails
6
+ def encode_id
7
+ require 'integer_hash'
8
+ extend ClassMethods
9
+ include InstanceMethods
10
+
11
+ cattr_accessor :integer_hash_configuration
12
+
13
+ end
14
+
15
+ module ClassMethods
16
+ def find(*args)
17
+ scope = args.slice!(0)
18
+ options = args.slice!(0) || {}
19
+ if has_encoded_id? && !options[:no_hashed_id]
20
+ if scope.is_a?(Array)
21
+ scope.map! {|a| IntegerHash.decode(a.to_i).to_s}
22
+ else
23
+ scope = IntegerHash.decode(scope.to_i).to_s
24
+ end
25
+ end
26
+ super(scope)
27
+ end
28
+
29
+ def has_encoded_id?
30
+ true
31
+ end
32
+ end
33
+
34
+ module InstanceMethods
35
+ def to_param
36
+ IntegerHash.encode(self.id).to_s
37
+ end
38
+
39
+ # Override ActiveRecord::Persistence#reload
40
+ # passing in an options flag with { no_hashed_id: true }
41
+ def reload(options = nil)
42
+ options = (options || {}).merge(no_hashed_id: true)
43
+
44
+ clear_aggregation_cache
45
+ clear_association_cache
46
+
47
+ fresh_object =
48
+ if options && options[:lock]
49
+ self.class.unscoped { self.class.lock(options[:lock]).find(id, options) }
50
+ else
51
+ self.class.unscoped { self.class.find(id, options) }
52
+ end
53
+
54
+ @attributes = fresh_object.instance_variable_get('@attributes')
55
+ @new_record = false
56
+ self
57
+ end
58
+ end
59
+ end
60
+ end
61
+
62
+ ActiveRecord::Base.extend IntegerHash::Rails
@@ -0,0 +1,3 @@
1
+ module IntegerHash
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,131 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: integer_hash
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Florin Popa
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-09-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
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: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rails
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '4.2'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '4.2'
83
+ description:
84
+ email:
85
+ - pp.flrn@gmail.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
+ - active_record_master.rb
98
+ - bin/console
99
+ - bin/setup
100
+ - integer_hash.gemspec
101
+ - lib/generators/integer_hash_generator.rb
102
+ - lib/integer_hash.rb
103
+ - lib/integer_hash/configuration.rb
104
+ - lib/integer_hash/generator.rb
105
+ - lib/integer_hash/rails.rb
106
+ - lib/integer_hash/version.rb
107
+ homepage: https://github.com/icehero/integer_hash
108
+ licenses:
109
+ - MIT
110
+ metadata: {}
111
+ post_install_message:
112
+ rdoc_options: []
113
+ require_paths:
114
+ - lib
115
+ required_ruby_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ required_rubygems_version: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ requirements: []
126
+ rubyforge_project:
127
+ rubygems_version: 2.4.5.1
128
+ signing_key:
129
+ specification_version: 4
130
+ summary: Integer hashing based on Knuth's multiplicative hashing algorithm.
131
+ test_files: []