Class: Fbp::Text_file_writer_node

Inherits:
Node
  • Object
show all
Defined in:
lib/fbp/text_file_writer_node.rb

Overview

Description

The Text file writer node takes in a file path as an IIP and will open that file and will take in IPs and write the value of the :data item in the IP to the file.

Discussion

The Text_file_writer_node does allow for setting the file open mode by having an IIP with a :file_open_mode key specifying the open mode. Setting this value using an IIP allows for setting the open mode to a+ so appending instead of the the default which to overwrite the file using “w”

Instance Attribute Summary

Attributes inherited from Node

#executing, #options, #output

Instance Method Summary collapse

Methods inherited from Node

#clean_option, #execute, #merge_options!, #register_for_output_from_node, #set_option, #stop, #unregister_for_output_from_node, #wait_until_completed, #write_to_input, #write_to_output

Constructor Details

#initialize(file_name = nil) ⇒ Text_file_writer_node

When creating a new Text_file_writer_node instance one can provide a string which is the full path to the file to be written.



19
20
21
22
23
24
# File 'lib/fbp/text_file_writer_node.rb', line 19

def initialize(file_name = nil)
  super()
  @options[:file_name] = file_name if !file_name.nil?
  @options[:file_open_mode] = 'w'
  @file = nil
end

Instance Method Details

#do_node_work(args) ⇒ Object

When an IP is received the IP is checked to see if it has a :data key. If it does then that data is written to the file specified by the :file_name option. This will continue until an IP is sent with the :completed key at which will cause the file to be closed.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/fbp/text_file_writer_node.rb', line 39

def do_node_work(args)

    if args.has_key? :completed
      @file.close if !@file.nil?
      @file = nil
      return false
    end

    if args.has_key? :data
      data = args[:data]

      if data.nil?
        @file.close if !@file.nil?
        @file = nil
        return false
      end

      @file = File.new(@options[:file_name], @options[:file_open_mode]) if @file.nil?
      @file.write(data)
      @file.flush
    end

  true
end

#is_ready_to_run?Boolean

Checks to see if this Text_file_writer_node instance has a file_name and file_open_mode set. If so then true will be returned otherwise false.

Returns:

  • (Boolean)


30
31
32
# File 'lib/fbp/text_file_writer_node.rb', line 30

def is_ready_to_run?()
  @options.has_key?(:file_name) && @options.has_key?(:file_open_mode)
end