vidibus-uuid 0.3.5

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Andre Pankratz
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.rdoc ADDED
@@ -0,0 +1,70 @@
1
+ = vidibus-uuid
2
+
3
+ This gem is part of the open source SOA framework Vidibus: http://www.vidibus.org
4
+
5
+ It generates compact UUIDs. Basically, this is an abstraction of http://github.com/assaf/uuid
6
+
7
+
8
+ == Installation
9
+
10
+ Add the dependency to the Gemfile of your application:
11
+
12
+ gem "vidibus-uuid"
13
+
14
+ Then call bundle install on your console.
15
+
16
+
17
+ == Usage
18
+
19
+ Vidibus::Uuid.generate
20
+ # => "b063263064e0012d47b658b035f038ab"
21
+
22
+
23
+ === Usage in Mongoid model
24
+
25
+ Include the Vidibus::Uuid::Mongoid module in your Mongoid model:
26
+
27
+ class MyModel
28
+ include Mongoid::Document
29
+ include Vidibus::Uuid::Mongoid
30
+ end
31
+
32
+ This will extend your model as follows:
33
+
34
+ class MyModel
35
+ include Mongoid::Document
36
+
37
+ field :uuid
38
+ index :uuid, :unique => true
39
+
40
+ before_validation :generate_uuid
41
+ validates :uuid, :uniqueness => true, :uuid => true
42
+
43
+ private
44
+
45
+ def generate_uuid
46
+ return unless new_record?
47
+ self.uuid ||= Vidibus::Uuid.generate
48
+ end
49
+ end
50
+
51
+
52
+ === Validation of custom attributes
53
+
54
+ To verfify that custom attributes are formatted as UUID, add the uuid validator to your model:
55
+
56
+ class MyModel
57
+ include Mongoid::Document
58
+ field :product_uuid
59
+ validates :product_uuid, :uuid => { :allow_blank => true }
60
+ end
61
+
62
+
63
+ == Copyright
64
+
65
+ Copyright (c) 2010 Andre Pankratz. See LICENSE for details.
66
+
67
+
68
+ == Thank you!
69
+
70
+ The development of this gem was sponsored by Käuferportal: http://www.kaeuferportal.de
data/Rakefile ADDED
@@ -0,0 +1,46 @@
1
+ require "rubygems"
2
+ require "rake"
3
+
4
+ begin
5
+ require "jeweler"
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "vidibus-uuid"
8
+ gem.summary = %Q{Provides UUID generation through UUID gem.}
9
+ gem.description = %Q{Provides UUID generation for Mongoid models. It includes a validator for UUIDs.}
10
+ gem.email = "andre@vidibus.com"
11
+ gem.homepage = "http://github.com/vidibus/vidibus-uuid"
12
+ gem.authors = ["Andre Pankratz"]
13
+ gem.add_development_dependency "rspec"
14
+ gem.add_development_dependency "mongoid", "= 2.0.0.beta.15"
15
+ gem.add_dependency "uuid"
16
+ end
17
+ Jeweler::GemcutterTasks.new
18
+ rescue LoadError
19
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
20
+ end
21
+
22
+ require "spec/rake/spectask"
23
+ Spec::Rake::SpecTask.new(:spec) do |spec|
24
+ spec.libs << "lib" << "spec"
25
+ spec.spec_files = FileList["spec/**/*_spec.rb"]
26
+ end
27
+
28
+ Spec::Rake::SpecTask.new(:rcov) do |t|
29
+ t.spec_files = FileList['spec/vidibus/**/*_spec.rb']
30
+ t.rcov = true
31
+ t.rcov_opts = ['--exclude', '^spec,/gems/']
32
+ end
33
+
34
+ task :spec => :check_dependencies
35
+ task :default => :spec
36
+
37
+ require "rake/rdoctask"
38
+ Rake::RDocTask.new do |rdoc|
39
+ version = File.exist?("VERSION") ? File.read("VERSION") : ""
40
+ rdoc.rdoc_dir = "rdoc"
41
+ rdoc.title = "vidibus-uuid #{version}"
42
+ rdoc.rdoc_files.include("README*")
43
+ rdoc.rdoc_files.include("lib/**/*.rb")
44
+ rdoc.options << "--charset=utf-8"
45
+ end
46
+
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.3.5
@@ -0,0 +1,5 @@
1
+ en:
2
+ activemodel:
3
+ errors:
4
+ messages:
5
+ invalid_uuid: "is not a valid UUID"
@@ -0,0 +1,6 @@
1
+ require "vidibus/uuid"
2
+ require "vidibus/validate_uuid"
3
+ require "vidibus/uuid/mongoid"
4
+
5
+ # Add UUID validator
6
+ ActiveModel::Validations.send(:include, Vidibus::ValidateUuid)
@@ -0,0 +1,9 @@
1
+ require "uuid"
2
+
3
+ module Vidibus
4
+ module Uuid
5
+ def self.generate
6
+ UUID.new.generate(:compact)
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,25 @@
1
+ module Vidibus
2
+ module Uuid
3
+ module Mongoid
4
+ extend ActiveSupport::Concern
5
+ included do
6
+ field :uuid
7
+ index :uuid, :unique => true
8
+ before_validation :generate_uuid
9
+ validates :uuid, :uniqueness => true, :uuid => true
10
+ end
11
+
12
+ # Returns UUID as param for urls.
13
+ def to_param
14
+ uuid
15
+ end
16
+
17
+ private
18
+
19
+ # Sets unique UUID unless uuid is present.
20
+ def generate_uuid
21
+ self.uuid ||= Vidibus::Uuid.generate
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,13 @@
1
+ module Vidibus
2
+ module ValidateUuid
3
+ class UuidValidator < ActiveModel::EachValidator
4
+
5
+ # Ensures that every value is of a valid compact UUID format
6
+ def validate_each(record, attribute, value)
7
+ unless value.to_s.match(/^[a-z0-9]{32}$/)
8
+ record.errors.add(attribute, :invalid_uuid)
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1,2 @@
1
+ --colour
2
+ --format nested
@@ -0,0 +1,24 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), "..", "lib"))
3
+
4
+ require "rubygems"
5
+ require "active_support/core_ext"
6
+ require "spec"
7
+ require "mongoid"
8
+ require "uuid"
9
+ require "vidibus-uuid"
10
+
11
+ Mongoid.configure do |config|
12
+ name = "vidibus-uuid_test"
13
+ host = "localhost"
14
+ config.master = Mongo::Connection.new.db(name)
15
+ end
16
+
17
+ Spec::Runner.configure do |config|
18
+ config.mock_with :rspec
19
+ config.before(:each) do
20
+ Mongoid.master.collections.select { |c| c.name != 'system.indexes' }.each(&:drop)
21
+ end
22
+ end
23
+
24
+ I18n.load_path += Dir[File.join('config', 'locale', '**', '*.{rb,yml}')]
@@ -0,0 +1,68 @@
1
+ require "spec_helper"
2
+
3
+ class Model
4
+ include Mongoid::Document
5
+ include Vidibus::Uuid::Mongoid
6
+ embeds_many :children
7
+ end
8
+
9
+ class Child
10
+ include Mongoid::Document
11
+ include Vidibus::Uuid::Mongoid
12
+ embedded_in :model, :inverse_of => :children
13
+ end
14
+
15
+ describe "Vidibus::Uuid::Mongoid" do
16
+ let(:model) { Model.create }
17
+ let(:new_model) { Model.new }
18
+ let(:uuid) { Vidibus::Uuid.generate }
19
+
20
+ it "should add an uuid field to model" do
21
+ model.should respond_to(:uuid)
22
+ end
23
+
24
+ it "should generate a valid UUID before creation" do
25
+ model.uuid.length.should eql(32)
26
+ end
27
+
28
+ it "should keep the UUID persistent" do
29
+ uuid = model.uuid
30
+ model.save!
31
+ model.uuid.should eql(uuid)
32
+ end
33
+
34
+ it "should allow assignment of UUID" do
35
+ new_model.uuid = uuid
36
+ new_model.save
37
+ new_model.uuid.should eql(uuid)
38
+ end
39
+
40
+ it "should allow changes to UUID" do
41
+ model.update_attributes(:uuid => uuid)
42
+ model.uuid.should eql(uuid)
43
+ end
44
+
45
+ it "should verify format of UUID" do
46
+ model.update_attributes(:uuid => "as")
47
+ model.errors[:uuid].length.should eql(1)
48
+ end
49
+
50
+ it "should verify uniqueness of UUID in class" do
51
+ another_model = Model.create(:uuid => model.uuid)
52
+ another_model.errors[:uuid].should have(1).error
53
+ end
54
+
55
+ it "should verify uniqueness of UUID in embedded documents" do
56
+ first_child = model.children.create!
57
+ second_child = model.children.create!
58
+ second_child.update_attributes(:uuid => first_child.uuid)
59
+ second_child.errors[:uuid].should have(1).error
60
+ model.reload
61
+ model.children[0].uuid.should_not eql(model.children[1].uuid)
62
+ end
63
+
64
+ it "should set UUID as default param" do
65
+ model.should respond_to(:to_param)
66
+ model.to_param.should eql(model.uuid)
67
+ end
68
+ end
@@ -0,0 +1,8 @@
1
+ require "spec_helper"
2
+
3
+ describe "Vidibus::Uuid" do
4
+ it "should return a compact UUID" do
5
+ uuid = Vidibus::Uuid.generate
6
+ uuid.length.should eql(32)
7
+ end
8
+ end
@@ -0,0 +1,44 @@
1
+ require "spec_helper"
2
+
3
+ class ModelWithValidations
4
+ include ActiveModel::Validations
5
+ attr_accessor :required_uuid, :optional_uuid
6
+ validates :required_uuid, :uuid => true
7
+ validates :optional_uuid, :uuid => { :allow_blank => true }
8
+ end
9
+
10
+ describe "Vidibus::ValidateUuid::UuidValidator" do
11
+ let(:model) { ModelWithValidations.new }
12
+
13
+ it "should be available as uuid validator for attribute required_uuid" do
14
+ ModelWithValidations.validators_on(:required_uuid).first.should be_a_kind_of(Vidibus::ValidateUuid::UuidValidator)
15
+ end
16
+
17
+ it "should be available as uuid validator for attribute optional_uuid" do
18
+ ModelWithValidations.validators_on(:optional_uuid).first.should be_a_kind_of(Vidibus::ValidateUuid::UuidValidator)
19
+ end
20
+
21
+ it "should validate ddeb4500668e012d47bb58b035f038ab" do
22
+ model.required_uuid = "ddeb4500668e012d47bb58b035f038ab"
23
+ model.should be_valid
24
+ end
25
+
26
+ it "should add an error for ddeb4500668e0" do
27
+ model.required_uuid = "ddeb4500668e0"
28
+ model.should be_invalid
29
+ model.errors[:required_uuid].should have(1).error
30
+ model.errors[:required_uuid].first.should eql("is not a valid UUID")
31
+ end
32
+
33
+ it "should add an error on required_uuid, if empty" do
34
+ model.required_uuid = ''
35
+ model.should be_invalid
36
+ model.errors[:required_uuid].should have(1).error
37
+ end
38
+
39
+ it "should allow emtpy optional_uuid" do
40
+ model.required_uuid = "ddeb4500668e012d47bb58b035f038ab"
41
+ model.optional_uuid = ""
42
+ model.should be_valid
43
+ end
44
+ end
@@ -0,0 +1,69 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{vidibus-uuid}
8
+ s.version = "0.3.5"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Andre Pankratz"]
12
+ s.date = %q{2010-08-09}
13
+ s.description = %q{Provides UUID generation for Mongoid models. It includes a validator for UUIDs.}
14
+ s.email = %q{andre@vidibus.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "config/locale/en.yml",
27
+ "lib/vidibus-uuid.rb",
28
+ "lib/vidibus/uuid.rb",
29
+ "lib/vidibus/uuid/mongoid.rb",
30
+ "lib/vidibus/validate_uuid.rb",
31
+ "spec/spec.opts",
32
+ "spec/spec_helper.rb",
33
+ "spec/vidibus/uuid/mongoid_spec.rb",
34
+ "spec/vidibus/uuid_spec.rb",
35
+ "spec/vidibus/validate_uuid_spec.rb",
36
+ "vidibus-uuid.gemspec"
37
+ ]
38
+ s.homepage = %q{http://github.com/vidibus/vidibus-uuid}
39
+ s.rdoc_options = ["--charset=UTF-8"]
40
+ s.require_paths = ["lib"]
41
+ s.rubygems_version = %q{1.3.7}
42
+ s.summary = %q{Provides UUID generation through UUID gem.}
43
+ s.test_files = [
44
+ "spec/spec_helper.rb",
45
+ "spec/vidibus/uuid/mongoid_spec.rb",
46
+ "spec/vidibus/uuid_spec.rb",
47
+ "spec/vidibus/validate_uuid_spec.rb"
48
+ ]
49
+
50
+ if s.respond_to? :specification_version then
51
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
52
+ s.specification_version = 3
53
+
54
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
55
+ s.add_development_dependency(%q<rspec>, [">= 0"])
56
+ s.add_development_dependency(%q<mongoid>, ["= 2.0.0.beta.15"])
57
+ s.add_runtime_dependency(%q<uuid>, [">= 0"])
58
+ else
59
+ s.add_dependency(%q<rspec>, [">= 0"])
60
+ s.add_dependency(%q<mongoid>, ["= 2.0.0.beta.15"])
61
+ s.add_dependency(%q<uuid>, [">= 0"])
62
+ end
63
+ else
64
+ s.add_dependency(%q<rspec>, [">= 0"])
65
+ s.add_dependency(%q<mongoid>, ["= 2.0.0.beta.15"])
66
+ s.add_dependency(%q<uuid>, [">= 0"])
67
+ end
68
+ end
69
+
metadata ADDED
@@ -0,0 +1,132 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: vidibus-uuid
3
+ version: !ruby/object:Gem::Version
4
+ hash: 25
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 3
9
+ - 5
10
+ version: 0.3.5
11
+ platform: ruby
12
+ authors:
13
+ - Andre Pankratz
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-08-09 00:00:00 +02:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rspec
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :development
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: mongoid
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - "="
42
+ - !ruby/object:Gem::Version
43
+ hash: 62196477
44
+ segments:
45
+ - 2
46
+ - 0
47
+ - 0
48
+ - beta
49
+ - 15
50
+ version: 2.0.0.beta.15
51
+ type: :development
52
+ version_requirements: *id002
53
+ - !ruby/object:Gem::Dependency
54
+ name: uuid
55
+ prerelease: false
56
+ requirement: &id003 !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ hash: 3
62
+ segments:
63
+ - 0
64
+ version: "0"
65
+ type: :runtime
66
+ version_requirements: *id003
67
+ description: Provides UUID generation for Mongoid models. It includes a validator for UUIDs.
68
+ email: andre@vidibus.com
69
+ executables: []
70
+
71
+ extensions: []
72
+
73
+ extra_rdoc_files:
74
+ - LICENSE
75
+ - README.rdoc
76
+ files:
77
+ - .document
78
+ - .gitignore
79
+ - LICENSE
80
+ - README.rdoc
81
+ - Rakefile
82
+ - VERSION
83
+ - config/locale/en.yml
84
+ - lib/vidibus-uuid.rb
85
+ - lib/vidibus/uuid.rb
86
+ - lib/vidibus/uuid/mongoid.rb
87
+ - lib/vidibus/validate_uuid.rb
88
+ - spec/spec.opts
89
+ - spec/spec_helper.rb
90
+ - spec/vidibus/uuid/mongoid_spec.rb
91
+ - spec/vidibus/uuid_spec.rb
92
+ - spec/vidibus/validate_uuid_spec.rb
93
+ - vidibus-uuid.gemspec
94
+ has_rdoc: true
95
+ homepage: http://github.com/vidibus/vidibus-uuid
96
+ licenses: []
97
+
98
+ post_install_message:
99
+ rdoc_options:
100
+ - --charset=UTF-8
101
+ require_paths:
102
+ - lib
103
+ required_ruby_version: !ruby/object:Gem::Requirement
104
+ none: false
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ hash: 3
109
+ segments:
110
+ - 0
111
+ version: "0"
112
+ required_rubygems_version: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ hash: 3
118
+ segments:
119
+ - 0
120
+ version: "0"
121
+ requirements: []
122
+
123
+ rubyforge_project:
124
+ rubygems_version: 1.3.7
125
+ signing_key:
126
+ specification_version: 3
127
+ summary: Provides UUID generation through UUID gem.
128
+ test_files:
129
+ - spec/spec_helper.rb
130
+ - spec/vidibus/uuid/mongoid_spec.rb
131
+ - spec/vidibus/uuid_spec.rb
132
+ - spec/vidibus/validate_uuid_spec.rb