Sha256: c5c373c795fe237a2d9bc81d20814f3c15261f004bfcc028fc369db0e4917f74

Contents?: true

Size: 1.62 KB

Versions: 1

Compression:

Stored size: 1.62 KB

Contents

require 'tap/task'

module Tap
  module Tasks
    # :startdoc::task the default dump task
    #
    # Dumps data to $stdout or a file output.
    #
    #   % tap run -- dump content --output FILEPATH
    #
    # Dump faciliates normal redirection:
    #
    #   % tap run -- load hello --: dump | more
    #   hello
    #
    #   % tap run -- load hello --: dump 1> results.txt
    #   % more results.txt
    #   hello
    #
    # Note that dumps are appended to the file.  Dump only accepts one object
    # at a time, so joins that produce an array (like sync) need to iterate
    # outputs to dump:
    #
    #   % tap run -- load hello -- load world -- dump --[0,1][2]i.sync
    #   hello
    #   world
    #
    # :startdoc::task-
    #
    # Dump serves as a baseclass for more complicated dumps.  A YAML dump
    # (see {tap-tasks}[http://tap.rubyforge.org/tap-tasks]) looks like this:
    #
    #   class Yaml < Tap::Tasks::Dump
    #     def dump(obj, io)
    #       YAML.dump(obj, io)
    #     end
    #   end
    #
    class Dump < Tap::Task
      config :output, $stdout, &c.io(:<<, :puts, :print)   # The dump target file
      config :overwrite, false, &c.flag                    # Overwrite the existing target
    
      # The default process prints dump headers as specified in the config,
      # then append the audit value to io.
      def process(input)
        open_io(output, overwrite ? 'w' : 'a') do |io|
          dump(input, io)
        end
        input
      end
    
      # Dumps the object to io, by default dump puts (not prints) obj.to_s.
      def dump(input, io)
        io.puts input.to_s
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
tap-0.18.0 lib/tap/tasks/dump.rb