Sha256: 1c66ad21d9ac001463f92f8893fda13675820f81789f4ff42ca00bc34114dbd2

Contents?: true

Size: 1.94 KB

Versions: 11

Compression:

Stored size: 1.94 KB

Contents

module Sources
  
  # Raised when a CSV source is instantiated without a file.
  #
  # Example:
  #   Sources::CSV.new(:column1, :column2) # without file option
  #
  class NoCSVFileGiven < StandardError; end
  
  # Describes a CSV source, a file with comma separated values in it.
  # 
  # The first column is implicitly assumed to be the id column.
  #
  # It takes the same options as the Ruby 1.9 CSV class.
  #
  # Examples:
  #  Sources::CSV.new(:title, :author, :isbn, file:'data/a_csv_file.csv')
  #  Sources::CSV.new(:title, :author, :isbn, file:'data/a_csv_file.csv', col_sep:';')
  #  Sources::CSV.new(:title, :author, :isbn, file:'data/a_csv_file.csv', row_sep:"\n")
  #
  class CSV < Base
    
    # The CSV file's path, relative to PICKY_ROOT.
    #
    attr_reader :file_name
    
    # The options that were passed into #new.
    #
    attr_reader :csv_options, :key_format
    
    # The data category names.
    #
    attr_reader :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)
      
      key_format   = options.delete :key_format
      @key_format  = key_format && key_format.to_sym || :to_i
    end
    
    # Raises a NoCSVFileGiven exception.
    #
    def raise_no_file_given category_names # :nodoc:
      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
        text       = ary[index]
        next unless text
        text.force_encoding 'utf-8' # TODO Still needed?
        yield indexed_id, text
      end
    end
    
    #
    #
    def get_data &block # :nodoc:
      ::CSV.foreach file_name, csv_options, &block
    end
    
  end
  
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
picky-1.5.2 lib/picky/sources/csv.rb
picky-1.5.1 lib/picky/sources/csv.rb
picky-1.5.0 lib/picky/sources/csv.rb
picky-1.4.3 lib/picky/sources/csv.rb
picky-1.4.2 lib/picky/sources/csv.rb
picky-1.4.1 lib/picky/sources/csv.rb
picky-1.4.0 lib/picky/sources/csv.rb
picky-1.3.4 lib/picky/sources/csv.rb
picky-1.3.3 lib/picky/sources/csv.rb
picky-1.3.2 lib/picky/sources/csv.rb
picky-1.3.1 lib/picky/sources/csv.rb