uuids 0.0.1 → 0.1.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: 6fa4c647a7c49ca3c8dc977ef4eecdb8c98d8c65
4
- data.tar.gz: 5ef6df5ecc81765ed887afb31ee8c9c76f065930
3
+ metadata.gz: 151323a9f9d9d1d1e2f0f852ca8dc3008d845022
4
+ data.tar.gz: b6b1d36f0697890239eb7d68f31f807fdb314fbf
5
5
  SHA512:
6
- metadata.gz: e5a8eb30e3fb60615ad8ed955a9978519e73c3aefbb5db98413228fdcdc64f40d6d74d33ed7c35fb7e8c51854fdf38bdd39929dbc942bc3094816b153c0b1ebe
7
- data.tar.gz: 581d68f4bc69eb48b28050f746cc744d89493b6cb156706cbf536767269ec1a16965ffcf37e665ffef58de4a55f73393cd6e43618e74b1aeb08e43f3b0cccbc1
6
+ metadata.gz: 864757381a06c18c1c0eeaa7883199fdf24a9873747aa9a0c736dd0a744a093f7a88a06b797f07f8a82d16405ec571ed00a7f1270a2c2962944e000be0a0b645
7
+ data.tar.gz: aa74d786fb783b687e07d201491a4788d60ab8aeb8357738abef2c674687259274557e05452f9594931bf3949d5a4ed0210ecfdbd0ce5a3dff2a2ad204e8a96e
data/README.rdoc CHANGED
@@ -77,32 +77,42 @@ Or install it yourself as:
77
77
 
78
78
  $ gem install uuids
79
79
 
80
- Then run from a command line in application root:
80
+ === Rails application
81
+
82
+ Run from a command line in application root:
81
83
 
82
84
  $ rake uuids:install
83
85
 
86
+ This will copy gem migrations to a `db/migrate` folder and run it.
87
+
88
+ === Rails mountable engine
89
+
90
+ Run from a command line in application root:
91
+
92
+ $ rake app:uuids:install
93
+
94
+ This will copy gem migrations to a `spec/dummy/db/migrate` folder and run it.
95
+
84
96
  == Usage
85
97
 
86
98
  Add the assotiation to your AR model:
87
99
 
88
100
  class YourModel < ActiveRecord::Base
89
- has_many :uuids, class_name: "Uuids::Uuid", as: :record, dependent: :destroy
90
- ...
91
- end
92
-
93
- Note the <tt>dependent: :destroy</tt> condition! It prevents a record from
94
- destruction before a corresponding uuids reassigned to another record.
95
101
 
96
- Then add a callback for uuid creation:
102
+ # declares the association
103
+ has_many :uuids, class_name: "Uuids::Uuid", as: :record, dependent: :destroy
97
104
 
98
- class YourModel < ActiveRecord::Base
99
- ...
100
- # create the uuid by default
105
+ # auto-generates uuid by default
101
106
  before_create -> { uuids.new }
102
- ...
107
+
108
+ # prevents the record from being unreferable
109
+ validates :uuids, presence: true, unless: :destroy
103
110
  end
104
111
 
105
- Now you can add reference to your model via uuid:
112
+ **Note** the <tt>dependent: :destroy</tt> condition! It requires all uuids
113
+ to be reassigned before destruction.
114
+
115
+ Now you can refer to your model via uuid:
106
116
 
107
117
  class CreateAnotherModelTable < ActiveRecord::Migration
108
118
  def change
@@ -116,20 +126,35 @@ Now you can add reference to your model via uuid:
116
126
  class AnotherModel < ActiveRecord::Base
117
127
 
118
128
  def your_model
119
- @your_model ||= YourModel.join(:uuids).where(uuids: { value: your_model_uuid }).first
129
+ @your_model ||= YourModel.join(:uuids)
130
+ .where(uuids_uuids: { value: your_model_uuid }).first
120
131
  end
121
132
 
