active_fedora-noid 2.0.1 → 2.0.2

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: 77f3bb9f61240306a75e00c8c4fc7d4c314110d7
4
- data.tar.gz: 5310a81c3a8b3573d9dd756263de8a5ada773f47
3
+ metadata.gz: e58687a86fa490783729234d0a1db375c494faed
4
+ data.tar.gz: c37de3f330b5cc4c0a0f429ff1c6225422d5f4e4
5
5
  SHA512:
6
- metadata.gz: a95ae3bae159006fdf2c89257b939693a77f92a7ee1c3e948e05164e703d7757219bb8a4d95557c74d87c0cb498f3605053398669dbb6886a8cf9fe6c1e1a88c
7
- data.tar.gz: e9b5e44da2b1d277891aa75b6e0f3b353e740da925ab53a67f144032ac88995b2d65be96204f70414b6d303e05c72bc3ef02ca590d371da0bd7a0fb3b718170d
6
+ metadata.gz: 7454c2ca35895fbb2adebeddde58876e910e885705a633ffc322397c7b2d80dc3e48978b5cd9c10c85954f4c43d6c4b05441b7bee71f0de98c3d414567764206
7
+ data.tar.gz: 29039bd1d5da0854797832c3761f5ccbe1aebc46922625c27c9167b213d764c1c8bf4b244369eca286e6c83de3fa34bea62d7d1d68cf359e3fb52b4f8b0fa46f
data/README.md CHANGED
@@ -99,7 +99,7 @@ This will make sure your objects have Noid-like identifiers (e.g. `bb22bb22b`) t
99
99
 
100
100
  ## Overriding default behavior
101
101
 
102
- The default minter in ActiveFedora::Noid 2.0.0 is the file-backed minter to preserve default behavior.
102
+ The default minter in ActiveFedora::Noid 2.x is the file-backed minter to preserve default behavior.
103
103
 
104
104
  To better support multi-host production installations that expect a shared database but not necessarily a shared filesystem (e.g., between load-balanced Rails applications), we highly recommend swapping in the database-backed minter.
105
105
 
@@ -111,7 +111,11 @@ The database-based minter stores minter state information in your application's
111
111
  $ rails generate active_fedora:noid:install
112
112
  ```
113
113
 
114
- This will create the necessary database tables and seed the database minter. To start minting identifiers with the new minter, override the AF::Noid configuration in e.g. `config/initializers/active_fedora-noid.rb`:
114
+ This will create the necessary database migrations.
115
+
116
+ Then run `rake db:migrate`
117
+
118
+ To start minting identifiers with the new minter, override the AF::Noid configuration in e.g. `config/initializers/active_fedora-noid.rb`:
115
119
 
116
120
  ```ruby
117
121
  require 'active_fedora/noid'
@@ -134,7 +138,7 @@ RSpec.configure do |config|
134
138
  end
135
139
  ```
136
140
 
137
- If you switch to the new database-backed minter and want to include in that minter the state of your current file-backed minter, AF::Noid 2.0.0 provides a new rake task that will copy your minter's state from the filesystem to the database:
141
+ If you switch to the new database-backed minter and want to include in that minter the state of your current file-backed minter, AF::Noid 2.x provides a new rake task that will copy your minter's state from the filesystem to the database:
138
142
 
139
143
  ```bash
140
144
  # For migrating minter state from a file to a database
@@ -23,7 +23,7 @@ Gem::Specification.new do |spec|
23
23
  spec.add_dependency 'rails', '>= 4.2.7.1', '< 6'
24
24
 
25
25
  spec.add_development_dependency 'bundler', '~> 1.7'
26
- spec.add_development_dependency 'rake', '~> 11.0'
26
+ spec.add_development_dependency 'rake', '>= 11'
27
27
  spec.add_development_dependency 'rspec', '~> 3.2'
28
28
  spec.add_development_dependency 'sqlite3'
29
29
  spec.add_development_dependency 'rubocop', '~> 0.42.0'
@@ -8,15 +8,13 @@ module ActiveFedora
8
8
  class Base < ::Noid::Minter
9
9
  ##
10
10
  # @param template [#to_s] a NOID template
11
- #
12
11
  # @see Noid::Template
13
12
  def initialize(template = default_template)
14
13
  super(template: template.to_s)
15
14
  end
16
15
 
17
16
  ##
