class Tent

Provides a way of deferring method calls to an underlying object.

Public Class Methods

cover(underlying, auto_commit = true) { |instance| ... } click to toggle source

Yields a `Tent` over the given `underlying`. By default, commits to the underlying when the block closes.

# File lib/tent.rb, line 11
def self.cover(underlying, auto_commit = true, &block)
  new(underlying).tap do |instance|
    yield instance
    instance.commit! if auto_commit
  end
end
new(underlying) click to toggle source
Calls superclass method
# File lib/tent.rb, line 18
def initialize(underlying)
  # Maintain a reference to the underlying:
  @underlying = underlying
  # Collect calls for the underlying:
  @buffer = []
  # Allow monitor mixin to initialise:
  super()
end

Public Instance Methods

commit!(*filters) click to toggle source

Commits the buffered calls to the underlying.

# File lib/tent.rb, line 39
def commit!(*filters)
  process_buffer(true, filters)
end
direct() click to toggle source

Provide access to the underlying object.

# File lib/tent.rb, line 28
def direct
  @underlying
end
discard!(*filters) click to toggle source

Clears the buffer. Optionally, only clears from the buffer.

# File lib/tent.rb, line 34
def discard!(*filters)
  process_buffer(false, filters)
end

Private Instance Methods

method_missing(method, *args, &block) click to toggle source
Calls superclass method
# File lib/tent.rb, line 78
def method_missing(method, *args, &block)
  return super unless direct.respond_to?(method)

  synchronize do
    @buffer << BufferedCall.new(method, *args, &block)
    self # Make buffering chainable.
  end
end
process_buffer(commit, filters) click to toggle source

Clear from the buffer elements matching any of the `filters`. Will commit those elements to the underlying if `commit` is true.

# File lib/tent.rb, line 67
def process_buffer(commit, filters)
  synchronize do
    @buffer.reject! do |call|
      if call.matched_by?(filters)
        call.apply_to(direct) if commit
        true # Remove from buffer
      end
    end
  end
end