122
- def your_model=(value)
123
- @your_model_uuid ||= Uuids::Uuid.find_by_record(value)
133
+ def your_model=(record)
134
+ @your_model_uuid ||= Uuids::Uuid.find_by_record(record).try(:value)
124
135
  end
125
136
  end
126
137
 
127
138
  == Uninstallation
128
139
 
140
+ === Rails app
141
+
129
142
  Run from a command line in application root:
130
143
 
131
144
  $ rake uuids:uninstall
132
145
 
146
+ This will rollback and remove migration from `db/migrate` folder and then
147
+ remove the gem dependencies from application's +Gemfile+ and gemspec.
148
+
149
+ === Rails mountable engine
150
+
151
+ Run from a command line in engine root:
152
+
153
+ $ rake app:uuids:uninstall
154
+
155
+ This will rollback and remove migration from `spec/dummy/db/migrate` folder and
156
+ then remove the gem dependencies from engine's +Gemfile+ and gemspec.
157
+
133
158
  == Contributing
134
159
 
135
160
  1. Fork it ( https://github.com/nepalez/uuids/fork )
data/Rakefile CHANGED
@@ -37,5 +37,6 @@ end
37
37
  task full: [:default] do
38
38
  sh "rubocop"
39
39
  sh "metric_fu"
40
- sh "inch"
40
+ sh "rails_best_practices"
41
+ sh "inch --pedantic"
41
42
  end
@@ -23,6 +23,7 @@ module Uuids
23
23
  #
24
24
  class Uuid < ActiveRecord::Base
25
25
 
26
+ attr_readonly :value
26
27
  belongs_to :record, polymorphic: true
27
28
  validate :record_present?
28
29
  before_destroy :forbid_destruction
@@ -35,9 +36,6 @@ module Uuids
35
36
 
36
37
  private
37
38
 
38
- # Disables value assignment.
39
- attr_writer :value
40
-
41
39
  # Validates record presence with a custom error message.
42
40
  def record_present?
43
41
  return if record
@@ -8,13 +8,17 @@ namespace :uuids do
8
8
  desc "Uninstalls and removes the uuids gem from a Rails application"
9
9
  task :uninstall do
10
10
  sh "rake db:rollback SCOPE=uuids"
11
- say do
11
+ remove_gem
12
+ sh "bundle"
13
+ end
14
+
15
+ def remove_gem
16
+ say "Removing the 'uuids' gem" do
12
17
  remove_from_file "Gemfile", /gem\s+["|']uuids["|']/
13
18
  Dir["*.gemspec"].each do |gemspec|
14
19
  remove_from_file gemspec, /_dependency\s+["|']uuids["|']/
15
20
  end
16
21
  end
17
- sh "bundle"
18
22
  end
19
23
 
20
24
  def remove_from_file(name, regex)
@@ -25,17 +29,20 @@ namespace :uuids do
25
29
  end
26
30
  end
27
31
 
28
- def say
29
- print "== Removing the 'uuids' gem" \
30
- " ===================================================\n"
32
+ def say(name)
33
+ print "== #{ name } #{ "=" * (75 - name.count) }\n"
31
34
  yield
32
35
  print "\n"
33
36
  end
34
37
 
35
38
  def say_with_time(name)
36
- start = Time.now.to_f
39
+ start = seconds
37
40
  print "-- remove from #{ name }\n"
38
41
  yield
39
- print " -> #{ (Time.now.to_f - start).round(4) }s\n"
42
+ print " -> #{ seconds - start }s\n"
43
+ end
44
+
45
+ def seconds
46
+ Time.now.to_f.round(4)
40
47
  end
41
48
  end
data/lib/uuids/version.rb CHANGED
@@ -1,4 +1,6 @@
1
- # Current release.
1
+ # Namespace for a module
2
2
  module Uuids
3
- VERSION = "0.0.1"
3
+
4
+ # Current release.
5
+ VERSION = "0.1.0"
4
6
  end
Binary file