uuids 1.4.2 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5b59630d1254cf17915e3a7b7beb44845dd1812f
4
- data.tar.gz: 9eb598e421669103dcbf1c227e71e0ce5a5b775a
3
+ metadata.gz: 47eac9c29e60c9923b3ccc38491475e2090447f4
4
+ data.tar.gz: da49a4fb5fe1cac362c92521d2a5a79691f97a2a
5
5
  SHA512:
6
- metadata.gz: e8c87ee98ad9b849aa557e1da0b42fb1da5b699dfce7e3039bdc448c0b2fda9884865b233d79b37f79a008ce33b2b71540bbb276c8b352e0999d14c6588c8ed3
7
- data.tar.gz: 5c94540825bcbf75fc55890c4ac28c1d5dfac97e41050de8e11a60796b4c4e82bf3b3431d7f1e93b7d7f6af7a713b7dd1ab4e420daf2fcb617c5e0f6b5a43f23
6
+ metadata.gz: f8fd6392d538cb0b819b1d3a54e691b180a60b50f85142cd69b855f3b471782b794eadbd5aeace541ee0b69b866e88f2f0a495047f0bd48462aca24afc59db6c
7
+ data.tar.gz: c9823e9694e4b849ee54f78b64aa96804830d43d0963c887dbd5b3c3a2db8987f301dda4c18f71b8381b999b15b9a670723ead18703c8db475b91aa91059fd1e
@@ -1,38 +1,40 @@
1
1
  module Uuids
2
2
 
3
- # Stores uuids assigned to corresponding records.
4
- #
5
- # Attributes:
6
- # +value+:: A value of uuid as defined in
7
- # {RFC4122}[http://www.ietf.org/rfc/rfc4122.txt].
8
- # Assigned by default on creation. Cannot be set or edited manually.
9
- # +record+:: An AR record the uuid is assigned to.
10
- # Required attribute. Can be changed.
3
+ # The model describes uuids assigned to corresponding records.
4
+ # @see http://www.ietf.org/rfc/rfc4122.txt RFC4122 standard.
5
+ # @see https://en.wikipedia.org/wiki/Universally_unique_identifier Wikipedia
6
+ # article.
11
7
  #
12
8
  # @example
13
- # # Create the uuid
14
- # uuid = Uuids::Uuid.create! record: some_record
9
+ # # Creates the uuid
10
+ # uuid = Uuids::Uuid.create! record: some_record
15
11
  #
16
- # # Find a record by uuid
17
- # Uuid.find_by_value(uuid.value).record # => some_record
12
+ # # Finds a record by uuid
13
+ # Uuid.find_by_value(uuid.value).record # => some_record
18
14
  #
19
- # # Reassigns a uuid to another record
20
- # uuid.update_attributes! record: another_record
15
+ # # Reassigns a uuid to another record
16
+ # uuid.update_attributes! record: another_record
21
17
  #
22
18
  class Uuid < ActiveRecord::Base
23
19
  self.table_name = :uuids_uuids
24
20
 
25
- # A format for UUID following RFC4122
26
- UUID_FORMAT = /[a-z\d]{8}-[a-z\d]{4}-[a-z\d]{4}-[a-z\d]{4}-[a-z\d]{12}/
21
+ # @!attribute [r] value
22
+ # Assigned by default on creation. Cannot be set or edited manually.
23
+ # @return [String] uuid value as defined in RFC4122.
24
+
25
+ UUID = /[a-z\d]{8}-[a-z\d]{4}-[a-z\d]{4}-[a-z\d]{4}-[a-z\d]{12}/
26
+ attr_readonly :value
27
+ validates :value, format: { with: UUID }, allow_nil: true
27
28
 
28
- attr_readonly :value
29
- belongs_to :record, polymorphic: true
29
+ # @!attribute record
30
+ # @return [ActiveRecord::Base] an object the uuid is assigned to.
30
31
 
31
- validates :value, format: { with: UUID_FORMAT }, allow_nil: true
32
- validate :record_present?
32
+ belongs_to :record, polymorphic: true
33
+ validate :record_present?
33
34
 
