sets_uuid 0.1.0

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,5 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Emmanuel Oga
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,27 @@
1
+ = sets_uuid
2
+
3
+ Allows you to declaratively initialize UUID columns in your ActiveRecords.
4
+
5
+ class MyModel < ActiveRecord::Base
6
+
7
+ sets_uuid :before_create, :guid => :compact
8
+
9
+ end
10
+
11
+ m = MyModel.create!
12
+ m.guid # => "a80cca54-6925-4331-a2f7-df23da2b837f"
13
+
14
+ == Note on Patches/Pull Requests
15
+
16
+ * Fork the project.
17
+ * Make your feature addition or bug fix.
18
+ * Add tests for it. This is important so I don't break it in a
19
+ future version unintentionally.
20
+ * Commit, do not mess with rakefile, version, or history.
21
+ (if you want to have your own version, that is fine but
22
+ bump version in a commit by itself I can ignore when I pull)
23
+ * Send me a pull request. Bonus points for topic branches.
24
+
25
+ == Copyright
26
+
27
+ Copyright (c) 2009 Emmanuel Oga. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,51 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "sets_uuid"
8
+ gem.summary = %Q{Adds declarative uuid attribute initialization to models}
9
+ gem.description = %Q{UUID management for models}
10
+ gem.email = "EmmanuelOga@gmail.com"
11
+ gem.homepage = "http://github.com/EmmanuelOga/sets_uuid"
12
+ gem.authors = ["Emmanuel Oga"]
13
+ gem.add_development_dependency "rspec"
14
+ gem.add_dependency "uuid", "~> 2.0.2"
15
+ gem.add_dependency "activerecord"
16
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
17
+ end
18
+ Jeweler::GemcutterTasks.new
19
+ rescue LoadError
20
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
21
+ end
22
+
23
+ require 'spec/rake/spectask'
24
+ Spec::Rake::SpecTask.new(:spec) do |spec|
25
+ spec.libs << 'lib' << 'spec'
26
+ spec.spec_files = FileList['spec/**/*_spec.rb']
27
+ end
28
+
29
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
30
+ spec.libs << 'lib' << 'spec'
31
+ spec.pattern = 'spec/**/*_spec.rb'
32
+ spec.rcov = true
33
+ end
34
+
35
+ task :spec => :check_dependencies
36
+
37
+ task :default => :spec
38
+
39
+ require 'rake/rdoctask'
40
+ Rake::RDocTask.new do |rdoc|
41
+ if File.exist?('VERSION')
42
+ version = File.read('VERSION')
43
+ else
44
+ version = ""
45
+ end
46
+
47
+ rdoc.rdoc_dir = 'rdoc'
48
+ rdoc.title = "sets_uuid #{version}"
49
+ rdoc.rdoc_files.include('README*')
50
+ rdoc.rdoc_files.include('lib/**/*.rb')
51
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
data/lib/sets_uuid.rb ADDED
@@ -0,0 +1,32 @@
1
+ gem 'uuid', "~> 2.0.2"
2
+ require 'uuid'
3
+
4
+ module SetsUUID
5
+ module ClassMethods
6
+
7
+ # Call it on ActiveRecord class definition body:
8
+ #
9
+ # sets_uuid :before_create, :attribute_name => :uuid_format.
10
+ #
11
+ # uuid_format is one of the formats available from uuid gem:
12
+ # :compact, :urn, :default.
13
+ #
14
+ # Inspect UUID::FORMATS for the complete list.
15
+ #
16
+ def sets_uuid(callback_name, attributes_and_formats_hash)
17
+ raise ArgumentError, "attributes_and_formats_hash does not act as a Hash!" unless attributes_and_formats_hash.respond_to?(:each)
18
+
19
+ attributes_and_formats_hash.each do |attribute, format|
20
+
21
+ meth = "__generate_uuid_for_#{ attribute }".intern
22
+
23
+ define_method(meth) { write_attribute(attribute, UUID.new.generate(format)) }
24
+
25
+ send(callback_name, meth)
26
+ end
27
+ end
28
+
29
+ end
30
+ end
31
+
32
+ ActiveRecord::Base.extend(SetsUUID::ClassMethods)
@@ -0,0 +1,38 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ class Thing < ActiveRecord::Base
4
+ sets_uuid :before_validation, :a => :compact
5
+ sets_uuid :before_create, :b => :default
6
+ end
7
+
8
+ describe "SetsUuid" do
9
+ it "should be able to create new things" do
10
+ lambda { Thing.create! :a => "1", :b => "2", :c => "3" }.should_not raise_error
11
+ end
12
+
13
+ it "should be able to set the UUID before validation" do
14
+ a = Thing.new
15
+ a.a.should be_blank
16
+ a.b.should be_blank
17
+ a.c.should be_blank
18
+
19
+ a.valid?
20
+
21
+ a.a.should_not be_blank
22
+ a.b.should be_blank
23
+ a.c.should be_blank
24
+ end
25
+
26
+ it "should be able to set the UUID before creation" do
27
+ a = Thing.new
28
+ a.a.should be_blank
29
+ a.b.should be_blank
30
+ a.c.should be_blank
31
+
32
+ a.save!
33
+
34
+ a.a.should_not be_blank
35
+ a.b.should_not be_blank
36
+ a.c.should be_blank
37
+ end
38
+ end
@@ -0,0 +1,23 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+
4
+ require 'spec'
5
+ require 'spec/autorun'
6
+ require 'logger'
7
+ require 'rubygems'
8
+ require 'activerecord'
9
+
10
+ require 'sets_uuid'
11
+
12
+ ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
13
+
14
+ # ActiveRecord::Base.logger = Logger.new(STDOUT)
15
+
16
+ ActiveRecord::Schema.define do
17
+ create_table "things" do |t|
18
+ t.string :a, :b, :c
19
+ end
20
+ end
21
+
22
+ Spec::Runner.configure do |config|
23
+ end
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sets_uuid
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Emmanuel Oga
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-08-25 00:00:00 +00:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rspec
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: uuid
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 2.0.2
34
+ version:
35
+ - !ruby/object:Gem::Dependency
36
+ name: activerecord
37
+ type: :runtime
38
+ version_requirement:
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: "0"
44
+ version:
45
+ description: UUID management for models
46
+ email: EmmanuelOga@gmail.com
47
+ executables: []
48
+
49
+ extensions: []
50
+
51
+ extra_rdoc_files:
52
+ - LICENSE
53
+ - README.rdoc
54
+ files:
55
+ - .document
56
+ - .gitignore
57
+ - LICENSE
58
+ - README.rdoc
59
+ - Rakefile
60
+ - VERSION
61
+ - lib/sets_uuid.rb
62
+ - spec/sets_uuid_spec.rb
63
+ - spec/spec_helper.rb
64
+ has_rdoc: true
65
+ homepage: http://github.com/EmmanuelOga/sets_uuid
66
+ licenses: []
67
+
68
+ post_install_message:
69
+ rdoc_options:
70
+ - --charset=UTF-8
71
+ require_paths:
72
+ - lib
73
+ required_ruby_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: "0"
78
+ version:
79
+ required_rubygems_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: "0"
84
+ version:
85
+ requirements: []
86
+
87
+ rubyforge_project:
88
+ rubygems_version: 1.3.3
89
+ signing_key:
90
+ specification_version: 3
91
+ summary: Adds declarative uuid attribute initialization to models
92
+ test_files:
93
+ - spec/sets_uuid_spec.rb
94
+ - spec/spec_helper.rb