@@ -5,7 +5,24 @@ module Hyrax
5
5
# Provides utilities for managing the lifecycle of an `Hyrax::Embargo` on a
6
6
# `Hyrax::Resource`.
7
7
#
8
- # This can do things like
8
+ # The embargo terminology used here is as follows:
9
+ #
10
+ # - "Release Date" is the day an embargo is scheduled to be released.
11
+ # - "Under Embargo" means the embargo is "active"; i.e. that its release
12
+ # date is today or later.
13
+ # - "Applied" means the embargo's pre-release visibility has been set on
14
+ # the resource.
15
+ # - "Released" means the embargo's post-release visibility has been set on
16
+ # the resource.
17
+ # - "Enforced" means the object's visibility matches the pre-release
18
+ # visibility of the embargo; i.e. the embargo has been applied,
19
+ # but not released.
20
+ #
21
+ # Note that an resource may be `#under_embargo?` even if the embargo is not
22
+ # be `#enforced?` (in this case, the application should seek to apply the
23
+ # embargo, e.g. via a scheduled job). Additionally, an embargo may be
24
+ # `#enforced?` after its release date (in this case, the application should
25
+ # seek to release the embargo).
9
26
#
10
27
# @example check whether a resource is under an active embargo
11
28
# manager = EmbargoManager.new(resource: my_resource)
@@ -21,9 +38,42 @@ module Hyrax
21
38
#
22
39
# manager = EmbargoManager.new(resource: resource)
23
40
#
24
- # manager.apply
41
+ # manager.apply!
42
+ # manager.enforced? => true
25
43
# resource.visibility # => 'restricted'
26
44
#
45
+ # @example releasing an embargo
46
+ # embargo = Hyrax::Embargo.new(visibility_during_embargo: 'restricted',
47
+ # visibility_after_embargo: 'open',
48
+ # embargo_release_date: Time.zone.today + 1000)
49
+ #
50
+ # @example releasing an embargo
51
+ # embargo = Hyrax::Embargo.new(visibility_during_embargo: 'restricted',
52
+ # visibility_after_embargo: 'open',
53
+ # embargo_release_date: Time.zone.today + 1)
54
+ #
55
+ # resource = Hyrax::Resource.new(embargo: embargo)
56
+ # manager = EmbargoManager.new(resource: resource)
57
+ #
58
+ # manager.under_embargo? => true
59
+ # manager.enforced? => false
60
+ #
61
+ # manager.apply!
62
+ #
63
+ # resource.visibility # => 'restricted'
64
+ # manager.enforced? => true
65
+ #
66
+ # manager.release! # => NotReleasableError
67
+ #
68
+ # # <spongebob narrator>ONE DAY LATER</spongebob narrator>
69
+ # manager.under_embargo? => false
70
+ # manager.enforced? => true
71
+ #
72
+ # manager.release!
73
+ #
74
+ # resource.visibility # => 'open'
75
+ # manager.enforced? => false
76
+ #
27
77
class EmbargoManager
28
78
##
29
79
# @!attribute [rw] resource
@@ -48,10 +98,25 @@ def apply_embargo_for(resource:, query_service: Hyrax.query_service)
48
98
. apply
49
99
end
50
100
101
+ def apply_embargo_for! ( resource :, query_service : Hyrax . query_service )
102
+ new ( resource : resource , query_service : query_service )
103
+ . apply!
104
+ end
105
+
51
106
def embargo_for ( resource :, query_service : Hyrax . query_service )
52
107
new ( resource : resource , query_service : query_service )
53
108
. embargo
54
109
end
110
+
111
+ def release_embargo_for ( resource :, query_service : Hyrax . query_service )
112
+ new ( resource : resource , query_service : query_service )
113
+ . release
114
+ end
115
+
116
+ def release_embargo_for! ( resource :, query_service : Hyrax . query_service )
117
+ new ( resource : resource , query_service : query_service )
118
+ . release!
119
+ end
55
120
end
56
121
57
122
##
@@ -68,6 +133,8 @@ def copy_embargo_to(target:)
68
133
end
69
134
70
135
##
136
+ # Sets the visibility of the resource to the embargo's visibility condition
137
+ #
71
138
# @return [Boolean]
72
139
def apply
73
140
return false unless under_embargo?
@@ -76,17 +143,54 @@ def apply
76
143
end
77
144
78
145
##
79
- # @return [Valkyrie::Resource]
146
+ # @return [void]
147
+ # @raise [NotEnforcableError] when trying to apply an embargo that isn't active
148
+ def apply!
149
+ apply || raise ( NotEnforcableError )
150
+ end
151
+
152
+ ##
153
+ # @return [Boolean]
154
+ def enforced?
155
+ embargo . visibility_during_embargo . to_s == resource . visibility
156
+ end
157
+
158
+ ##
159
+ # @return [Hyrax::Embargo]
80
160
def embargo
81
161
resource . embargo || Embargo . new
82
162
end
83
163
84
164
##
85
- # @return [Boolean]
165
+ # Sets the visibility of the resource to the embargo's visibility condition.
166
+ # no-op if the embargo period is current.
167
+ #
168
+ # @return [Boolean] truthy if the embargo has been applied
169
+ def release
170
+ return false if under_embargo?
171
+ return true if embargo . visibility_after_embargo . nil?
172
+
173
+ resource . visibility = embargo . visibility_after_embargo
174
+ end
175
+
176
+ ##
177
+ # @return [void]
178
+ # @raise [NotEnforcableError] when trying to release an embargo that
179
+ # is currently active
180
+ def release!
181
+ release || raise ( NotReleasableError )
182
+ end
183
+
184
+ ##
185
+ # @return [Boolean] indicates whether the date range for the embargo's
186
+ # applicability includes the present date.
86
187
def under_embargo?
87
188
embargo . active?
88
189
end
89
190
191
+ class NotEnforcableError < RuntimeError ; end
192
+ class NotReleasableError < RuntimeError ; end
193
+
90
194
private
91
195
92
196
def clone_attributes
0 commit comments