uuids 1.2.0 → 1.3.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: c89b99f654c1dfea64b0eb20b53e29c43a23e5b6
4
- data.tar.gz: 8248163a5e3b377d0b2d28953d95a12f035e0747
3
+ metadata.gz: ab2fee573db51b09e456f46051b38e4a4bb191a9
4
+ data.tar.gz: 86a0819f977fb1866d60c286bffda28d090285c5
5
5
  SHA512:
6
- metadata.gz: 9c138b109db21234ff97829e1116a542257100dfeec619009edca64e9d33c88b215dd87139aab5a1a852d8b3b2514e5c82ec2db6db26d632d96e379696e3d15e
7
- data.tar.gz: ec574018240a64d28ccb52018f1d91a16a62c71767eb4cbb8318b149337a698059c0bd2fdc45962db94b6fcbd5997d835b775cefe3147f5cc51cca2e869647ed
6
+ metadata.gz: 360f89779357fc89de36f625aac37ea2f8d16516e981f3428f92fe49574c43b11b2982769a02403630e9a18e1abf2f501d8c10e6d87330d819006a698757b2cc
7
+ data.tar.gz: d88f464eeb0923ea24a0405f4aa4a45b4630beaea14ab14afdbff61817d29aaa53a80dd703418ee091c632e366ca7db10a52e43693dc77bc2d2e390ee7a06b1e
data/README.rdoc CHANGED
@@ -98,8 +98,8 @@ This will add methods:
98
98
 
99
99
  +#uuids+:: List of <tt>Uuids::Uuid</tt> objects referred to the record.
100
100
  +#uuid+:: main UUID for the record - a value of the first +uuids+ (ordered by value).
101
- +#uuid=(value):: assigns the UUID (an alias for <tt>#uuids.new value: value</tt>).
102
- <tt>.by_uuid</tt>:: A scope for unique records by UUID: <tt>City.by_uuid</tt>.
101
+ <tt>#uuid=(value)</tt>:: assigns the UUID (an alias for <tt>#uuids.new value: value</tt>).
102
+ <tt>.by_uuid(*values)</tt>:: A scope for selecting unique records by UUID.
103
103
 
104
104
  The first uuid is added by default. It can also be set manually:
105
105
 
data/Rakefile CHANGED
@@ -29,14 +29,16 @@ require "bundler/gem_tasks"
29
29
  require "rspec/core/rake_task"
30
30
 
31
31
  RSpec::Core::RakeTask.new :spec
32
- task default: %w(app:uuids:test:prepare) do
33
- sh "bundle exec rspec spec"
32
+
33
+ task :default do
34
+ system "rake db:migrate RAILS_ENV=test"
35
+ system "bundle exec rspec spec"
34
36
  end
35
37
 
36
- task :full do
37
- sh "coveralls report"
38
- sh "rubocop"
39
- sh "metric_fu"
40
- sh "rails_best_practices"
41
- sh "inch --pedantic"
38
+ task :check do
39
+ system "coveralls report"
40
+ system "rubocop"
41
+ system "metric_fu"
42
+ system "inch --pedantic"
43
+ system "rails_best_practices"
42
44
  end
@@ -0,0 +1,9 @@
1
+ namespace :uuids do
2
+
3
+ desc "Installs the uuids gem inside a Rails application or Rails engine."
4
+ task install: :environment do
5
+ app = Rake::Task.task_defined?("railties:install:migrations") ? "" : "app:"
6
+ Rake::Task["#{ app }railties:install:migrations"].reenable
7
+ system "rake #{ app }uuids:install:migrations"
8
+ end
9
+ end
@@ -0,0 +1,100 @@
1
+ module Uuids
2
+ module Base
3
+
4
+ # Defines custom methods to be defined by `has_uuids` method.
5
+ module HasUuids
6
+ extend ActiveSupport::Concern
7
+
8
+ # Model scopes.
9
+ module ClassMethods
10
+
11
+ # Selects distinct records by uuids (relation scope).
12
+ #
13
+ # @example
14
+ #
15
+ # class MyRecord < ActiveRecord::Base
16
+ # include Uuids::Base
17
+ # has_uuids
18
+ # end
19
+ #
20
+ # MyRecord.by_uuid(
21
+ # "23423fe3-28d8-a1e5-bde3-2e08074aa92d",
22
+ # "9223238d-a7e3-2d36-a93d-1e50fea02de2"
23
+ # )
24
+ # # => #<ActiveRecord::Relation ...>
25
+ #
26
+ # Params:
27
+ # <tt>values</tt>:: a list of string uuids values to select records by.
28
+ #
29
+ # Returns the <tt>ActiveRecord::Relation</tt> object.
30
+ #
31
+ def by_uuid(*values)
32
+ first_value = values.first
33
+ list = first_value.is_a?(Array) ? first_value : values
34
+ joins(:uuids).where(uuids_uuids: { value: list }).uniq
35
+ end
36
+ end
37
+
38
+ # Returns the first UUID (sorted as a string) for the record.
39
+ #
40
+ # @example
41
+ # record.uuids.new value: "23423fe3-28d8-a1e5-bde3-2e08074aa92d"
42
+ # record.uuids.new value: "9223238d-a7e3-2d36-a93d-1e50fea02de2"
43
+ # record.uuid # => "23423fe3-28d8-a1e5-bde3-2e08074aa92d"
44
+ #
45
+ def uuid
46
+ uuids.map(&:value).sort.first
47
+ end
48
+
49
+ # Assigns a new uuid to the record.
50
+ #
51
+ # @example
52
+ # record.uuid = "23423fe3-28d8-a1e5-bde3-2e08074aa92d"
53
+ # record.uuid # => "23423fe3-28d8-a1e5-bde3-2e08074aa92d"
54
+ #
55
+ # Params:
56
+ # <tt>value</tt>:: a string value of uuid to assign.
57
+ #
58
+ def uuid=(value)
59
+ uuids.new value: value
60
+ end
61
+
62
+ # Assigns a list of new uuids to the record.
63
+ #
64
+ # @example
65
+ # record.uuids = [
66
+ # "23423fe3-28d8-a1e5-bde3-2e08074aa92d",
67
+ # "9223238d-a7e3-2d36-a93d-1e50fea02de2"
68
+ # ]
69
+ # record.uuids.map(&:value)
70
+ # # => [
71
+ # "23423fe3-28d8-a1e5-bde3-2e08074aa92d",
72
+ # "9223238d-a7e3-2d36-a93d-1e50fea02de2"
73
+ # ]
74
+ #
75
+ # Params:
76
+ # <tt>values</tt>:: an array of uuids string values to assign.
77
+ #
78
+ def uuids=(*values)
79
+ first_value = values.first
80
+ list = first_value.is_a?(Array) ? first_value : values
81
+ list.each { |value| self.uuid = value }
82
+ end
83
+
84
+ private
85
+
86
+ # Creates the uuids by default preventing a record from being ureferrable.
87
+ def add_default_uuid
88
+ uuids.present? || uuids.new
89
+ end
90
+
91
+ # Prevents destruction of a record before its uuids reassigned to
92
+ # other records.
93
+ def prevent_destruction
94
+ return true if uuids.blank?
95
+ errors.add :base, :uuids_present
96
+ false
97
+ end
98
+ end
99
+ end
100
+ end
data/lib/uuids/base.rb CHANGED
@@ -1,3 +1,5 @@
1
+ require_relative "base/has_uuids"
2
+
1
3
  module Uuids
