Class: Tent

Inherits:
Object
  • Object
show all
Includes:
MonitorMixin
Defined in:
lib/tent.rb

Overview

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

Defined Under Namespace

Classes: BufferedCall

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(underlying) ⇒ Tent

Returns a new instance of Tent.



18
19
20
21
22
23
24
25
# 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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object (private)



78
79
80
81
82
83
84
85
# 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

Class Method Details

.cover(underlying, auto_commit = true, &block) ⇒ Object

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



11
12
13
14
15
16
# 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

Instance Method Details

#commit!(*filters) ⇒ Object

Commits the buffered calls to the underlying.



39
40
41
# File 'lib/tent.rb', line 39

def commit!(*filters)
  process_buffer(true, filters)
end

#directObject

Provide access to the underlying object.



28
29
30
# File 'lib/tent.rb', line 28

def direct
  @underlying
end

#discard!(*filters) ⇒ Object

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



34
35
36
# File 'lib/tent.rb', line 34

def discard!(*filters)
  process_buffer(false, filters)
end