Class: Tap::Tasks::Load

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

Overview

:startdoc::task load data

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

% tap load string -: dump
string

Load facilitates normal redirection:

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

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

:startdoc::task-

Load serves as a baseclass for more complicated loads. A YAML load could look like this:

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

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

Direct Known Subclasses

Stream

Instance Attribute Summary

Attributes inherited from Tap::Task

#joins

Attributes inherited from App::Api

#app

Instance Method Summary collapse

Methods inherited from Tap::Task

#associations, #call, #enq, #exe, #initialize, #log, #on_complete, parser

Methods inherited from App::Api

#associations, build, help, inherited, #initialize, #inspect, parse, parse!, parser, #to_spec

Methods included from Signals

#sig, #signal, #signal?, #signals

Methods included from Signals::ModuleMethods

included

Constructor Details

This class inherits a constructor from Tap::Task

Instance Method Details

#close(io) ⇒ Object

Closes io.



86
87
88
# File 'lib/tap/tasks/load.rb', line 86

def close(io)
  io.close
end

#load(io) ⇒ Object

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



81
82
83
# File 'lib/tap/tasks/load.rb', line 81

def load(io)
  io.read
end

#open(io) ⇒ Object

Opens the io; specifically this means:

  • Creating a StringIO for String inputs

  • Opening an IO for integer file descriptors

  • Returning all other objects



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/tap/tasks/load.rb', line 61

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

#open_file(io) ⇒ Object

Opens io as a File.



75
76
77
# File 'lib/tap/tasks/load.rb', line 75

def open_file(io)
  io.kind_of?(File) ? io : File.open(io)
end

#process(io = $stdin) ⇒ Object

Loads data from io. Process will open the input io object, load a result. Process will close io when loading is complete, provided use_close or file is specified.



44
45
46
47
48
49
50
51
52
53
# File 'lib/tap/tasks/load.rb', line 44

def process(io=$stdin)
  io = open(io)
  result = load(io)
  
  if use_close || file
    close(io)
  end
  
  result
end