Class: Fbp::Test_file_reader_node
- Defined in:
- lib/fbp/text_file_reader_node.rb
Overview
Description
The Text file reader node takes in a file path as an IIP and will open that file and read a line at a time and package that data as an IP and send that data to the downstream node.
Discussion
The Test_file_reader_node requires the file to be read exists before running this node.
Instance Attribute Summary
Attributes inherited from Node
Instance Method Summary collapse
-
#do_node_work(args) ⇒ Object
Once this instance is executed, it will open the file specified in the :file_name option and read a line at a time.
-
#initialize(file_name = nil) ⇒ Test_file_reader_node
constructor
When creating a new Test_file_reader_node instance one can provide a string which is the full path to the file to be read.
-
#is_ready_to_run? ⇒ Boolean
Checks to see if this Test_file_reader_node instance has a file_name set.
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) ⇒ Test_file_reader_node
When creating a new Test_file_reader_node instance one can provide a string which is the full path to the file to be read.
17 18 19 20 21 22 |
# File 'lib/fbp/text_file_reader_node.rb', line 17 def initialize(file_name = nil) super() @options[:requires_input] = false @options[:file_name] = file_name if !file_name.nil? @file = nil end |
Instance Method Details
#do_node_work(args) ⇒ Object
Once this instance is executed, it will open the file specified in the :file_name option and read a line at a time. A line is defined as a series of characters up to a return character.
Once a line is read, an IP is made with that data keys with :data and sent to the down stream node.
When the EOF is reached the file will be closed.
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/fbp/text_file_reader_node.rb', line 44 def do_node_work(args) if args.has_key? :completed @file.close if !@file.nil? @file = nil return false end if args.has_key? :continue file_line = nil close_file = false @file = File.new(@options[:file_name], 'r') if @file.nil? begin file_line = @file.gets close_file = true if file_line.nil? close_file = true if @file.eof? rescue close_file = true end write_to_output({:data => file_line}) if !file_line.nil? if close_file @file.close @file = nil return false end end true end |
#is_ready_to_run? ⇒ Boolean
Checks to see if this Test_file_reader_node instance has a file_name set. If so then true will be returned otherwise false.
29 30 31 |
# File 'lib/fbp/text_file_reader_node.rb', line 29 def is_ready_to_run?() @options.has_key? :file_name end |