Class: Tap::Tasks::Load

Inherits:
Tap::Task show all
Defined in:
lib/tap/tasks/load.rb

Overview

:startdoc::task the default load task

Loads data from $stdin. String data may be passed directly. Load is typically used as a gateway to other tasks.

% tap run -- load string --: dump
string

Load facilitates normal redirection:

% echo goodnight moon | tap run -- load --: dump
goodnight moon

% tap run -- load --: dump < somefile.txt
contents of somefile

:startdoc::task-

Load serves as a baseclass for more complicated loads. A YAML load (see tap-tasks) looks like this:

class Yaml < Tap::Tasks::Load
  def load(io)
    YAML.load(io)
  end
end

Load is constructed to reque itself in cases where objects are to be loaded sequentially from the same io. Load will reque until the end-of-file is reached, but this behavior can be modified by overriding the complete? method. An example is a prompt task:

class Prompt < Tap::Tasks::Load
  config :exit_seq, "\n"

  def load(io)
    if io.eof?
      nil
    else
      io.readline
    end
  end

  def complete?(io, line)
    line == nil || line == exit_seq
  end
end

If the use_close configuration is specified, load will close io upon completion. Files opened by load are always closed upon completion.

Constant Summary

Constants inherited from Tap::Task

Tap::Task::DEFAULT_HELP_TEMPLATE

Instance Attribute Summary

Attributes inherited from Tap::Task

#app

Attributes included from App::Node

#dependencies, #joins

Instance Method Summary collapse

Methods inherited from Tap::Task

#call, desc, #enq, #execute, #fork, help, inherited, #initialize, #inspect, instance, instantiate, intern, load_config, #log, manifest, #merge, parse, parse!, #sequence, #switch, #sync_merge

Methods included from App::Node

#depends_on, extended, intern, #on_complete

Constructor Details

This class inherits a constructor from Tap::Task

Instance Method Details

#close(io) ⇒ Object

Closes io.



109
110
111
# File 'lib/tap/tasks/load.rb', line 109

def close(io)
  io.close
end

#complete?(io, last) ⇒ Boolean

Returns io.eof? Override in subclasses for the desired behavior (see process).

Returns:

  • (Boolean)


115
116
117
# File 'lib/tap/tasks/load.rb', line 115

def complete?(io, last)
  io.eof?
end

#load(io) ⇒ Object

Loads data from io using io.read. Load is intended as a hook for subclasses.



104
105
106
# File 'lib/tap/tasks/load.rb', line 104

def load(io)
  io.read
end

#open(io) ⇒ Object

Opens the io; specifically this means:

  • Opening a File (file true)

  • Creating a StringIO for String inputs

  • Opening an IO for integer file descriptors

  • Returning all other objects



89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/tap/tasks/load.rb', line 89

def open(io)
  return File.open(io) if file
  
  case io
  when String
    StringIO.new(io)
  when Integer
    IO.open(io)
  else 
    io
  end
end

#process(io = $stdin) ⇒ Object

Loads data from io. Process will open the input io object, load a result, then check to see if the loading is complete (using the complete? method). Unless loading is complete, process will enque io to self. Process will close io when loading is complete, provided use_close or file is specified.



67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/tap/tasks/load.rb', line 67

def process(io=$stdin)
  io = open(io)
  result = load(io)
  
  if complete?(io, result)
    if use_close || file
      close(io)
    end
  else
    enq(io)
  end
  
  result
end