activerecord_uuid 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ .idea
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in activerecord_uuid.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,67 @@
1
+ # ActiveRecordUUID
2
+
3
+ UUID extension for ActiveRecord
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'activerecord_uuid'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install activerecord_uuid
18
+
19
+ ## Usage
20
+
21
+ ### Migrations
22
+
23
+ Define attribute as UUID
24
+
25
+ class CreateCountries < ActiveRecord::Migration
26
+ def change
27
+ create_table :countries do |t|
28
+ t.uuid :key
29
+ end
30
+ end
31
+ end
32
+
33
+ Define primary key as UUID
34
+
35
+ class CreateLanguages < ActiveRecord::Migration
36
+ def change
37
+ create_table :languages do |t|
38
+ t.uuid :id, primary_key: true
39
+ end
40
+ end
41
+ end
42
+
43
+ ### Models
44
+
45
+ For automatic generation of UUID before creation
46
+
47
+ class Country < ActiveRecord::Base
48
+ attr_uuid :key
49
+ end
50
+
51
+ For change primary key to UUID
52
+
53
+ class Language < ActiveRecord::Base
54
+ pk_uuid
55
+ end
56
+
57
+ ### Manually creation of UUIDs
58
+
59
+ ActiveRecordUUID.random
60
+
61
+ ## Contributing
62
+
63
+ 1. Fork it
64
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
65
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
66
+ 4. Push to the branch (`git push origin my-new-feature`)
67
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "activerecord_uuid/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = 'activerecord_uuid'
7
+ s.version = ActiveRecordUUID::VERSION
8
+ s.authors = ['Gabriel Naiman']
9
+ s.email = ['gabynaiman@gmail.com']
10
+ s.homepage = 'https://github.com/gabynaiman/activerecord_uuid'
11
+ s.summary = 'UUID extension for ActiveRecord'
12
+ s.description = 'UUID extension for ActiveRecord'
13
+
14
+ s.files = `git ls-files`.split("\n")
15
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
16
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
17
+ s.require_paths = ["lib"]
18
+
19
+ s.add_dependency 'rails', '>= 3.2.0'
20
+ s.add_dependency 'uuidtools'
21
+
22
+ s.add_development_dependency 'sqlite3'
23
+ s.add_development_dependency 'rspec'
24
+ end
@@ -0,0 +1,26 @@
1
+ module ActiveRecordUUID
2
+
3
+ module ActiveRecord
4
+ def pk_uuid
5
+ self.primary_key = :id
6
+ self.sequence_name = nil
7
+ before_create do
8
+ self.id = ActiveRecordUUID.random unless self.id
9
+ end
10
+ end
11
+
12
+ def attr_uuid(name)
13
+ before_create do
14
+ self.send("#{name}=", ActiveRecordUUID.random) unless self.send("#{name}")
15
+ end
16
+ end
17
+ end
18
+
19
+ module TableDefinition
20
+ def uuid(name, options={})
21
+ @columns.delete @columns_hash.delete('id') if options[:primary_key]
22
+ column(name, :string, limit: 36, null: options[:null] || !options[:primary_key], primary: options[:primary_key])
23
+ end
24
+ end
25
+
26
+ end
@@ -0,0 +1,3 @@
1
+ module ActiveRecordUUID
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,17 @@
1
+ require 'active_record'
2
+ require 'uuidtools'
3
+ require 'logger'
4
+
5
+ require 'activerecord_uuid/version'
6
+ require 'activerecord_uuid/active_record_extension'
7
+
8
+ ActiveRecord::Base.send :extend, ActiveRecordUUID::ActiveRecord
9
+ ActiveRecord::ConnectionAdapters::TableDefinition.send :include, ActiveRecordUUID::TableDefinition
10
+
11
+ module ActiveRecordUUID
12
+
13
+ def self.random
14
+ UUIDTools::UUID.random_create.to_s
15
+ end
16
+
17
+ end
@@ -0,0 +1,48 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'ActiveRecord extension' do
4
+
5
+ before :all do
6
+ ActiveRecord::Base.establish_connection adapter: 'sqlite3', database: ":memory:"
7
+ ActiveRecord::Base.connection
8
+ ActiveRecord::Migrator.migrate ActiveRecord::Migrator.migrations_path
9
+ end
10
+
11
+ context 'Attribute uuid' do
12
+
13
+ it 'Migration type' do
14
+ Country.primary_key.should eq 'id'
15
+ uuid_column = Country.columns_hash['key']
16
+ uuid_column.type.should eq :string
17
+ uuid_column.limit.should eq 36
18
+ uuid_column.null.should be_true
19
+ end
20
+
21
+ it 'ActiveRecord generation' do
22
+ model = Country.create!
23
+ model.key.should be_a String
24
+ model.key.size.should eq 36
25
+ end
26
+
27
+ end
28
+
29
+ context 'Primary key uuid' do
30
+
31
+ it 'Migration type' do
32
+ Language.primary_key.should eq 'id'
33
+ uuid_column = Language.columns_hash['id']
34
+ uuid_column.type.should eq :string
35
+ uuid_column.limit.should eq 36
36
+ uuid_column.primary.should be_true
37
+ uuid_column.null.should be_false
38
+ end
39
+
40
+ it 'ActiveRecord generation' do
41
+ model = Language.create!
42
+ model.id.should be_a String
43
+ model.id.size.should eq 36
44
+ end
45
+
46
+ end
47
+
48
+ end
@@ -0,0 +1,7 @@
1
+ class CreateCountries < ActiveRecord::Migration
2
+ def change
3
+ create_table :countries do |t|
4
+ t.uuid :key
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ class CreateLanguages < ActiveRecord::Migration
2
+ def change
3
+ create_table :languages do |t|
4
+ t.uuid :id, primary_key: true
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,11 @@
1
+ require 'activerecord_uuid'
2
+
3
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
4
+
5
+ TEMP_PATH = ENV['TMP'].gsub("\\", '/')
6
+
7
+ ActiveRecord::Base.logger = Logger.new($stdout)
8
+ ActiveRecord::Migrator.migrations_path = "#{File.dirname(__FILE__)}/migrations"
9
+
10
+ RSpec.configure do |config|
11
+ end
@@ -0,0 +1,3 @@
1
+ class Country < ActiveRecord::Base
2
+ attr_uuid :key
3
+ end
@@ -0,0 +1,3 @@
1
+ class Language < ActiveRecord::Base
2
+ pk_uuid
3
+ end
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activerecord_uuid
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Gabriel Naiman
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-10 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rails
16
+ requirement: &2175168 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 3.2.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *2175168
25
+ - !ruby/object:Gem::Dependency
26
+ name: uuidtools
27
+ requirement: &2174436 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *2174436
36
+ - !ruby/object:Gem::Dependency
37
+ name: sqlite3
38
+ requirement: &2173824 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *2173824
47
+ - !ruby/object:Gem::Dependency
48
+ name: rspec
49
+ requirement: &2173296 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *2173296
58
+ description: UUID extension for ActiveRecord
59
+ email:
60
+ - gabynaiman@gmail.com
61
+ executables: []
62
+ extensions: []
63
+ extra_rdoc_files: []
64
+ files:
65
+ - .gitignore
66
+ - Gemfile
67
+ - README.md
68
+ - Rakefile
69
+ - activerecord_uuid.gemspec
70
+ - lib/activerecord_uuid.rb
71
+ - lib/activerecord_uuid/active_record_extension.rb
72
+ - lib/activerecord_uuid/version.rb
73
+ - spec/active_record_extension_spec.rb
74
+ - spec/migrations/20121004135939_create_countries.rb
75
+ - spec/migrations/20121004135940_create_languages.rb
76
+ - spec/spec_helper.rb
77
+ - spec/support/models/country.rb
78
+ - spec/support/models/language.rb
79
+ homepage: https://github.com/gabynaiman/activerecord_uuid
80
+ licenses: []
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ none: false
87
+ requirements:
88
+ - - ! '>='
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ none: false
93
+ requirements:
94
+ - - ! '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ requirements: []
98
+ rubyforge_project:
99
+ rubygems_version: 1.8.16
100
+ signing_key:
101
+ specification_version: 3
102
+ summary: UUID extension for ActiveRecord
103
+ test_files: []