Class: Farce::Abstract::Lease Abstract

Inherits:
Object
  • Object
show all
Includes:
Internal::Noncopyable, Internal::Inspect
Defined in:
lib/farce/abstract/lease.rb

Overview

This class is abstract.

Shared checkout, return, ownership, and retirement behavior for leases.

Direct Known Subclasses

Lease, Local::Lease, Strict::Lease, Unshared::Lease

Instance Method Summary collapse

Methods included from Internal::Noncopyable

#duplicable?

Constructor Details

#initialize { ... } ⇒ Lease

Construct a lease from one resource-building block.

Yields:

  • builds the resource managed by the lease

Yield Returns:

  • (BasicObject) —

    the initial resource

Raises:

  • (ArgumentError) —

    if no block is given or the resource is nil or boolean



16
17
18
19
20
21
# File 'lib/farce/abstract/lease.rb', line 16

def initialize
  raise ArgumentError, "a resource constructor block is required" unless block_given?

  @lease = new_internal_lease(yield)
  super
end

Instance Method Details

#available? ⇒ Boolean

Returns whether the resource is available for checkout.

Returns:

  • (Boolean) —

    whether the resource is available for checkout



67
# File 'lib/farce/abstract/lease.rb', line 67

def available? = internal_lease.available?

#checked_out? ⇒ Boolean

Returns whether the resource is checked out.

Returns:

  • (Boolean) —

    whether the resource is checked out



70
# File 'lib/farce/abstract/lease.rb', line 70

def checked_out? = internal_lease.checked_out?

#checkin(resource) ⇒ self

Return an explicitly checked-out resource or a replacement.

Parameters:

  • resource (BasicObject) —

    the resource to store

Returns:

  • (self)

Raises:



49
50
51
52
# File 'lib/farce/abstract/lease.rb', line 49

def checkin(resource)
  internal_lease.checkin(resource)
  self
end

#checkout(timeout: nil) {|resource| ... } ⇒ BasicObject

Acquire the resource, waiting until it is available.

Parameters:

  • timeout (Numeric, nil) (defaults to: nil) —

    maximum seconds to wait

Yield Parameters:

  • resource (BasicObject) —

    the acquired resource

Returns:

  • (BasicObject) —

    the resource without a block, or the block result

Raises:



29
30
31
32
# File 'lib/farce/abstract/lease.rb', line 29

def checkout(timeout: nil, &block)
  lease = internal_lease
  block ? lease.checkout(timeout:, &block) : lease.checkout(timeout:)
end

#owned? ⇒ Boolean

Returns whether the current Fiber owns the checkout.

Returns:

  • (Boolean) —

    whether the current Fiber owns the checkout



73
# File 'lib/farce/abstract/lease.rb', line 73

def owned? = internal_lease.owned?

#retire ⇒ self

Permanently retire this lease and wake its waiters.

The current Fiber must own the checkout. Retirement ends that checkout, so automatic block cleanup does not return the resource. The resource is not closed or otherwise disposed. Calling this again after retirement is safe.

Returns:

  • (self)

Raises:



61
62
63
64
# File 'lib/farce/abstract/lease.rb', line 61

def retire
  internal_lease.retire
  self
end

#retired? ⇒ Boolean

Returns whether the lease has been permanently retired.

Returns:

  • (Boolean) —

    whether the lease has been permanently retired



76
# File 'lib/farce/abstract/lease.rb', line 76

def retired? = internal_lease.retired?

#try_checkout {|resource| ... } ⇒ BasicObject?

Acquire the resource immediately if it is available.

Yield Parameters:

  • resource (BasicObject) —

    the acquired resource

Returns:

  • (BasicObject, nil) —

    the resource or block result, or nil when unavailable

Raises:



38
39
40
41
# File 'lib/farce/abstract/lease.rb', line 38

def try_checkout(&block)
  lease = internal_lease
  block ? lease.try_checkout(&block) : lease.try_checkout
end