34
- before_create :set_default_value
35
- before_destroy :forbid_destruction
35
+ # Callbacks
36
+ after_initialize :set_default_value
37
+ before_destroy :forbid_destruction
36
38
 
37
39
  private
38
40
 
data/bin/uuids CHANGED
@@ -1,6 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
  require "active_record_install"
3
3
 
4
+ # Initializes the 'uuids' gem inside application.
4
5
  ActiveRecordInstall::Generator.start(
5
6
  [File.expand_path("../..", __FILE__), "uuids"] + ARGV[1..-1]
6
7
  )
@@ -1,3 +1,6 @@
1
+ # encoding: utf-8
2
+
3
+ # Creates a table for the Uuids::Uuid model of uuids.
1
4
  class CreateUuidsUuids < ActiveRecord::Migration
2
5
  def change
3
6
  create_table :uuids_uuids do |t|
@@ -1,7 +1,7 @@
1
1
  module Uuids
2
2
  module Base
3
3
 
4
- # Defines custom methods to be defined by `has_uuids` method.
4
+ # Defines methods to be added by the +has_uuids+ class helper.
5
5
  module HasUuids
6
6
  extend ActiveSupport::Concern
7
7
 
@@ -10,25 +10,20 @@ module Uuids
10
10
 
11
11
  # Selects records by uuid.
12
12
  #
13
- # by_uuid(*values)
14
- #
15
13
  # @example
14
+ # class MyRecord < ActiveRecord::Base
15
+ # include Uuids::Base
16
+ # has_uuids
17
+ # end
16
18
  #
17
- # class MyRecord < ActiveRecord::Base
18
- # include Uuids::Base
19
- # has_uuids
20
- # end
21
- #
22
- # MyRecord.by_uuid(
23
- # "23423fe3-28d8-a1e5-bde3-2e08074aa92d",
24
- # "9223238d-a7e3-2d36-a93d-1e50fea02de2"
25
- # )
26
- # # => #<ActiveRecord::Relation ...>
27
- #
28
- # Params:
29
- # <tt>*values</tt>:: a list of uuids to select records by.
19
+ # MyRecord.by_uuid(
20
+ # "23423fe3-28d8-a1e5-bde3-2e08074aa92d",
21
+ # "9223238d-a7e3-2d36-a93d-1e50fea02de2"
22
+ # )
23
+ # # => #<ActiveRecord::Relation ...>
30
24
  #
31
- # Returns an <tt>ActiveRecord::Relation</tt> scope.
25
+ # @param values [Array<String>] a list of uuids to select records by.
26
+ # @return [ActiveRecord::Relation] scope.
32
27
  def by_uuid(*values)
33
28
  first_value = values.first
34
29
  list = first_value.is_a?(Array) ? first_value : values
@@ -39,10 +34,11 @@ module Uuids
39
34
  # Returns the first UUID (sorted as a string) for the record.
40
35
  #
41
36
  # @example
42
- # record.uuids.new value: "23423fe3-28d8-a1e5-bde3-2e08074aa92d"
43
- # record.uuids.new value: "9223238d-a7e3-2d36-a93d-1e50fea02de2"
44
- # record.uuid # => "23423fe3-28d8-a1e5-bde3-2e08074aa92d"
37
+ # record.uuids.new value: "23423fe3-28d8-a1e5-bde3-2e08074aa92d"
38
+ # record.uuids.new value: "9223238d-a7e3-2d36-a93d-1e50fea02de2"
39
+ # record.uuid # => "23423fe3-28d8-a1e5-bde3-2e08074aa92d"
45
40
  #
41
+ # @return [String] first value of the record's uuids.
46
42
  def uuid
47
43
  uuids.map(&:value).sort.first
48
44
  end
@@ -50,12 +46,11 @@ module Uuids
50
46
  # Assigns a new uuid to the record.
51
47
  #
52
48
  # @example
53
- # record.uuid = "23423fe3-28d8-a1e5-bde3-2e08074aa92d"
54
- # record.uuid # => "23423fe3-28d8-a1e5-bde3-2e08074aa92d"
55
- #
56
- # Params:
57
- # <tt>value</tt>:: a string value of uuid to assign.
49
+ # record.uuid = "23423fe3-28d8-a1e5-bde3-2e08074aa92d"
50
+ # record.uuid # => "23423fe3-28d8-a1e5-bde3-2e08074aa92d"
58
51
  #
