Sha256: 648939dcf934d749456dd9bf5a083e445a52b25744ebbfbd3516e113817d8db9

Contents?: true

Size: 1.04 KB

Versions: 15

Compression:

Stored size: 1.04 KB

Contents

require "csv"
require "json"
require "yaml"

module Eddy
  module Util

    # Read a TSV file and return its contents as an array of hashes.
    #
    # @param filepath [String] Path to the TSV file.
    # @return [Array<Hash{Symbol => String}>]
    def self.parse_tsv(filepath)
      return CSV.read(
        filepath,
        col_sep: "\t",
        headers: true,
        quote_char: "\x00",
        header_converters: :symbol,
      ).map(&:to_hash)
    end

    # Read data in from either a JSON or YAML file.
    #
    # @param path [String] Path to the file.
    # @param symbolize [Boolean] (true)
    # @return [Hash{Symbol => Object}]
    def self.read_json_or_yaml(path, symbolize: true)
      path = File.expand_path(path)
      data = case File.extname(path).downcase
             when /\.ya?ml/ then YAML.safe_load(File.read(path), symbolize_names: symbolize)
             when ".json"   then JSON.parse(File.read(path), symbolize_names: symbolize)
             else raise Eddy::Errors::Error
             end
      return data
    end

  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
eddy-0.10.0 lib/eddy/util/read.rb
eddy-0.9.2 lib/eddy/util/read.rb
eddy-0.9.1 lib/eddy/util/read.rb
eddy-0.9.0 lib/eddy/util/read.rb
eddy-0.8.4 lib/eddy/util/read.rb
eddy-0.8.3 lib/eddy/util/read.rb
eddy-0.8.2 lib/eddy/util/read.rb
eddy-0.8.1 lib/eddy/util/read.rb
eddy-0.8.0 lib/eddy/util/read.rb
eddy-0.7.0 lib/eddy/util/read.rb
eddy-0.6.0 lib/eddy/util/read.rb
eddy-0.5.1 lib/eddy/util/read.rb
eddy-0.5.0 lib/eddy/util/read.rb
eddy-0.4.0 lib/eddy/util/read.rb
eddy-0.3.0 lib/eddy/util/read.rb