Class: CsvController

Inherits:
Object
  • Object
show all
Defined in:
lib/prefix_tree/csv_controller.rb

Instance Method Summary collapse

Constructor Details

#initialize(tree = PrefixTree.new) ⇒ CsvController

Returns a new instance of CsvController.



8
9
10
# File 'lib/prefix_tree/csv_controller.rb', line 8

def initialize(tree = PrefixTree.new)
  @tree = tree
end

Instance Method Details

#fill_csvObject



12
13
14
15
16
17
18
19
# File 'lib/prefix_tree/csv_controller.rb', line 12

def fill_csv
  strings = @tree.dictionary.map { |key, value| [key, value] }

  CSV.open('dictionary.csv', 'w') do |csv|
    csv << %w[id name]
    strings.each { |string| csv << string }
  end
end

#read_csvObject



21
22
23
24
# File 'lib/prefix_tree/csv_controller.rb', line 21

def read_csv
  strings = CSV.parse(File.read('strings_for_tree.csv'), headers: true).by_col[1]
  fill_tree(strings)
end