Class: FileFormatterOutput

Inherits:
Object
  • Object
show all
Includes:
Reloadable
Defined in:
lib/file_formatter_output.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(yaml_def, formatter_input) ⇒ FileFormatterOutput

Returns a new instance of FileFormatterOutput.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/file_formatter_output.rb', line 7

def initialize(yaml_def, formatter_input)
	raise 'No definition was provided' if yaml_def.size.zero?
	
	@__yaml__ = yaml_def.dup

	@data = FileFormatterOutputData.new(formatter_input)
	
	# Load Yaml into a hash
	output_def = YAML::load(yaml_def)
	raise 'Hash was not provided.' unless output_def.is_a?  Hash
	raise 'No entries exist in the Hash' if output_def.length.zero?
	
	output_def.each {|k,v| instance_eval("self.#{k}=v")}
end

Instance Attribute Details

#__yaml__Object

Returns the value of attribute __yaml__.



5
6
7
# File 'lib/file_formatter_output.rb', line 5

def __yaml__
  @__yaml__
end

#file_nameObject

Returns the value of attribute file_name.



5
6
7
# File 'lib/file_formatter_output.rb', line 5

def file_name
  @file_name
end

#file_pathObject

Returns the value of attribute file_path.



5
6
7
# File 'lib/file_formatter_output.rb', line 5

def file_path
  @file_path
end

Instance Method Details

#dataObject



22
# File 'lib/file_formatter_output.rb', line 22

def data; @data; end

#data=(value) ⇒ Object



23
# File 'lib/file_formatter_output.rb', line 23

def data=(value); @data.data = value; end

#file_full_nameObject



25
# File 'lib/file_formatter_output.rb', line 25

def file_full_name; File.join(file_path, file_name); end

#generate(max_lines = nil) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/file_formatter_output.rb', line 27

def generate(max_lines=nil)
	input = File.open(@data.__formatter_input__.file_full_name)
	output = File.open(file_full_name, 'w')
	
	i = 0
	
	while !input.eof?
		@data.__formatter_input__.data.__set_content__(input.readline)
		output_line = @data.__generate_line__
		
		yield output_line if block_given?
		
		output.puts output_line
		
		break if (i += 1) == max_lines
	end
	
	output.close
	output = nil
	
	input.close
	input = nil
	
	return i
end