18
- # Sychronously mint a new identifier. Guarantees the ID is not
19
- # already reserved in ActiveFedora.
17
+ # Sychronously mint a new identifier. Guarantees the ID is not already reserved in ActiveFedora.
20
18
  #
21
19
  # @return [String] the minted identifier
22
20
  def mint
@@ -29,7 +27,7 @@ module ActiveFedora
29
27
  end
30
28
 
31
29
  ##
32
- # @return [Hash] an object representing the current minter state
30
+ # @return [Hash{Symbol => String, Object}] representation of the current minter state
33
31
  def read
34
32
  raise NotImplementedError, 'Implement #read in child class'
35
33
  end
@@ -38,7 +36,6 @@ module ActiveFedora
38
36
  # Updates the minter state to that of the `minter` parameter.
39
37
  #
40
38
  # @param minter [Minter::Base]
41
- #
42
39
  # @return [void]
43
40
  def write!(_)
44
41
  raise NotImplementedError, 'Implement #write! in child class'
@@ -53,7 +50,7 @@ module ActiveFedora
53
50
  end
54
51
 
55
52
  ##
56
- # @return [String] a new identifier.
53
+ # @return [String] a new identifier
57
54
  def next_id
58
55
  raise NotImplementedError, 'Implement #next_id in child class'
59
56
  end
@@ -6,27 +6,40 @@ module ActiveFedora
6
6
  module Minter
7
7
  class Db < Base
8
8
  def read
9
- filtered_hash = instance.as_json.select { |key| %w(template counters seq rand namespace).include?(key) }
9
+ deserialize(instance)
10
+ end
11
+
12
+ def write!(minter)
13
+ serialize(instance, minter)
14
+ end
15
+
16
+ protected
17
+
18
+ # @param [MinterState] inst minter state to be converted
19
+ # @return [Hash{Symbol => String, Object}] minter state as a Hash, like #read
20
+ # @see #read, ActiveFedora::Noid::Minter::Base#read
21
+ def deserialize(inst)
22
+ filtered_hash = inst.as_json.slice(*%w(template counters seq rand namespace))
10
23
  filtered_hash['counters'] = JSON.parse(filtered_hash['counters'], symbolize_names: true) if filtered_hash['counters']
11
24
  filtered_hash.symbolize_keys
12
25
  end
13
26
 
14
- def write!(minter)
27
+ # @param [MinterState] inst a locked row/object to be updated
28
+ # @param [::Noid::Minter] minter state containing the updates
29
+ def serialize(inst, minter)
15
30
  # namespace and template are the same, now update the other attributes
