milk1000cc-acts_as_digested_on 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,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
@@ -0,0 +1,5 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 milk1000cc
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,72 @@
1
+ = ActsAsDigestedOn
2
+
3
+ acts_as_digested_on is a rails plugin.
4
+
5
+ This sets the digested value before validation and validates uniqueness of the digested value.
6
+
7
+ == Install
8
+
9
+ sudo gem install milk1000cc-acts_as_digested_on --source=http://gems.github.com
10
+
11
+ or
12
+
13
+ script/plugin install git://github.com/milk1000cc/acts_as_digested_on.git
14
+
15
+ == Usage
16
+
17
+ # db/migrate/20090706000000_create_articles.rb
18
+ class CreateArticles < ActiveRecord::Migration
19
+ def self.up
20
+ create_table :articles do |t|
21
+ t.text :url # any type is ok
22
+ t.string :digest # this is sha-1 hex digest field of url value
23
+ ...
24
+ end
25
+
26
+ add_index :articles, :digest, :unique => true # you can add index on digest field
27
+ end
28
+
29
+ def self.down
30
+ drop_table :articles
31
+ end
32
+ end
33
+
34
+ # app/models/article.rb
35
+ class Article < ActiveRecord::Base
36
+ acts_as_digested_on :url # please add this
37
+ ...
38
+ end
39
+
40
+ # this means
41
+ class Article < ActiveRecord::Base
42
+ validates_uniqueness_of :digest
43
+ before_validation :set_digest
44
+
45
+ def generate_digest
46
+ Digest::SHA1.hexdigest "--#{ url.to_s }--"
47
+ end
48
+
49
+ private
50
+ def set_digest
51
+ self.digest = generate_digest
52
+ end
53
+ ...
54
+ end
55
+
56
+ == Examples
57
+
58
+ acts_as_digested_on :url, :unique => false
59
+
60
+ acts_as_digested_on :url, :digest_column => :url_digest
61
+
62
+ acts_as_digested_on [:url, :salt]
63
+
64
+ acts_as_digested_on :url, :scope => :site_id
65
+
66
+ Article.new(:url => 'http://example.com').generate_digest
67
+
68
+ == Copyright
69
+
70
+ Copyright (c) 2009 milk1000cc, released under the MIT license
71
+
72
+ milk1000cc <mailto:info@milk1000.cc>
@@ -0,0 +1,49 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "acts_as_digested_on"
8
+ gem.description = 'A rails plugin to set the digested value before validation and validate uniqueness.'
9
+ gem.summary = %Q{automatically set sha-1 hex digest}
10
+ gem.email = "info@milk1000.cc"
11
+ gem.homepage = "http://github.com/milk1000cc/acts_as_digested_on"
12
+ gem.authors = ["milk1000cc"]
13
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
14
+ end
15
+
16
+ rescue LoadError
17
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
18
+ end
19
+
20
+ require 'spec/rake/spectask'
21
+ Spec::Rake::SpecTask.new(:spec) do |spec|
22
+ spec.libs << 'lib' << 'spec'
23
+ spec.spec_files = FileList['spec/**/*_spec.rb']
24
+ end
25
+
26
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
27
+ spec.libs << 'lib' << 'spec'
28
+ spec.pattern = 'spec/**/*_spec.rb'
29
+ spec.rcov = true
30
+ end
31
+
32
+
33
+ task :default => :spec
34
+
35
+ require 'rake/rdoctask'
36
+ Rake::RDocTask.new do |rdoc|
37
+ if File.exist?('VERSION.yml')
38
+ config = YAML.load(File.read('VERSION.yml'))
39
+ version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
40
+ else
41
+ version = ""
42
+ end
43
+
44
+ rdoc.rdoc_dir = 'rdoc'
45
+ rdoc.title = "acts_as_digested_on #{version}"
46
+ rdoc.rdoc_files.include('README*')
47
+ rdoc.rdoc_files.include('lib/**/*.rb')
48
+ end
49
+
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
@@ -0,0 +1,49 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{acts_as_digested_on}
5
+ s.version = "0.0.1"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["milk1000cc"]
9
+ s.date = %q{2009-07-07}
10
+ s.description = %q{A rails plugin to set the digested value before validation and validate uniqueness.}
11
+ s.email = %q{info@milk1000.cc}
12
+ s.extra_rdoc_files = [
13
+ "LICENSE",
14
+ "README.rdoc"
15
+ ]
16
+ s.files = [
17
+ ".document",
18
+ ".gitignore",
19
+ "LICENSE",
20
+ "README.rdoc",
21
+ "Rakefile",
22
+ "VERSION",
23
+ "acts_as_digested_on.gemspec",
24
+ "init.rb",
25
+ "lib/acts_as_digested_on.rb",
26
+ "spec/acts_as_digested_on_spec.rb",
27
+ "spec/spec_helper.rb"
28
+ ]
29
+ s.has_rdoc = true
30
+ s.homepage = %q{http://github.com/milk1000cc/acts_as_digested_on}
31
+ s.rdoc_options = ["--charset=UTF-8"]
32
+ s.require_paths = ["lib"]
33
+ s.rubygems_version = %q{1.3.1}
34
+ s.summary = %q{automatically set sha-1 hex digest}
35
+ s.test_files = [
36
+ "spec/spec_helper.rb",
37
+ "spec/acts_as_digested_on_spec.rb"
38
+ ]
39
+
40
+ if s.respond_to? :specification_version then
41
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
42
+ s.specification_version = 2
43
+
44
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
45
+ else
46
+ end
47
+ else
48
+ end
49
+ end
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'acts_as_digested_on'
@@ -0,0 +1,62 @@
1
+ require 'digest/sha1'
2
+
3
+ module ActsAsDigestedOn
4
+ def self.included(model)
5
+ model.extend ClassMethods
6
+ end
7
+
8
+ module Validations
9
+ def self.included(model)
10
+ model.class_eval do
11
+ options = acts_as_digested_on_vars[:validates_uniqueness_of_options]
12
+ validates_uniqueness_of acts_as_digested_on_vars[:digest_column], options
13
+ end
14
+ end
15
+ end
16
+
17
+ module Callbacks
18
+ def self.included(model)
19
+ model.class_eval do
20
+ before_validation :set_digest
21
+ end
22
+ end
23
+ end
24
+
25
+ module InstanceMethods
26
+ def generate_digest
27
+ original_columns = self.class.acts_as_digested_on_vars[:original_columns]
28
+ original_string = "--#{ original_columns.map { |v| self[v].to_s }.join('--') }--"
29
+ Digest::SHA1.hexdigest original_string
30
+ end
31
+
32
+ private
33
+ def set_digest
34
+ digest_column = self.class.acts_as_digested_on_vars[:digest_column]
35
+ self[digest_column] = generate_digest
36
+ end
37
+ end
38
+
39
+ module ClassMethods
40
+ def acts_as_digested_on(original_columns, options = {})
41
+ options = options.symbolize_keys
42
+
43
+ original_columns = Array(original_columns).flatten
44
+ digest_column = options.delete(:digest_column) || 'digest'
45
+ unique = options.key?(:unique) ? options.delete(:unique) : true
46
+
47
+ class_inheritable_hash :acts_as_digested_on_vars
48
+ self.acts_as_digested_on_vars = {
49
+ :original_columns => original_columns,
50
+ :digest_column => digest_column,
51
+ :unique => unique,
52
+ :validates_uniqueness_of_options => options
53
+ }
54
+
55
+ include InstanceMethods
56
+ include Callbacks
57
+ include Validations if self.acts_as_digested_on_vars[:unique]
58
+ end
59
+ end
60
+ end
61
+
62
+ ActiveRecord::Base.send :include, ActsAsDigestedOn
@@ -0,0 +1,107 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ class Article < ActiveRecord::Base
4
+ define_table do |t|
5
+ t.integer :user_id
6
+ t.string :title
7
+ t.text :url
8
+ t.string :digest
9
+ t.string :my_digest
10
+ t.text :content
11
+ end
12
+ end
13
+
14
+ describe "ActsAsDigestedOn" do
15
+ before do
16
+ (ActiveRecord::Callbacks::CALLBACKS + [:validate, :validate_on_create, :validate_on_update]).each do |cb|
17
+ Article.instance_variable_set "@#{ cb }_callbacks", nil
18
+ end
19
+ Article.destroy_all
20
+ end
21
+
22
+ before do
23
+ @url = 'http://example.com'
24
+ end
25
+
26
+ describe '#generate_digest' do
27
+ it 'should generate sha-1 digest of target field with some string' do
28
+ Article.acts_as_digested_on :url
29
+
30
+ article = Article.new(:url => @url)
31
+ article.generate_digest.should == Digest::SHA1.hexdigest("--#{ @url }--")
32
+ end
33
+
34
+ describe 'when more than one target field is given' do
35
+ it 'should generate sha-1 digest of target fields with some string' do
36
+ Article.acts_as_digested_on [:title, :url]
37
+
38
+ title = 'an article'
39
+ article = Article.new(:title => title, :url => @url)
40
+ article.generate_digest.should == Digest::SHA1.hexdigest("--#{ title }--#{ @url }--")
41
+ end
42
+ end
43
+ end
44
+
45
+ describe 'before validation' do
46
+ it 'should set "digest" field' do
47
+ Article.acts_as_digested_on :url
48
+
49
+ article = Article.new(:url => @url)
50
+ article.digest.should be_nil
51
+ article.valid?
52
+ article.digest.should == article.generate_digest
53
+ end
54
+
55
+ it 'should set the digest value to :digest_column field when :digest_column option is set' do
56
+ Article.acts_as_digested_on :url, :digest_column => :my_digest
57
+
58
+ article = Article.new(:url => @url)
59
+ article.digest.should be_nil
60
+ article.my_digest.should be_nil
61
+ article.valid?
62
+ article.digest.should be_nil
63
+ article.my_digest.should == article.generate_digest
64
+ end
65
+ end
66
+
67
+ describe 'when validation' do
68
+ it 'should validate uniqueness of digest field by default' do
69
+ Article.acts_as_digested_on :url
70
+
71
+ Article.create! :url => @url
72
+ proc { Article.create!(:url => @url) }.should raise_error(ActiveRecord::RecordInvalid)
73
+ proc { Article.create!(:url => 'http://example2.com') }.should_not raise_error
74
+ end
75
+
76
+ it 'should carry over the options to the validates_uniqueness_of options' do
77
+ Article.acts_as_digested_on :url, :scope => :user_id
78
+
79
+ Article.create! :url => @url, :user_id => 1
80
+ proc { Article.create!(:url => @url, :user_id => 1) }.should raise_error(ActiveRecord::RecordInvalid)
81
+ proc { Article.create!(:url => @url, :user_id => 2) }.should_not raise_error
82
+ proc { Article.create!(:url => 'http://example2.com', :user_id => 1) }.should_not raise_error
83
+ end
84
+
85
+ it 'should validate uniqueness of digest field when :unique option is true' do
86
+ Article.acts_as_digested_on :url, :unique => true
87
+
88
+ Article.create! :url => @url
89
+ proc { Article.create!(:url => @url) }.should raise_error(ActiveRecord::RecordInvalid)
90
+ proc { Article.create!(:url => 'http://example2.com') }.should_not raise_error
91
+ end
92
+
93
+ it 'should not validate uniqueness of digest field when :unique option is false' do
94
+ Article.acts_as_digested_on :url, :unique => false
95
+
96
+ Article.create! :url => @url
97
+ proc { Article.create!(:url => @url) }.should_not raise_error
98
+ end
99
+
100
+ it 'should support string key options' do
101
+ Article.acts_as_digested_on :url, 'unique' => false
102
+
103
+ Article.create! :url => @url
104
+ proc { Article.create!(:url => @url) }.should_not raise_error
105
+ end
106
+ end
107
+ end
@@ -0,0 +1,21 @@
1
+ require 'rubygems'
2
+ require 'spec'
3
+ require 'active_record'
4
+
5
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
6
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
7
+ require 'acts_as_digested_on'
8
+
9
+ Spec::Runner.configure do |config|
10
+
11
+ end
12
+
13
+ ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => ':memory:')
14
+
15
+ class ActiveRecord::Base
16
+ def self.define_table(table_name = self.name.tableize, &migration)
17
+ ActiveRecord::Schema.define(:version => 1) do
18
+ create_table(table_name, &migration)
19
+ end
20
+ end
21
+ end
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: milk1000cc-acts_as_digested_on
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - milk1000cc
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-07-07 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: A rails plugin to set the digested value before validation and validate uniqueness.
17
+ email: info@milk1000.cc
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - LICENSE
24
+ - README.rdoc
25
+ files:
26
+ - .document
27
+ - .gitignore
28
+ - LICENSE
29
+ - README.rdoc
30
+ - Rakefile
31
+ - VERSION
32
+ - acts_as_digested_on.gemspec
33
+ - init.rb
34
+ - lib/acts_as_digested_on.rb
35
+ - spec/acts_as_digested_on_spec.rb
36
+ - spec/spec_helper.rb
37
+ has_rdoc: true
38
+ homepage: http://github.com/milk1000cc/acts_as_digested_on
39
+ post_install_message:
40
+ rdoc_options:
41
+ - --charset=UTF-8
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: "0"
49
+ version:
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: "0"
55
+ version:
56
+ requirements: []
57
+
58
+ rubyforge_project:
59
+ rubygems_version: 1.2.0
60
+ signing_key:
61
+ specification_version: 2
62
+ summary: automatically set sha-1 hex digest
63
+ test_files:
64
+ - spec/spec_helper.rb
65
+ - spec/acts_as_digested_on_spec.rb