Sha256: 9ba9bbfdc1b0013102d1757e7c2c104f3f8b2bf547c3c61b2d3c7c86d1dde52f

Contents?: true

Size: 1.16 KB

Versions: 2

Compression:

Stored size: 1.16 KB

Contents

module SimpleCsv
  class Base
    attr_reader :index

    COMMON_DELIMITERS = %w(, ; |).freeze

    private

    def settings(**opts)
      return @settings.merge opts if opts.any? && @settings
      @settings ||= Settings.new opts
    end

    def headers(*cols, **col_map)
      @headers ||= []

      if cols.any?
        @headers.concat cols.map { |col| col.to_s.strip }
        alias_to_friendly_headers
      end

      @col_map ||= {}
      @col_map.merge! stringify_col_map(col_map) if col_map.any?

      @headers_set ||= @headers.any?
      @headers
    end

    def alias_to_friendly_headers
      @col_map ||= {}
      aliasses = headers.each_with_object({}) do |hdr, h|
        n = hdr.to_s.strip.gsub(/([a-z])([A-Z])/, '\1_\2').downcase
               .gsub(/[^\w]|\s/, '_')
        h[n] = hdr unless @col_map.key? n
      end

      @col_map.merge! aliasses
    end

    def method_missing(mtd, *args, &block)
      super
    end

    def stringify_col_map(col_map)
      col_map.to_a.map { |m| m.reverse.map(&:to_s) }.to_h
    end

    def respond_to_missing?(mtd, include_private = false)
      @headers.include?(mtd) || @col_map.key?(mtd.to_s) || super
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
simple_csv-0.2.2 lib/simple_csv/base.rb
simple_csv-0.2.1 lib/simple_csv/base.rb