Class: Hyrax::LeaseManager

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

Overview

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

The lease terminology used here is as follows:

- "Expiration Date" is the day a lease is scheduled to expire.
- "Under Lease" means the lease is "active"; i.e. that its expiration
   date is today or later.
- "Applied" means the lease's pre-expiration visibility has been set on
  the resource.
- "Enforced" means the object's visibility matches the pre-expiration
  visibility of the lease; i.e. the lease has been applied,
  but not released.
- "Released" means the leases's post-release visibility has been set on
  the resource.
- "Deactivate" means that the existing lease will be removed, even if it
  is active

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) ⇒ LeaseManager

Returns a new instance of LeaseManager.

Parameters:



36
37
38
39
# File 'app/services/hyrax/lease_manager.rb', line 36

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.



32
33
34
# File 'app/services/hyrax/lease_manager.rb', line 32

def query_service
  @query_service
end

#resourceHyrax::Resource

Returns:



27
28
29
# File 'app/services/hyrax/lease_manager.rb', line 27

def resource
  @resource
end

Class Method Details

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



42
43
44
45
# File 'app/services/hyrax/lease_manager.rb', line 42

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

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



47
48
49
50
# File 'app/services/hyrax/lease_manager.rb', line 47

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

.create_or_update_lease_on_members(members, work) ⇒ Object

Creates or updates an existing lease on a member to match the lease on the parent work rubocop:disable Metrics/AbcSize, Metrics/MethodLength

Parameters:

  • members (Array<Valkyrie::Resource>)
  • work (Hyrax::Work)


85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'app/services/hyrax/lease_manager.rb', line 85

def create_or_update_lease_on_members(members, work)
  # TODO: account for all members and levels, not just file sets. ref: #6131

  members.each do |member|
    member_lease_needs_updating = work.lease.updated_at > member.lease&.updated_at if member.lease

    if member.lease && member_lease_needs_updating
      member.lease.lease_expiration_date = work.lease['lease_expiration_date']
      member.lease.visibility_during_lease = work.lease['visibility_during_lease']
      member.lease.visibility_after_lease = work.lease['visibility_after_lease']
      member.lease = Hyrax.persister.save(resource: member.lease)
    else
      work_lease_manager = Hyrax::LeaseManager.new(resource: work)
      work_lease_manager.copy_lease_to(target: member)
      member = Hyrax.persister.save(resource: member)
    end

    user ||= ::User.find_by_user_key(member.depositor)
    # the line below works in that it indexes the file set with the necessary lease properties
    # I do not know however if this is the best event_id to pass
    Hyrax.publisher.publish('object.metadata.updated', object: member, user: user)
  end
end

.deactivate_lease_for(resource:, query_service: Hyrax.query_service) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
61
# File 'app/services/hyrax/lease_manager.rb', line 58

def deactivate_lease_for(resource:, query_service: Hyrax.query_service)
  new(resource: resource, query_service: query_service)
    .deactivate
end

.deactivate_lease_for!(resource:, query_service: Hyrax.query_service) ⇒ Boolean

Returns:

  • (Boolean)


64
65
66
67
# File 'app/services/hyrax/lease_manager.rb', line 64

def deactivate_lease_for!(resource:, query_service: Hyrax.query_service)
  new(resource: resource, query_service: query_service)
    .deactivate!
end

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



52
53
54
55
# File 'app/services/hyrax/lease_manager.rb', line 52

def lease_for(resource:, query_service: Hyrax.query_service)
  new(resource: resource, query_service: query_service)
    .lease
end

.release_lease_for(resource:, query_service: Hyrax.query_service) ⇒ Boolean

Returns:

  • (Boolean)


70
71
72
73
# File 'app/services/hyrax/lease_manager.rb', line 70

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

.release_lease_for!(resource:, query_service: Hyrax.query_service) ⇒ Boolean

Returns:

  • (Boolean)


76
77
78
79
# File 'app/services/hyrax/lease_manager.rb', line 76

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

Instance Method Details

#applyBoolean

Returns:

  • (Boolean)


137
138
139
140
141
# File 'app/services/hyrax/lease_manager.rb', line 137

def apply
  return false unless under_lease?

  resource.visibility = lease.visibility_during_lease
end

#apply!void

This method returns an undefined value.

Raises:



146
147
148
# File 'app/services/hyrax/lease_manager.rb', line 146

def apply!
  apply || raise(NotEnforcableError)
end

#copy_lease_to(target:) ⇒ Boolean

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

Parameters:

Returns:

  • (Boolean)


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

def copy_lease_to(target:)
  return false unless under_lease?

  target.lease = Hyrax.persister.save(resource: Lease.new(clone_attributes))
  self.class.apply_lease_for(resource: target)
end

#deactivateObject



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

def deactivate
  release(force: true) &&
    nullify(force: true)
end

#deactivate!Object

Deactivates the lease and logs a message to the lease_history property



117
118
119
120
# File 'app/services/hyrax/lease_manager.rb', line 117

def deactivate!
  release(force: true)
  nullify(force: true)
end

#enforced?Boolean

Returns:

  • (Boolean)


152
153
154
155
# File 'app/services/hyrax/lease_manager.rb', line 152

def enforced?
  lease.lease_expiration_date.present? &&
    lease.visibility_during_lease.to_s == resource.visibility
end

#leaseHyrax::Lease

Returns:



159
160
161
# File 'app/services/hyrax/lease_manager.rb', line 159

def lease
  resource.lease || Lease.new
end

#nullify(force: false) ⇒ Boolean

Drop the lease by setting its release date and visibility settings to ‘nil`.

Parameters:

  • force (Boolean) (defaults to: false)

    force the nullify even when the lease period is current

Returns:

  • (Boolean)


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

def nullify(force: false)
  return false if !force && under_lease?

  lease.lease_expiration_date = nil
  lease.visibility_during_lease = nil
  lease.visibility_after_lease = nil
  true
end

#release(force: false) ⇒ Boolean

Sets the visibility of the resource to the lease’s after lease visibility. no-op if the lease period is current and the force flag is false.

Parameters:

  • force (boolean) (defaults to: false)

    force the release even when the lease period is current

Returns:

  • (Boolean)


184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'app/services/hyrax/lease_manager.rb', line 184

def release(force: false)
  return false if !force && under_lease?

  lease_state = lease.active? ? 'active' : 'expired'
  history_record = lease_history_message(
    lease_state,
    Hyrax::TimeService.time_in_utc,
    lease.lease_expiration_date,
    lease.visibility_during_lease,
    lease.visibility_after_lease
  )

  lease.lease_history += [history_record]

  return true if lease.visibility_after_lease.nil?

  resource.visibility = lease.visibility_after_lease
end

#release!void

This method returns an undefined value.



205
206
207
# File 'app/services/hyrax/lease_manager.rb', line 205

def release!
  release || raise(NotReleasableError)
end

#under_lease?Boolean

Returns:

  • (Boolean)


211
212
213
# File 'app/services/hyrax/lease_manager.rb', line 211

def under_lease?
  lease.active?
end