Class: FileFormatterInputData

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFileFormatterInputData

Returns a new instance of FileFormatterInputData.



7
8
9
10
11
12
# File 'lib/file_formatter_input_data.rb', line 7

def initialize
	@__format__ = :delimited
	@__delimiter__ = :comma
	# A hash is used to speed things up a bit 
	@children = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/file_formatter_input_data.rb', line 101

def method_missing(sym, *args)
	element_name = sym.to_s
	# We look for the element name in the hash
	# i.e. {1=>'name', 2=>'address'} 
	# if a "name" method is requested search in the values and return the key 
	if entry = @data.find {|k,v| v[0] == element_name}
		# if there is content (!nil) return the requested element from the array 
		@content ? @content[entry[0]].to_s : '[error no input provided]'
		
	elsif !@children.size.zero?
		# Check if any of the values that contain instances of FileFormatterInputData matches the symbol
		inner_data = nil
		@data.each do |k,v|
			if v[0].is_a? FileFormatterInputData
				if v[0].__name__ == element_name
					inner_data = v[0]
					break
				end
			end
		end
		
		if inner_data
			inner_data
		else
			super(sym, *args)
		end
	else
		super(sym, *args)
	end
end

Instance Attribute Details

#__delimiter__Object

Returns the value of attribute __delimiter__.



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

def __delimiter__
  @__delimiter__
end

#__format__Object

Returns the value of attribute __format__.



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

def __format__
  @__format__
end

#__name__Object

Returns the value of attribute __name__.



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

def __name__
  @__name__
end

#dataObject

Returns the value of attribute data.



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

def data
  @data
end

Instance Method Details

#__set_content__(text_line) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/file_formatter_input_data.rb', line 14

def __set_content__(text_line)
	if __format__ == :delimited
		# Parse the line (space separator has a different behaviour)
		if __delimiter__ == :space
			@content = text_line.split(FileFormatter::DATA_DELIMITERS[__delimiter__])
		else
			@content = CSV::parse_line(text_line, FileFormatter::DATA_DELIMITERS[__delimiter__])
		end
	else
		# Fixed format
		@content = []
		i = 0
		@data.keys.sort.each do |k|
			# There may be situations where indexed are skipped, as a result
			# some nil values may be stored in the array
			if @data[k]
				#puts @data[k].to_yaml
				@content << text_line[i, @data[k][1]]
				i += @data[k][1]
			end
		end
	end
	# Check if there are any other objects embedded
	@children.each {|item| item[1].__set_content__(@content[item[0]])}
end