2
4
 
3
5
  # Creates required `#uuids` attribute of the ActiveRecord model.
@@ -13,8 +15,8 @@ module Uuids
13
15
  #
14
16
  # +uuids+:: association attribute.
15
17
  # +uuid+:: method (string).
16
- # <tt>uuid=</tt>:: new uuid's value assignment.
17
- # An alias for <tt>#uuids.new value: value</tt>
18
+ # <tt>uuid=</tt>:: new uuid value assignment.
19
+ # <tt>uuids=</tt>:: a list of uuids values assignment.
18
20
  # <tt>uuid</tt>:: relation scope.
19
21
  #
20
22
  # @example
@@ -32,48 +34,21 @@ module Uuids
32
34
  #
33
35
  # city.uuid = "51f50391-fcd2-4f69-aab7-6ef31b29c379"
34
36
  #
37
+ # city.uuids = [
38
+ # "51f50391-fcd2-4f69-aab7-6ef31b29c379",
39
+ # "3ea8fd89-232f-4ed1-90b5-743da173cd7d"
40
+ # ]
41
+ #
35
42
  # City.by_uuid("51f50391-fcd2-4f69-aab7-6ef31b29c379").to_a == [city]
36
43
  # # => true
37
44
  #
38
45
  def has_uuids
39
- define_uuids
40
- define_uuid_getter
41
- define_uuid_setter
42
- define_scope
43
- end
44
-
45
- # Defines the +uuids+ association attribute;
46
- def define_uuids
47
46
  has_many :uuids, class_name: "Uuids::Uuid", as: :record, validate: false
47
+ include Uuids::Base::HasUuids
48
48
  before_create :add_default_uuid
49
49
  before_destroy :prevent_destruction
50
50
  validates :uuids, presence: true, on: :update
51
51
  end
52
-
53
- # Defines the +uuid+ method
54
- def define_uuid_getter
55
- class_eval "def uuid; uuids.map(&:value).sort.first; end"
56
- end
57
-
58
- # Defines the <tt>uuid=</tt> method
59
- def define_uuid_setter
60
- class_eval "def uuid=(value); uuids.new value: value; end"
61
- end
62
-
63
- # Defines the <tt>uuid</tt> relation scope.
64
- def define_scope
65
- scope(
66
- :by_uuid,
67
- ->(*list) {
68
- if list.first.is_a? Array
69
- warn "[DEPRECATION] use a list of values, not an array" \
70
- " in .by_uuid(*values) method."
71
- list = list.first
72
- end
73
- joins(:uuids).where(uuids_uuids: { value: list }).uniq
74
- }
75
- )
76
- end
77
52
  end
78
53
 
79
54
  private
@@ -84,17 +59,5 @@ module Uuids
84
59
  fail TypeError.new("#{ klass.name } isn't an ActiveRecord model.")
85
60
  end
86
61
  end
87
-
88
- # Creates the uuids by default preventing a record from being ureferrable
89
- def add_default_uuid
90
- uuids.present? || uuids.new
91
- end
92
-
93
- # Prevents destruction of a record before its uuids assigned to another one
94
- def prevent_destruction
95
- return true if uuids.blank?
96
- errors.add :base, :uuids_present
97
- false
98
- end
99
62
  end
100
63
  end
data/lib/uuids/version.rb CHANGED
@@ -2,5 +2,5 @@
2
2
  module Uuids
3
3
 
4
4
  # Current release.
5
- VERSION = "1.2.0"
5
+ VERSION = "1.3.0"
6
6
  end
Binary file
File without changes