52
+ # @param value [String] a value of uuid to assign.
53
+ # @return object
59
54
  def uuid=(value)
60
55
  uuids.new value: value
61
56
  end
@@ -63,19 +58,18 @@ module Uuids
63
58
  # Assigns a list of new uuids to the record.
64
59
  #
65
60
  # @example
66
- # record.uuids = [
67
- # "23423fe3-28d8-a1e5-bde3-2e08074aa92d",
68
- # "9223238d-a7e3-2d36-a93d-1e50fea02de2"
69
- # ]
70
- # record.uuids.map(&:value)
71
- # # => [
72
- # "23423fe3-28d8-a1e5-bde3-2e08074aa92d",
73
- # "9223238d-a7e3-2d36-a93d-1e50fea02de2"
74
- # ]
75
- #
76
- # Params:
77
- # <tt>values</tt>:: an array of uuids string values to assign.
61
+ # record.uuids = [
62
+ # "23423fe3-28d8-a1e5-bde3-2e08074aa92d",
63
+ # "9223238d-a7e3-2d36-a93d-1e50fea02de2"
64
+ # ]
65
+ # record.uuids.map(&:value)
66
+ # # => [
67
+ # "23423fe3-28d8-a1e5-bde3-2e08074aa92d",
68
+ # "9223238d-a7e3-2d36-a93d-1e50fea02de2"
69
+ # ]
78
70
  #
71
+ # @param values [Array<String>] an array of uuids string values to assign
72
+ # @return object.
79
73
  def uuids=(*values)
80
74
  first = values.first
81
75
  list = first.is_a?(Array) ? first : values
@@ -84,12 +78,12 @@ module Uuids
84
78
 
85
79
  private
86
80
 
87
- # Creates the uuids by default preventing a record from being ureferrable.
81
+ # Creates the uuid value by default.
88
82
  def add_default_uuid
89
83
  uuids.present? || uuids.new
90
84
  end
91
85
 
92
- # Prevents destruction of a record before its uuids reassigned to
86
+ # Prevents destruction of a record before its uuids are reassigned to
93
87
  # other records.
94
88
  def prevent_destruction
95
89
  return true if uuids.blank?
data/lib/uuids/base.rb CHANGED
@@ -2,7 +2,7 @@ require_relative "base/has_uuids"
2
2
 
3
3
  module Uuids
4
4
 
5
- # Creates required `#uuids` attribute of the ActiveRecord model.
5
+ # Defines the +has_uuids+ model class helper.
6
6
  module Base
7
7
  extend ActiveSupport::Concern
8
8
 
@@ -11,42 +11,41 @@ module Uuids
11
11
 
12
12
  private
13
13
 
14
- # Declares:
14
+ # The helper defines:
15
15
  #
16
- # +uuids+:: association attribute.
17
- # +uuid+:: method (string).
18
- # <tt>uuid=</tt>:: new uuid value assignment.
19
- # <tt>uuids=</tt>:: a list of uuids values assignment.
20
- # <tt>uuid</tt>:: relation scope.
16
+ # +uuids+:: the +ActiveRecord+ association with a record's uuids.
17
+ # +uuid+:: a virtual attribute that returns the first value of uuids.
18
+ # <tt>uuid=</tt>:: new uuid setter.
19
+ # <tt>uuids=</tt>:: uuids group setter.
20
+ # <tt>by_uuid</tt>:: the +ActiveRecord+ relation scope.
21
21
  #
22
22
  # @example
23
- # class City < ActiveRecord::Base
24
- # has_uuids
25
- # end
23
+ # class City < ActiveRecord::Base
24
+ # has_uuids
25
+ # end
26
26
  #
27
- # city = City.create!
27
+ # city = City.create!
28
28
  #
29
- # city.uuids.map(&:value)
30
- # # => 51f50391-fcd2-4f69-aab7-6ef31b29c379
29
+ # city.uuids.map(&:value)
30
+ # # => 51f50391-fcd2-4f69-aab7-6ef31b29c379
31
31
  #
