acts_as_hashed 0.0.1

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,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm --create 1.9.3-p125@acts_as_hashed
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in acts_as_hashed.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Rodrigo Pinto
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,53 @@
1
+ # ActsAsHashed
2
+
3
+ ActsAsHashed is helpful to set a hash_code column based on SecureRandom.hex(16).
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'acts_as_hashed'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install acts_as_hashed
18
+
19
+ ## Usage
20
+
21
+ #### Run your migrations for the desired models
22
+
23
+ ```ruby
24
+ class AddHashedCodeToSeller < ActiveRecord::Migration
25
+ def self.up
26
+ add_column :sellers, :hashed_code, :string
27
+ end
28
+
29
+ def self.down
30
+ remove_column :sellers, :hashed_code, :string
31
+ end
32
+ end
33
+ ```
34
+
35
+ ### Usage
36
+
37
+ #### In your model:
38
+
39
+ ```ruby
40
+ class Seller < ActiveRecord::Base
41
+ acts_as_hashed
42
+
43
+ ...
44
+ end
45
+ ```
46
+
47
+ ## Contributing
48
+
49
+ 1. Fork it
50
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
51
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
52
+ 4. Push to the branch (`git push origin my-new-feature`)
53
+ 5. Create new Pull Request
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/acts_as_hashed/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Rodrigo Pinto"]
6
+ gem.email = ["rodrigopqn@gmail.com"]
7
+ gem.description = %q{ActsAsHashed is helpful to set a hash_code column based on SecureRandom.hex(16).}
8
+ gem.summary = %q{SecureRandom hex generator for hashed_code column.}
9
+ gem.homepage = ""
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "acts_as_hashed"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = ActsAsHashed::VERSION
17
+
18
+ gem.add_development_dependency "rspec", '2.11.0'
19
+ gem.add_development_dependency "sqlite3"
20
+
21
+ gem.add_dependency "activerecord", '>= 3.0.0'
22
+ end
@@ -0,0 +1,35 @@
1
+ require "acts_as_hashed/version"
2
+
3
+ module ActsAsHashed
4
+ def acts_as_hashed
5
+ before_create :set_hashed_code
6
+ include InstanceMethods
7
+ end
8
+
9
+ module InstanceMethods
10
+ def self.included(base)
11
+ base.extend ClassMethods
12
+ end
13
+
14
+ module ClassMethods
15
+ def friendly_token
16
+ SecureRandom.hex(16)
17
+ end
18
+
19
+ def hashed_code_exists?(hashed_code)
20
+ !where(:hashed_code => hashed_code).count.zero?
21
+ end
22
+ end
23
+
24
+ def set_hashed_code
25
+ return unless hashed_code.nil?
26
+ loop do
27
+ self.hashed_code = self.class.friendly_token
28
+ break self.hashed_code unless self.class.hashed_code_exists?(self.hashed_code)
29
+ end
30
+ end
31
+
32
+ end
33
+ end
34
+
35
+ ActiveRecord::Base.extend ActsAsHashed
@@ -0,0 +1,3 @@
1
+ module ActsAsHashed
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,43 @@
1
+ require 'spec_helper'
2
+ require 'active_record'
3
+ require File.expand_path(File.dirname(__FILE__) + "/../lib/acts_as_hashed")
4
+
5
+ DB_FILE = 'tmp/test_db'
6
+
7
+ FileUtils.mkdir_p File.dirname(DB_FILE)
8
+ FileUtils.rm_f DB_FILE
9
+
10
+ ActiveRecord::Base.establish_connection :adapter => 'sqlite3', :database => DB_FILE
11
+ ActiveRecord::Base.connection.execute 'CREATE TABLE some_models (id INTEGER NOT NULL PRIMARY KEY, hashed_code string)'
12
+
13
+ class SomeModel < ActiveRecord::Base
14
+ acts_as_hashed
15
+ end
16
+
17
+ describe ActsAsHashed do
18
+ before(:each) do
19
+ clean_database!
20
+ end
21
+ context "new record" do
22
+ let(:model) { SomeModel.new }
23
+ it "should save hashed code" do
24
+ SomeModel.stub(:friendly_token).and_return('new-hashed-code-here')
25
+ expect {
26
+ model.save!
27
+ }.to change(model, :hashed_code).from(nil).to('new-hashed-code-here')
28
+ end
29
+ end
30
+ describe "persisted record" do
31
+ let(:model) do
32
+ model = SomeModel.new
33
+ model.save!
34
+ model
35
+ end
36
+ it "should save hashed code" do
37
+ SomeModel.stub(:friendly_token).and_return('new-hashed-code-here')
38
+ expect {
39
+ model.save!
40
+ }.to_not change(model, :hashed_code)
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,21 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+ RSpec.configure do |config|
8
+ config.treat_symbols_as_metadata_keys_with_true_values = true
9
+ config.run_all_when_everything_filtered = true
10
+ config.filter_run :focus
11
+
12
+ # Run specs in random order to surface order dependencies. If you find an
13
+ # order dependency and want to debug it, you can fix the order by providing
14
+ # the seed, which is printed after each run.
15
+ # --seed 1234
16
+ config.order = 'random'
17
+ end
18
+
19
+ def clean_database!
20
+ ActiveRecord::Base.connection.execute "DELETE FROM #{SomeModel.table_name}"
21
+ end
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: acts_as_hashed
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Rodrigo Pinto
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-07-14 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &2160500900 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - =
20
+ - !ruby/object:Gem::Version
21
+ version: 2.11.0
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *2160500900
25
+ - !ruby/object:Gem::Dependency
26
+ name: sqlite3
27
+ requirement: &2160500460 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *2160500460
36
+ - !ruby/object:Gem::Dependency
37
+ name: activerecord
38
+ requirement: &2160499900 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: 3.0.0
44
+ type: :runtime
45
+ prerelease: false
46
+ version_requirements: *2160499900
47
+ description: ActsAsHashed is helpful to set a hash_code column based on SecureRandom.hex(16).
48
+ email:
49
+ - rodrigopqn@gmail.com
50
+ executables: []
51
+ extensions: []
52
+ extra_rdoc_files: []
53
+ files:
54
+ - .gitignore
55
+ - .rspec
56
+ - .rvmrc
57
+ - Gemfile
58
+ - LICENSE
59
+ - README.md
60
+ - Rakefile
61
+ - acts_as_hashed.gemspec
62
+ - lib/acts_as_hashed.rb
63
+ - lib/acts_as_hashed/version.rb
64
+ - spec/acts_as_hashed_spec.rb
65
+ - spec/spec_helper.rb
66
+ homepage: ''
67
+ licenses: []
68
+ post_install_message:
69
+ rdoc_options: []
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ none: false
80
+ requirements:
81
+ - - ! '>='
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ requirements: []
85
+ rubyforge_project:
86
+ rubygems_version: 1.8.15
87
+ signing_key:
88
+ specification_version: 3
89
+ summary: SecureRandom hex generator for hashed_code column.
90
+ test_files:
91
+ - spec/acts_as_hashed_spec.rb
92
+ - spec/spec_helper.rb