Module: CSVhelper
- Defined in:
- lib/tabbyx/helpers/csv_helper.rb
Class Method Summary collapse
-
.append_to_row_end(filename, row, value) ⇒ Object
example: CSVhelper.append_to_row_end(“api_testcases.csv”,2,“pass”).
- .read_from_csv(filename) ⇒ Object
-
.read_specified_cell(filename, row, column) ⇒ Object
example: CSVhelper.read_specified_cell(“api_testcases.csv”,2,2).
-
.write_array_to_csv(array, filename) ⇒ Object
example: arr = [“test”, “test2”, “test3”] CSVhelper.write_array_to_csv(arr,“test_ar.csv”).
-
.write_dictionary_to_csv(dic, filename) ⇒ Object
example: dic = [[“test”, “test2”, “test3”],[“test”, “test2”, “test3”],[“test”, “test2”, “test3”]] CSVhelper.write_dictionary_to_csv(dic, “test_dic.csv”).
-
.write_hash_to_csv(hash, filename) ⇒ Object
example: h = { ‘dog’ => ‘canine’, ‘cat’ => ‘feline’, ‘donkey’ => ‘asinine’ } CSVhelper.write_hash_to_csv(h,“test.csv”).
Class Method Details
.append_to_row_end(filename, row, value) ⇒ Object
example: CSVhelper.append_to_row_end(“api_testcases.csv”,2,“pass”)
54 55 56 57 58 59 60 61 62 63 |
# File 'lib/tabbyx/helpers/csv_helper.rb', line 54 def self.append_to_row_end(filename,row,value) file = Base.file_exists?(filename) original_data = CSV.parse(File.read(file)) if Base.file_exists?(filename) original_data[row] << value CSV.open(file, "wb") do |csv| original_data.each do |data| csv << data end end end |
.read_from_csv(filename) ⇒ Object
6 7 8 9 10 |
# File 'lib/tabbyx/helpers/csv_helper.rb', line 6 def self.read_from_csv(filename) file = Base.file_exists?(filename) sheet = CSV.read(file) sheet end |
.read_specified_cell(filename, row, column) ⇒ Object
example: CSVhelper.read_specified_cell(“api_testcases.csv”,2,2)
14 15 16 17 |
# File 'lib/tabbyx/helpers/csv_helper.rb', line 14 def self.read_specified_cell(filename,row,column) sheet = read_from_csv(filename) sheet[row][column] end |
.write_array_to_csv(array, filename) ⇒ Object
example: arr = [“test”, “test2”, “test3”] CSVhelper.write_array_to_csv(arr,“test_ar.csv”)
32 33 34 35 36 37 |
# File 'lib/tabbyx/helpers/csv_helper.rb', line 32 def self.write_array_to_csv(array,filename) Base.file_exists?(filename) ? file = Base.file_exists?(filename) : file = Base.create_file(filename) CSV.open(file, "wb") do |csv| csv << array end end |
.write_dictionary_to_csv(dic, filename) ⇒ Object
example: dic = [[“test”, “test2”, “test3”],[“test”, “test2”, “test3”],[“test”, “test2”, “test3”]] CSVhelper.write_dictionary_to_csv(dic, “test_dic.csv”)
43 44 45 46 47 48 49 50 |
# File 'lib/tabbyx/helpers/csv_helper.rb', line 43 def self.write_dictionary_to_csv(dic,filename) Base.file_exists?(filename) ? file = Base.file_exists?(filename) : file = Base.create_file(filename) CSV.open(file, "wb") do |csv| dic.each do |data| csv << data end end end |
.write_hash_to_csv(hash, filename) ⇒ Object
example: h = { ‘dog’ => ‘canine’, ‘cat’ => ‘feline’, ‘donkey’ => ‘asinine’ } CSVhelper.write_hash_to_csv(h,“test.csv”)
23 24 25 26 |
# File 'lib/tabbyx/helpers/csv_helper.rb', line 23 def self.write_hash_to_csv(hash,filename) Base.file_exists?(filename) ? file = Base.file_exists?(filename) : file = Base.create_file(filename) CSV.open(file, "wb") {|csv| hash.to_a.each {|elem| csv << elem} } end |