Sha256: fd00eb2e4f37b57280cbbbcc70f452175314021a72b0db53dfca77db329b793f

Contents?: true

Size: 1.6 KB

Versions: 3

Compression:

Stored size: 1.6 KB

Contents

module SimpleCsv
  class Settings
    DEFAULTS = { headers: true, col_sep: ',', seperator: ',',
                 force_quotes: true,
                 converters: [:all, :blank_to_nil, :null_to_nil] }.freeze
    ALIASSED = { seperator: :col_sep, has_headers: :headers }.freeze
    INVERTED_ALIASSES = ALIASSED.to_a.map(&:reverse).to_h

    def initialize(**opts)
      @settings = DEFAULTS.dup.merge opts
    end

    def [](setting)
      send setting
    end

    def []=(m, val)
      @settings[m] = val
      return @settings[ALIASSED[m]] = val if ALIASSED.key? m
      return @settings[INVERTED_ALIASSES[m]] = val if INVERTED_ALIASSES.key? m
      val
    end

    def for_csv
      settings = @settings.dup

      ALIASSED.each do |opt_alias, opt|
        settings[opt] = settings.delete(opt_alias) if settings.key? opt_alias
      end

      CSV::DEFAULT_OPTIONS.each_with_object({}) do |(prop, default), csv_hash|
        csv_hash[prop] = settings.key?(prop) ? settings[prop] : default
      end
    end

    def apply(*hashes)
      hashes.each { |opts| opts.each { |k, v| self[k] = v } } && @settings
    end

    def any?
      @settings && @settings.any?
    end

    def respond_to_missing?(mtd, include_private = false)
      accepted_method? mtd || super
    end

    def method_missing(mtd, *args, &block)
      return super unless accepted_method? mtd
      mtd = ALIASSED[mtd] if ALIASSED.key? mtd
      mtd = INVERTED_ALIASSES[mtd] if INVERTED_ALIASSES.key? mtd
      @settings[mtd]
    end

    def accepted_method?(mtd)
      @settings.key?(mtd) || ALIASSED.key?(mtd) || INVERTED_ALIASSES.key?(mtd)
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
simple_csv-0.2.2 lib/simple_csv/settings.rb
simple_csv-0.2.1 lib/simple_csv/settings.rb
simple_csv-0.2.0 lib/simple_csv/settings.rb