Class: Pants::Writers::BaseWriter

Inherits:
Object
  • Object
show all
Defined in:
lib/pants/writers/base_writer.rb

Overview

Provides conventions for creating your own writer that can stop and start safely.

You should also consider adding attr_readers/methods for attributes that are differentiators from other writers of the same type. This will allow readers to more easily remove your writer from them.

Direct Known Subclasses

FileWriter, UDPWriter

Instance Method Summary collapse

Constructor Details

#initialize(read_from_channel) ⇒ BaseWriter

Returns a new instance of BaseWriter.

Parameters:

  • read_from_channel (EventMachine::Channel)

    The channel that this writer should read from.



18
19
20
21
22
23
24
# File 'lib/pants/writers/base_writer.rb', line 18

def initialize(read_from_channel)
  @running = false
  @read_from_channel = read_from_channel
  @write_object ||= nil
  @starter = nil
  @stopper = nil
end

Instance Method Details

#running?Boolean

Returns Is the Writer writing data?.

Returns:

  • (Boolean)

    Is the Writer writing data?



68
69
70
# File 'lib/pants/writers/base_writer.rb', line 68

def running?
  @running
end

#startObject

This method must be redefined in a child class. The reader that this writer is tied to will call this before it starts reading.



28
29
30
# File 'lib/pants/writers/base_writer.rb', line 28

def start
  warn "You haven't defined a start method--are you sure this writer does something?"
end

#starterEventMachine::Callback

This should get called with #call after the writer is sure to be up and running, ready for accepting data.

Returns:

  • (EventMachine::Callback)

    The Callback that should get called.



54
55
56
# File 'lib/pants/writers/base_writer.rb', line 54

def starter
  @starter ||= EM.Callback { @running = true }
end

#stopObject

This method must be redefined in a child class. The reader that this writer is tied to will call this when it’s done reading whatever it’s reading.



35
36
37
# File 'lib/pants/writers/base_writer.rb', line 35

def stop
  warn "You haven't defined a stop method--are you sure you're cleaning up?"
end

#stopperEventMachine::Callback

This should get called with #call after the writer is done writing out the data in its channel.

Returns:

  • (EventMachine::Callback)

    The Callback that should get called.



63
64
65
# File 'lib/pants/writers/base_writer.rb', line 63

def stopper
  @stopper ||= EM.Callback { @running = false }
end

#write_objectString

Returns A String that identifies what the writer is writing to. This is simply used for displaying info to the user.

Returns:

  • (String)

    A String that identifies what the writer is writing to. This is simply used for displaying info to the user.



41
42
43
44
45
46
47
# File 'lib/pants/writers/base_writer.rb', line 41

def write_object
  if @write_object
    @write_object
  else
    warn "No write_object info has been defined for this writer."
  end
end