16
- instance.update_attributes!(
31
+ inst.update_attributes!(
17
32
  seq: minter.seq,
18
33
  counters: JSON.generate(minter.counters),
19
34
  rand: Marshal.dump(minter.instance_variable_get(:@rand))
20
35
  )
21
36
  end
22
37
 
23
- protected
24
-
25
38
  # Uses pessimistic lock to ensure the record fetched is the same one updated.
26
39
  # Should be fast enough to avoid terrible deadlock.
27
40
  # Must lock because of multi-connection context! (transaction is per connection -- not enough)
28
41
  # The DB table will only ever have at most one row per namespace.
29
- # The 'default' namespace row is inserted by `rails generate active_fedora:noid:seed`.
42
+ # The 'default' namespace row is inserted by `rails generate active_fedora:noid:seed` or autofilled by instance below.
30
43
  # If you want another namespace, edit your config initialzer to something like:
31
44
  # ActiveFedora::Noid.config.namespace = 'druid'
32
45
  # ActiveFedora::Noid.config.template = '.reeedek'
@@ -35,14 +48,15 @@ module ActiveFedora
35
48
  def next_id
36
49
  id = nil
37
50
  MinterState.transaction do
38
- state = read
39
- minter = ::Noid::Minter.new(state)
51
+ locked = instance
52
+ minter = ::Noid::Minter.new(deserialize(locked))
40
53
  id = minter.mint
41
- write!(minter)
54
+ serialize(locked, minter)
42
55
  end # transaction
43
56
  id
44
57
  end
45
58
 
59
+ # @return [MinterState]
46
60
  def instance
47
61
  MinterState.lock.find_by!(
48
62
  namespace: ActiveFedora::Noid.config.namespace,
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
  module ActiveFedora
3
3
  module Noid
4
- VERSION = '2.0.1'
4
+ VERSION = '2.0.2'
5
5
  end
6
6
  end
@@ -14,11 +14,6 @@ Copies DB migrations
14
14
 
15
15
  def migrations
16
16
  rake 'active_fedora_noid_engine:install:migrations'
17
- rake 'db:migrate'
18
- end
19
-
20
- def seed
21
- generate 'active_fedora:noid:seed'
22
17
  end
23
18
  end
24
19
  end
data/spec/spec_helper.rb CHANGED
@@ -20,4 +20,5 @@ RSpec.configure do |config|
20
20
  config.mock_with :rspec do |mocks|
21
21
  mocks.verify_partial_doubles = true
22
22
  end
23
+ config.filter_run_when_matching :focus
23
24
  end
@@ -14,14 +14,9 @@ shared_examples 'a minter' do
14
14
  expect(described_class.new.valid?(subject)).to be true
15
15
  end
16
16
 
17
- it 'is invalid under a different template' do
18
- expect(described_class.new('.reedddk').valid?(subject)).to be false
19
- end
20
-
21
17
  context 'with a different template' do
22
- let(:other) { described_class.new('.reedddk') }
23
- it 'is invalid under a different template' do
24
- expect(other).not_to be_valid(minter.mint)
18
+ it 'is invalid' do
19
+ expect(described_class.new('.reedddk').valid?(subject)).to be false
25
20
  end
26
21
  end
27
22
  end
@@ -10,5 +10,6 @@ class TestAppGenerator < Rails::Generators::Base
10
10
 
11
11
  def install_engine
12
12
  generate 'active_fedora:noid:install'
13
+ rake 'db:migrate'
13
14
  end
14
15
  end
@@ -64,13 +64,23 @@ describe ActiveFedora::Noid::Minter::Db do
64
64
  let(:starting_state) { db_minter.read }
65
65
  let(:minter) { Noid::Minter.new(starting_state) }
66
66
  before { minter.mint }
67
+
67
68
  it 'changes the state of the minter' do
68
- expect { db_minter.write!(minter) }.to change { db_minter.read[:seq] }
69
- .from(starting_state[:seq]).to(minter.seq)
70
- .and change { db_minter.read[:counters] }
71
- .from(starting_state[:counters]).to(minter.counters)
72
- .and change { db_minter.read[:rand] }
73
- .from(starting_state[:rand]).to(Marshal.dump(minter.instance_variable_get(:@rand)))
69
+ expect { db_minter.write!(minter) }
70
+ .to change { db_minter.read[:seq] }.from(starting_state[:seq]).to(minter.seq)
71
+ .and change { db_minter.read[:counters] }.from(starting_state[:counters]).to(minter.counters)
72
+ .and change { db_minter.read[:rand] }.from(starting_state[:rand]).to(Marshal.dump(minter.instance_variable_get(:@rand)))
73
+ end
74
+ end
75
+
76
+ describe '#next_id' do
77
+ let(:stub_minter) { described_class.new }
78
+ let(:locked) { MinterState }
79
+
80
+ it 'locks DB row and does not query twice' do
81
+ expect(MinterState).to receive(:lock).and_return(locked).once
82
+ expect(locked).to receive(:find_by!).once.and_return(MinterState.first)
83
+ stub_minter.send(:next_id)
74
84
  end
75
85
  end
76
86
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_fedora-noid
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.1
4
+ version: 2.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael J. Giarlo
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-01-09 00:00:00.000000000 Z
11
+ date: 2017-03-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: active-fedora
@@ -82,16 +82,16 @@ dependencies:
82
82
  name: rake
83
83
  requirement: !ruby/object:Gem::Requirement
84
84
  requirements:
85
- - - "~>"
85
+ - - ">="
86
86
  - !ruby/object:Gem::Version
87
- version: '11.0'
87
+ version: '11'
88
88
  type: :development
89
89
  prerelease: false
90
90
  version_requirements: !ruby/object:Gem::Requirement
91
91
  requirements:
92
- - - "~>"
92
+ - - ">="
93
93
  - !ruby/object:Gem::Version
94
- version: '11.0'
94
+ version: '11'
95
95
  - !ruby/object:Gem::Dependency
96
96
  name: rspec
97
97
  requirement: !ruby/object:Gem::Requirement
@@ -227,7 +227,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
227
227
  version: '0'
228
228
  requirements: []
229
229
  rubyforge_project:
230
- rubygems_version: 2.6.8
230
+ rubygems_version: 2.5.1
231
231
  signing_key:
232
232
  specification_version: 4
233
233
  summary: Noid identifier services for ActiveFedora-based applications