universal_identifiable 0.0.3 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3bc5d482d8dbeb285bec83b006c9433fd0cef381
4
- data.tar.gz: 9e90ab652ffd0544d54f5564b997efc31b08d20c
3
+ metadata.gz: 91f7d2b36d371e5ffbc7f20025af65735e6d1c3f
4
+ data.tar.gz: 48b468318c9352dc9ed4dfc9e833285ea58e3681
5
5
  SHA512:
6
- metadata.gz: 5294c8ca97397018e88770739b39c6e1505bfc3229ff8dab4e03f56d941171791bd68488616f5c23c61e1f63ea35111ee6eb33880c2f35d3a53987e9665b7574
7
- data.tar.gz: 4d2de664d3b195532e5a92dbaf294ff9a5c589a45baf45bc80418c79186e130f5b9d7a9a7d6b38c2ca38a556ed31bea87d812e99d562434c53d276b053a082ef
6
+ metadata.gz: 7a9c1fbf3d720d8178b3d139b6a7ec51aaa18c5f68352c9b7617a4d6824869f1548167368d78e2eab4aac491f7fb8b306059768967a8ce45203d60d60cd31b67
7
+ data.tar.gz: 84cb8169e8002633e070fbe634c1af3cc8233ae3a41dadd278eb5e9965e138affbb773f4e0b99e7d00cc3f608f80b962ad759da9d415398162248f8df8b338ca
@@ -1,11 +1,11 @@
1
1
  require "universal_identifiable/version"
2
2
  require 'active_record'
3
+ require 'active_support/core_ext/string'
3
4
 
4
5
  module UniversalIdentifiable
5
- # sample uuid: 'hotel.ritz'
6
- # TODO: store underscored modelname as prefix automatically. e.G. '#{modelname}.ritz' when assigning attribute
7
6
 
8
7
  NAMESPACER = "."
8
+ REGEX = Regexp.new("\\#{NAMESPACER}(.*)")
9
9
 
10
10
  def self.included(base)
11
11
  base.validates :uuid, :presence => true, :uniqueness => true
@@ -16,9 +16,16 @@ module UniversalIdentifiable
16
16
  options[:namespaced] ? read_attribute(:uuid) : short_uuid
17
17
  end
18
18
 
19
+ def uuid=(uuid)
20
+ uuid = "#{self.class.name.underscore}#{NAMESPACER}#{uuid}" if uuid
21
+ super uuid
22
+ end
23
+
19
24
  private
20
25
 
21
26
  def short_uuid
22
- read_attribute(:uuid).split(NAMESPACER).last
27
+ value = read_attribute(:uuid)
28
+ value.match(REGEX)[1] if value
23
29
  end
30
+
24
31
  end
@@ -1,3 +1,3 @@
1
1
  module UniversalIdentifiable
2
- VERSION = "0.0.3"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -6,7 +6,7 @@ require 'universal_identifiable'
6
6
  class TestCase < Minitest::Spec
7
7
  end
8
8
 
9
- class FakeRecord < ActiveRecord::Base
9
+ class Airport < ActiveRecord::Base
10
10
  include UniversalIdentifiable
11
11
  end
12
12
 
@@ -18,7 +18,7 @@ ActiveRecord::Base.establish_connection(
18
18
  ActiveRecord::Schema.define do
19
19
  self.verbose = false
20
20
 
21
- create_table :fake_records, :force => true do |t|
21
+ create_table :airports, :force => true do |t|
22
22
  t.string :uuid
23
23
  end
24
24
  end
@@ -5,7 +5,8 @@ module UniversalIdentifiable
5
5
  class UniversalIdentifiableTest < TestCase
6
6
 
7
7
  before do
8
- @identifiable_model = FakeRecord.new(:uuid => "airport#{UniversalIdentifiable::NAMESPACER}dortmund")
8
+ Airport.delete_all
9
+ @identifiable_model = Airport.new(:uuid => "dortmund")
9
10
  end
10
11
 
11
12
  it "is valid" do
@@ -18,7 +19,7 @@ module UniversalIdentifiable
18
19
 
19
20
  it "must have attributes present" do
20
21
  @identifiable_model.uuid = nil
21
- @identifiable_model.valid?
22
+ refute @identifiable_model.valid?
22
23
 
23
24
  assert @identifiable_model.errors.full_messages.any? do |message|
24
25
  message.include? "blank"
@@ -28,9 +29,9 @@ module UniversalIdentifiable
28
29
  it "must be unique" do
29
30
  @identifiable_model.save!
30
31
 
31
- new_model = FakeRecord.new
32
- new_model.uuid = @identifiable_model.uuid
33
- new_model.valid?
32
+ new_model = Airport.new
33
+ new_model.uuid = @identifiable_model.uuid(:namespaced => false)
34
+ refute new_model.valid?
34
35
 
35
36
  assert new_model.errors.full_messages.any? do |message|
36
37
  message.include? "taken"
@@ -44,6 +45,31 @@ module UniversalIdentifiable
44
45
  assert_equal "airport#{UniversalIdentifiable::NAMESPACER}dortmund", @identifiable_model.uuid(:namespaced => true)
45
46
  end
46
47
 
48
+ it "prefixes uuid with class name" do
49
+ airport = Airport.create(:uuid => "dortmund")
50
+
51
+ assert_equal "airport#{UniversalIdentifiable::NAMESPACER}dortmund", airport.uuid(:namespaced => true)
52
+ airport.uuid = "dusseldorf"
53
+ assert_equal "airport#{UniversalIdentifiable::NAMESPACER}dusseldorf", airport.uuid(:namespaced => true)
54
+ airport.update_attribute(:uuid, "dortmund")
55
+ assert_equal "airport#{UniversalIdentifiable::NAMESPACER}dortmund", airport.uuid(:namespaced => true)
56
+ airport.update_attributes(:uuid => "dusseldorf")
57
+ assert_equal "airport#{UniversalIdentifiable::NAMESPACER}dusseldorf", airport.uuid(:namespaced => true)
58
+ end
59
+
60
+ it "anything preceeding first namespacer is considered to be the uuid" do
61
+ airport = Airport.new(:uuid => "dortmund.foo.bar")
62
+
63
+ assert_equal "airport#{UniversalIdentifiable::NAMESPACER}dortmund.foo.bar", airport.uuid(:namespaced => true)
64
+ assert_equal "dortmund.foo.bar", airport.uuid(:namespaced => false)
65
+
66
+ airport.uuid = ""
67
+ assert_equal "", airport.uuid(:namespaced => false)
68
+
69
+ airport.uuid = nil
70
+ assert_equal nil, airport.uuid(:namespaced => false)
71
+ end
72
+
47
73
  end
48
74
 
49
75
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: universal_identifiable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andreas Busold
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-05-15 00:00:00.000000000 Z
11
+ date: 2013-05-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler