Class: Hyrax::EmbargoManager

Inherits:
Object
  • Object
show all
Defined in:
app/services/hyrax/embargo_manager.rb

Overview

Provides utilities for managing the lifecycle of an `Hyrax::Embargo` on a `Hyrax::Resource`.

The embargo terminology used here is as follows:

- "Release Date" is the day an embargo is scheduled to be released.
- "Under Embargo" means the embargo is "active"; i.e. that its release
   date is today or later.
- "Applied" means the embargo's pre-release visibility has been set on
  the resource.
- "Released" means the embargo's post-release visibility has been set on
  the resource.
- "Enforced" means the object's visibility matches the pre-release
  visibility of the embargo; i.e. the embargo has been applied,
  but not released.

Note that an resource may be `#under_embargo?` even if the embargo is not be `#enforced?` (in this case, the application should seek to apply the embargo, e.g. via a scheduled job). Additionally, an embargo may be `#enforced?` after its release date (in this case, the application should seek to release the embargo).

Examples:

check whether a resource is under an active embargo

manager = EmbargoManager.new(resource: my_resource)
manager.under_embargo? # => false

applying an embargo

embargo = Hyrax::Embargo.new(visibility_during_embargo: 'restricted',
                             visibility_after_embargo:  'open',
                             embargo_release_date:      Time.zone.today + 1000)

resource            = Hyrax::Resource.new(embargo: embargo)
resource.visibility = 'open'

manager = EmbargoManager.new(resource: resource)

manager.apply!
manager.enforced? => true
resource.visibility # => 'restricted'

releasing an embargo

embargo = Hyrax::Embargo.new(visibility_during_embargo: 'restricted',
                             visibility_after_embargo:  'open',
                             embargo_release_date:      Time.zone.today + 1000)

releasing an embargo

embargo = Hyrax::Embargo.new(visibility_during_embargo: 'restricted',
                             visibility_after_embargo:  'open',
                             embargo_release_date:      Time.zone.today + 1)

resource = Hyrax::Resource.new(embargo: embargo)
manager  = EmbargoManager.new(resource: resource)

manager.under_embargo? => true
manager.enforced? => false

manager.apply!

resource.visibility # => 'restricted'
manager.enforced? => true

manager.release! # => NotReleasableError

# <spongebob narrator>ONE DAY LATER</spongebob narrator>
manager.under_embargo? => false
manager.enforced? => true

manager.release!

resource.visibility # => 'open'
manager.enforced? => false

Defined Under Namespace

Classes: NotEnforcableError, NotReleasableError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource:, query_service: Hyrax.query_service) ⇒ EmbargoManager

Returns a new instance of EmbargoManager.

Parameters:


90
91
92
93
# File 'app/services/hyrax/embargo_manager.rb', line 90

def initialize(resource:, query_service: Hyrax.query_service)
  @query_service = query_service
  self.resource  = resource
end

Instance Attribute Details

#query_serviceObject (readonly)

Returns the value of attribute query_service


86
87
88
# File 'app/services/hyrax/embargo_manager.rb', line 86

def query_service
  @query_service
end

#resourceHyrax::Resource

Returns:


81
82
83
# File 'app/services/hyrax/embargo_manager.rb', line 81

def resource
  @resource
end

Class Method Details

.apply_embargo_for(resource:, query_service: Hyrax.query_service) ⇒ Object


96
97
98
99
# File 'app/services/hyrax/embargo_manager.rb', line 96

def apply_embargo_for(resource:, query_service: Hyrax.query_service)
  new(resource: resource, query_service: query_service)
    .apply
end

.apply_embargo_for!(resource:, query_service: Hyrax.query_service) ⇒ Object


101
102
103
104
# File 'app/services/hyrax/embargo_manager.rb', line 101

def apply_embargo_for!(resource:, query_service: Hyrax.query_service)
  new(resource: resource, query_service: query_service)
    .apply!
end

.embargo_for(resource:, query_service: Hyrax.query_service) ⇒ Object


106
107
108
109
# File 'app/services/hyrax/embargo_manager.rb', line 106

def embargo_for(resource:, query_service: Hyrax.query_service)
  new(resource: resource, query_service: query_service)
    .embargo
end

.release_embargo_for(resource:, query_service: Hyrax.query_service) ⇒ Object


111
112
113
114
# File 'app/services/hyrax/embargo_manager.rb', line 111

def release_embargo_for(resource:, query_service: Hyrax.query_service)
  new(resource: resource, query_service: query_service)
    .release
end

.release_embargo_for!(resource:, query_service: Hyrax.query_service) ⇒ Object


116
117
118
119
# File 'app/services/hyrax/embargo_manager.rb', line 116

def release_embargo_for!(resource:, query_service: Hyrax.query_service)
  new(resource: resource, query_service: query_service)
    .release!
end

Instance Method Details

#applyBoolean

Sets the visibility of the resource to the embargo's visibility condition

Returns:

  • (Boolean)

139
140
141
142
143
# File 'app/services/hyrax/embargo_manager.rb', line 139

def apply
  return false unless under_embargo?

  resource.visibility = embargo.visibility_during_embargo
end

#apply!void

This method returns an undefined value.

Raises:


148
149
150
# File 'app/services/hyrax/embargo_manager.rb', line 148

def apply!
  apply || raise(NotEnforcableError)
end

#copy_embargo_to(target:) ⇒ Boolean

Copies and applies the embargo to a new (target) resource.

Parameters:

Returns:

  • (Boolean)

128
129
130
131
132
133
# File 'app/services/hyrax/embargo_manager.rb', line 128

def copy_embargo_to(target:)
  return false unless under_embargo?

  target.embargo = Embargo.new(clone_attributes)
  self.class.apply_embargo_for(resource: target)
end

#embargoHyrax::Embargo

Returns:


160
161
162
# File 'app/services/hyrax/embargo_manager.rb', line 160

def embargo
  resource.embargo || Embargo.new
end

#enforced?Boolean

Returns:

  • (Boolean)

154
155
156
# File 'app/services/hyrax/embargo_manager.rb', line 154

def enforced?
  embargo.visibility_during_embargo.to_s == resource.visibility
end

#releaseBoolean

Sets the visibility of the resource to the embargo's visibility condition. no-op if the embargo period is current.

Returns:

  • (Boolean)

    truthy if the embargo has been applied


169
170
171
172
173
174
# File 'app/services/hyrax/embargo_manager.rb', line 169

def release
  return false if under_embargo?
  return true if embargo.visibility_after_embargo.nil?

  resource.visibility = embargo.visibility_after_embargo
end

#release!void

This method returns an undefined value.

Raises:


180
181
182
# File 'app/services/hyrax/embargo_manager.rb', line 180

def release!
  release || raise(NotReleasableError)
end

#under_embargo?Boolean

Returns indicates whether the date range for the embargo's applicability includes the present date.

Returns:

  • (Boolean)

    indicates whether the date range for the embargo's applicability includes the present date.


187
188
189
# File 'app/services/hyrax/embargo_manager.rb', line 187

def under_embargo?
  embargo.active?
end