has_uuid 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in has_uuid.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008 Norbert Crombach
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.
data/README.markdown ADDED
@@ -0,0 +1,60 @@
1
+ has_uuid
2
+ ========
3
+
4
+ [has_uuid](http://github.com/troessner/has_uuid) provides basic [UUID](http://en.wikipedia.org/wiki/Universally_Unique_Identifier) assignment methods for ActiveRecord objects.
5
+
6
+ It depends on the [uuidtools](http://uuidtools.rubyforge.org/) gem.
7
+
8
+ This is a fork - full credits for the source code go to [has_uuid](http://github.com/norbert/has_uuid).
9
+ What I changed:
10
+ a) Made it rails 3.1 compatible.
11
+ b) Bundler'ized it - you can now use and manage it as a gem, not as a rails plugin.
12
+
13
+ Installation
14
+ ------------
15
+
16
+ Add
17
+
18
+ >> gem 'has_uuid'
19
+
20
+ to your Gemfile and run
21
+
22
+ >> bundle install
23
+
24
+ Usage
25
+ -----
26
+
27
+ class Post < ActiveRecord::Base
28
+ # automatically assign a UUID to the "uuid" column on create
29
+ has_uuid
30
+ end
31
+
32
+ class Comment < ActiveRecord::Base
33
+ # skip assignment on create
34
+ has_uuid :auto => false
35
+ end
36
+
37
+ class User < ActiveRecord::Base
38
+ # store the UUID in the "token" column, generate version 1 UUIDs
39
+ has_uuid :column => :token, :generator => :timestamp
40
+ end
41
+
42
+ # assign a UUID if a valid one is not already present
43
+ @post.assign_uuid
44
+
45
+ # assign a UUID, replacing whatever is already there
46
+ @post.assign_uuid(:force => true)
47
+
48
+ # assign a UUID and save, replacing whatever is there
49
+ @post.assign_uuid!
50
+
51
+ # check if the current UUID is valid
52
+ @post.uuid_valid?
53
+
54
+
55
+ Credits
56
+ -------
57
+
58
+ * Norbert Crombach
59
+ * Todd Eichel
60
+ * Joerg Batterman
data/Rakefile ADDED
@@ -0,0 +1,23 @@
1
+ require 'rake/testtask'
2
+ require 'rake/rdoctask'
3
+
4
+ desc 'Default: run unit tests.'
5
+ task :default => :test
6
+
7
+ desc 'Test the has_uuid plugin.'
8
+ Rake::TestTask.new(:test) do |t|
9
+ t.libs << 'lib'
10
+ t.pattern = 'test/**/*_test.rb'
11
+ t.verbose = true
12
+ end
13
+
14
+ desc 'Generate documentation for the has_uuid plugin.'
15
+ Rake::RDocTask.new(:rdoc) do |rdoc|
16
+ rdoc.rdoc_dir = 'rdoc'
17
+ rdoc.title = 'has_uuid'
18
+ rdoc.options << '--line-numbers' << '--inline-source'
19
+ rdoc.rdoc_files.include('README')
20
+ rdoc.rdoc_files.include('lib/**/*.rb')
21
+ end
22
+
23
+ require 'bundler/gem_tasks'
data/has_uuid.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "has_uuid/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "has_uuid"
7
+ s.version = HasUuid::VERSION
8
+ s.authors = ["Timo Rößner"]
9
+ s.email = ["timo.roessner@googlemail.com"]
10
+ s.homepage = ""
11
+ s.summary = %q{The has_uuid gem adds a UUID to your AR models.}
12
+ s.description = %q{The has_uuid gem adds a UUID to your AR models. See the README for details.}
13
+
14
+ s.rubyforge_project = "has_uuid"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+ s.add_dependency 'activerecord'
21
+ s.add_dependency 'uuidtools'
22
+ s.add_development_dependency 'rake'
23
+ s.add_development_dependency 'sqlite3'
24
+ end
@@ -0,0 +1,3 @@
1
+ module HasUuid
2
+ VERSION = "0.0.2"
3
+ end
data/lib/has_uuid.rb ADDED
@@ -0,0 +1,71 @@
1
+ require 'uuidtools'
2
+
3
+ module ActiveRecord #:nodoc:
4
+ module Acts #:nodoc:
5
+ # Use this extension to assign a UUID to your models.
6
+ #
7
+ # Example:
8
+ #
9
+ # class Post < ActiveRecord::Base
10
+ # has_uuid
11
+ # end
12
+ #
13
+ # That is all.
14
+ module HasUuid
15
+ GENERATORS = [:random, :timestamp] #:nodoc:
16
+
17
+ # Configuration options are:
18
+ #
19
+ # * <tt>:column</tt> - specifies the column in which to store the UUID (default: +uuid+).
20
+ # * <tt>:auto</tt> - specifies whether the plugin should assign a UUID on create (default: true).
21
+ # * <tt>:generator</tt> - sets the UUID generator. Possible values are <tt>:random</tt> for version 4 (default) and <tt>:timestamp</tt> for version 1.
22
+ def has_uuid(options = {})
23
+ options.reverse_merge!(:auto => true, :generator => :random, :column => :uuid)
24
+ raise ArgumentError, "invalid UUID generator" unless GENERATORS.include?(options[:generator])
25
+
26
+ class_eval do
27
+ send :include, InstanceMethods # hide include from RDoc
28
+
29
+ if options[:auto]
30
+ # always applies to subclasses
31
+ before_validation(:on => :create) { assign_uuid }
32
+ end
33
+
34
+ class_inheritable_reader :uuid_column
35
+ write_inheritable_attribute :uuid_column, options[:column]
36
+
37
+ class_inheritable_reader :uuid_generator
38
+ write_inheritable_attribute :uuid_generator, options[:generator]
39
+ end
40
+ end
41
+
42
+ module InstanceMethods #:nodoc:
43
+ def assign_uuid(options = {})
44
+ return if uuid_valid? unless options[:force]
45
+
46
+ uuid = UUIDTools::UUID.send("#{uuid_generator}_create").to_s
47
+ send("#{uuid_column}=", uuid)
48
+ end
49
+
50
+ def assign_uuid!
51
+ assign_uuid(:force => true)
52
+ save!
53
+ end
54
+
55
+ def uuid_valid?
56
+ uuid = send(uuid_column)
57
+ return false if uuid.blank?
58
+ begin
59
+ UUIDTools::UUID.parse(uuid).kind_of?(UUIDTools::UUID)
60
+ rescue ArgumentError, TypeError
61
+ false
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
68
+
69
+ ActiveRecord::Base.class_eval do
70
+ extend ActiveRecord::Acts::HasUuid
71
+ end
data/rails/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'has_uuid'
@@ -0,0 +1,83 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class Widget < ActiveRecord::Base
4
+ has_uuid
5
+ end
6
+
7
+ class Thingy < ActiveRecord::Base
8
+ has_uuid :auto => false
9
+ end
10
+
11
+ class HasUuidTest < Test::Unit::TestCase
12
+ def test_should_assign_uuid
13
+ @widget = Widget.new
14
+ @widget.assign_uuid
15
+ assert @widget.new_record?
16
+ assert @widget.uuid_valid?
17
+ end
18
+
19
+ def test_should_force_assign_uuid
20
+ original_uuid = UUIDTools::UUID.random_create.to_s
21
+ @widget = Widget.new(:uuid => original_uuid)
22
+ @widget.assign_uuid(:force => true)
23
+ assert_not_equal original_uuid, @widget.uuid
24
+ end
25
+
26
+ def test_should_force_assign_uuid_and_save
27
+ @widget = Widget.new
28
+ @widget.assign_uuid!
29
+ assert !@widget.new_record?
30
+ assert @widget.uuid_valid?
31
+ end
32
+
33
+ def test_should_assign_uuid_on_create
34
+ @widget = Widget.new
35
+ assert_nil @widget.uuid
36
+ @widget.save
37
+ @widget.reload
38
+ assert @widget.uuid_valid?
39
+ end
40
+
41
+ def test_should_check_uuid_validity
42
+ @widget = Widget.new
43
+ assert !@widget.uuid_valid?
44
+ @widget.uuid = "test"
45
+ assert !@widget.uuid_valid?
46
+ @widget.uuid = UUIDTools::UUID.random_create.to_s
47
+ assert @widget.uuid_valid?
48
+ end
49
+
50
+ def test_should_not_overwrite_valid_uuid
51
+ manual_uuid = UUIDTools::UUID.random_create.to_s
52
+ @widget = Widget.new(:uuid => manual_uuid)
53
+ @widget.save
54
+ assert_equal manual_uuid, @widget.uuid
55
+ end
56
+
57
+ def test_should_assign_uuid_when_nil
58
+ @widget = Widget.new
59
+ @widget.save
60
+ assert @widget.uuid_valid?
61
+ end
62
+
63
+ def test_should_assign_uuid_when_blank
64
+ @widget = Widget.new(:uuid => "")
65
+ @widget.save
66
+ assert @widget.uuid_valid?
67
+ end
68
+
69
+ def test_should_assign_uuid_when_invalid
70
+ @widget = Widget.new(:uuid => "test")
71
+ assert !@widget.uuid_valid?
72
+ @widget.save
73
+ assert @widget.uuid_valid?
74
+ end
75
+
76
+ def test_should_not_assign_uuid_on_create_for_thingies
77
+ # such a weird name for a class
78
+ @thingy = Thingy.new
79
+ @thingy.save
80
+ @thingy.reload
81
+ assert_nil @thingy.uuid
82
+ end
83
+ end
@@ -0,0 +1,18 @@
1
+ require 'test/unit'
2
+
3
+ require 'active_record'
4
+ ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
5
+
6
+ require File.dirname(__FILE__) + '/../lib/has_uuid'
7
+
8
+ silence_stream(STDOUT) do
9
+ ActiveRecord::Schema.define do
10
+ create_table :widgets do |t|
11
+ t.string :uuid
12
+ end
13
+
14
+ create_table :thingies do |t|
15
+ t.string :uuid
16
+ end
17
+ end
18
+ end
metadata ADDED
@@ -0,0 +1,133 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: has_uuid
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 2
10
+ version: 0.0.2
11
+ platform: ruby
12
+ authors:
13
+ - !binary |
14
+ VGltbyBSw7bDn25lcg==
15
+
16
+ autorequire:
17
+ bindir: bin
18
+ cert_chain: []
19
+
20
+ date: 2011-07-31 00:00:00 Z
21
+ dependencies:
22
+ - !ruby/object:Gem::Dependency
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 3
29
+ segments:
30
+ - 0
31
+ version: "0"
32
+ name: activerecord
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *id001
36
+ - !ruby/object:Gem::Dependency
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ hash: 3
43
+ segments:
44
+ - 0
45
+ version: "0"
46
+ name: uuidtools
47
+ type: :runtime
48
+ prerelease: false
49
+ version_requirements: *id002
50
+ - !ruby/object:Gem::Dependency
51
+ requirement: &id003 !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ hash: 3
57
+ segments:
58
+ - 0
59
+ version: "0"
60
+ name: rake
61
+ type: :development
62
+ prerelease: false
63
+ version_requirements: *id003
64
+ - !ruby/object:Gem::Dependency
65
+ requirement: &id004 !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ hash: 3
71
+ segments:
72
+ - 0
73
+ version: "0"
74
+ name: sqlite3
75
+ type: :development
76
+ prerelease: false
77
+ version_requirements: *id004
78
+ description: The has_uuid gem adds a UUID to your AR models. See the README for details.
79
+ email:
80
+ - timo.roessner@googlemail.com
81
+ executables: []
82
+
83
+ extensions: []
84
+
85
+ extra_rdoc_files: []
86
+
87
+ files:
88
+ - .gitignore
89
+ - Gemfile
90
+ - LICENSE
91
+ - README.markdown
92
+ - Rakefile
93
+ - has_uuid.gemspec
94
+ - lib/has_uuid.rb
95
+ - lib/has_uuid/version.rb
96
+ - rails/init.rb
97
+ - test/has_uuid_test.rb
98
+ - test/test_helper.rb
99
+ homepage: ""
100
+ licenses: []
101
+
102
+ post_install_message:
103
+ rdoc_options: []
104
+
105
+ require_paths:
106
+ - lib
107
+ required_ruby_version: !ruby/object:Gem::Requirement
108
+ none: false
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ hash: 3
113
+ segments:
114
+ - 0
115
+ version: "0"
116
+ required_rubygems_version: !ruby/object:Gem::Requirement
117
+ none: false
118
+ requirements:
119
+ - - ">="
120
+ - !ruby/object:Gem::Version
121
+ hash: 3
122
+ segments:
123
+ - 0
124
+ version: "0"
125
+ requirements: []
126
+
127
+ rubyforge_project: has_uuid
128
+ rubygems_version: 1.7.2
129
+ signing_key:
130
+ specification_version: 3
131
+ summary: The has_uuid gem adds a UUID to your AR models.
132
+ test_files: []
133
+