Sha256: 3d1921821c71814ba4c1f62c047bf178cf69215b1c45ba96d4e875f8bf071d18

Contents?: true

Size: 1.17 KB

Versions: 1

Compression:

Stored size: 1.17 KB

Contents

# frozen_string_literal: true

# Ruby internal
require 'csv'

# Pass Station module
module PassStation
  # Password database handling
  class DB
    # Register CSV converters for parsing
    def csv_config
      strip_converter = proc { |field| field.strip }
      CSV::Converters[:strip] = strip_converter
      # https://github.com/ruby/csv/issues/208
      # @config[:strip] = true
      # @config[:liberal_parsing] = true
      @config[:headers] = true
      @config[:converters] = :strip
      @config[:header_converters] = :symbol
      @config[:empty_value] = '<blank>'
      @config[:nil_value] = '<blank>'
    end

    # Parse, sort and sanitize the password database
    # @param sort [Symbol] column name to sort by (columns depends on the database source, see {UPSTREAM_DATABASE})
    # @return [Array<CSV::Row>] table of +CSV::Row+, each row contains multiple
    #   attributes (columns depends on the database source, see {UPSTREAM_DATABASE})
    def parse(sort = nil)
      sort ||= UPSTREAM_DATABASE[@database_type][:COLUMNS].first[0]
      @data = CSV.table(@database_path, **@config).sort_by do |s|
        s[sort].downcase
      end
    end

    protected :csv_config
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pass-station-1.2.3 lib/pass_station/parse.rb