Sha256: f7efb423ad4121adaad44128f657b2f572e7c334bff99f92ba3ae2d65d75f76c

Contents?: true

Size: 1.14 KB

Versions: 2

Compression:

Stored size: 1.14 KB

Contents

module Sources
  
  # Describes a CSV source, a file with csv in it.
  # Give it a sequence of category names and a file option with the filename.
  #
  class NoCSVFileGiven < StandardError; end
  
  class CSV < Base
    
    attr_reader :file_name, :csv_options, :category_names
    
    def initialize *category_names, options
      require 'csv'
      @category_names = category_names
      
      @csv_options    = Hash === options && options || {}
      @file_name      = @csv_options.delete(:file) || raise_no_file_given(category_names)
    end
    
    #
    #
    def raise_no_file_given category_names
      raise NoCSVFileGiven.new(category_names.join(', '))
    end
    
    # Harvests the data to index.
    #
    def harvest _, category
      index = category_names.index category.from
      get_data do |ary|
        indexed_id = ary.shift.to_i # TODO is to_i necessary?
        text       = ary[index]
        next unless text
        text.force_encoding 'utf-8' # TODO Still needed?
        yield indexed_id, text
      end
    end
    
    #
    #
    def get_data &block
      ::CSV.foreach file_name, csv_options, &block
    end
    
  end
  
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
picky-0.12.3 lib/picky/sources/csv.rb
picky-0.12.2 lib/picky/sources/csv.rb