Sha256: 44c4d0a740f7a3dc684e0b6e2e985616fbf25d641d3a45f5c0a9c621fefcf69e

Contents?: true

Size: 1.75 KB

Versions: 1

Compression:

Stored size: 1.75 KB

Contents

# encoding: utf-8
#
require 'csv'

module Mode
  module Sdk
    # Represents a local CSV file
    #
    # @attr_reader [String] original_path the path of the file
    #
    class CsvFile
      attr_reader :original_path

      # Construct a new CsvFile instance
      #
      # @param original_path [String] the original path of the file
      #
      # @return [Mode::Sdk::CsvFile] the instance
      #
      def initialize(original_path)
        @original_path = original_path
      end

      # The expanded path of the file
      #
      # @return [String] the expanded path
      #
      def path
        @path ||= File.expand_path(original_path)
      end

      # The base name of the file with CSV extension removed, if present
      #
      # @return [String] the filename
      #
      def name
        File.basename(path, '.csv')
      end

      # The size of the file
      #
      # @return [Integer] the filesize
      #
      def size
        @size ||= File.size(path)
      end

      # An array of all lines in the file
      #
      # @return [Array<String>] array of lines
      #
      def lines
        @lines ||= File.open(path, 'r', &:read).split(/[\r\n]+/)
      end

      # The total number of lines in the file
      #
      # @return [Integer] number of lines
      #
      def line_count
        @line_count ||= lines.size
      end

      # An array of parsed CSV header column names
      #
      # @return [Array<String>] array of column names
      #
      def header
        @header ||= CSV.parse_line(lines[0])
      end

      # All lines of the file except the header with standardized LF line
      # breaks
      #
      # @return [String] the file content
      #
      def content
        @content ||= lines[1..-1].join("\n")
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mode-sdk-0.1.0 lib/mode/sdk/csv_file.rb