32
- # city.uuid
33
- # # => 51f50391-fcd2-4f69-aab7-6ef31b29c379
32
+ # city.uuid
33
+ # # => 51f50391-fcd2-4f69-aab7-6ef31b29c379
34
34
  #
35
- # city.uuid = "51f50391-fcd2-4f69-aab7-6ef31b29c379"
35
+ # city.uuid = "51f50391-fcd2-4f69-aab7-6ef31b29c379"
36
36
  #
37
- # city.uuids = [
38
- # "51f50391-fcd2-4f69-aab7-6ef31b29c379",
39
- # "3ea8fd89-232f-4ed1-90b5-743da173cd7d"
40
- # ]
41
- #
42
- # City.by_uuid("51f50391-fcd2-4f69-aab7-6ef31b29c379").to_a == [city]
43
- # # => true
37
+ # city.uuids = [
38
+ # "51f50391-fcd2-4f69-aab7-6ef31b29c379",
39
+ # "3ea8fd89-232f-4ed1-90b5-743da173cd7d"
40
+ # ]
44
41
  #
42
+ # City.by_uuid("51f50391-fcd2-4f69-aab7-6ef31b29c379").to_a == [city]
43
+ # # => true
45
44
  def has_uuids
46
45
  has_many :uuids, class_name: "Uuids::Uuid", as: :record, validate: false
47
46
  include Uuids::Base::HasUuids
48
- before_create :add_default_uuid
49
- before_destroy :prevent_destruction
47
+ after_initialize :add_default_uuid
48
+ before_destroy :prevent_destruction
50
49
  validates :uuids, presence: true, on: :update
51
50
  end
52
51
  end
data/lib/uuids/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  module Uuids
2
2
 
3
3
  # Current release.
4
- VERSION = "1.4.2"
4
+ VERSION = "2.0.0"
5
5
  end
data/lib/uuids.rb CHANGED
@@ -3,11 +3,11 @@ require "active_record"
3
3
  require "i18n"
4
4
  require "securerandom"
5
5
 
6
- # `uuids` gem content.
6
+ # The +uuids+ gem content.
7
7
  root = File.expand_path "../..", __FILE__
8
8
  Dir[File.join(root, "lib/uuids/**/*.rb")].each { |f| require f }
9
9
  Dir[File.join(root, "app/**/*.rb")].each { |f| require f }
10
10
 
11
- # Namespace for `uuids` gem code.
11
+ # Namespace for the +uuids+ gem code.
12
12
  module Uuids
13
13
  end
@@ -15,7 +15,6 @@ module Uuids
15
15
  end
16
16
 
17
17
  it "assigned by default" do
18
- subject.save!
19
18
  expect(subject.value).not_to be_blank
20
19
  end
21
20
  end
Binary file
@@ -45,17 +45,14 @@ module Uuids
45
45
 
46
46
  it "defines the #uuid= method" do
47
47
  value = "3ea8fd89-232f-4ed1-90b5-743da173cd7d"
48
- expect { subject.uuid = value }.to change { subject.uuids.map(&:value) }
49
- .to [value]
48
+ subject.uuid = value
49
+ expect(subject.uuids.map(&:value)).to be_include(value)
50
50
  end
51
51
 
52
52
  it "defines the #uuids= method" do
53
- values = [
54
- "3ea8fd89-232f-4ed1-90b5-743da173cd7d",
55
- "4ea8fd89-232f-4ed1-90b5-743da173cd7d"
56
- ]
57
- expect { subject.uuids = values }
58
- .to change { subject.uuids.map(&:value) }.to values
53
+ value = "3ea8fd89-232f-4ed1-90b5-743da173cd7d"
54
+ subject.uuids = [value]
55
+ expect(subject.uuids.map(&:value)).to be_include(value)
59
56
  end
60
57
 
61
58
  it "defines +by_uuid+ class scope" do
@@ -66,7 +63,6 @@ module Uuids
66
63
  end
67
64
 
68
65
  it "creates the first uuid by default" do
69
- subject.save!
70
66
  expect(subject.uuid).not_to be_blank
71
67
  end
72
68
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: uuids
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.2
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Kozin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-11-22 00:00:00.000000000 Z
11
+ date: 2014-11-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord