Sha256: 70454664a59cd84264c1618a83ddc2e7740bec85e874dbf2835a5e5b6d22ca4e

Contents?: true

Size: 1.24 KB

Versions: 6

Compression:

Stored size: 1.24 KB

Contents

require 'csv'

module CsvShaper
  # Config
  # Configure the standard CSV default options
  # as well the option to output the header row

  class Config
    CUSTOM_DEFAULT_OPTIONS = { header_inflector: :humanize }

    attr_reader :options

    def initialize
      @options = {}
      @options.merge!(CUSTOM_DEFAULT_OPTIONS)
      yield self if block_given?
    end

    # Public: set options where the method name
    # matches a key
    def method_missing(meth, value)
      meth = sanitize_setter_method(meth)

      if defaults.key?(meth)
        @options[meth] = value
      else
        super
      end
    end

    def respond_to?(meth)
      meth = sanitize_setter_method(meth)
      defaults.key?(meth)
    end

    private

    # Internal: removes the equals from the end of the
    # method name
    #
    # `meth` - Symbol of the method of sanitize
    #
    # Returns a Symbol
    def sanitize_setter_method(meth)
      meth = meth.to_s.gsub('=', '')
      meth.to_sym
    end

    # Internal: default CSV options, plus a write headers
    # option, to pass to #to_csv
    #
    # Returns a Hash
    def defaults
      @defaults ||= CSV::DEFAULT_OPTIONS.dup.
        merge(write_headers: true).
        merge(CUSTOM_DEFAULT_OPTIONS)
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
csv_shaper-1.3.2 lib/csv_shaper/config.rb
csv_shaper-1.3.1 lib/csv_shaper/config.rb
csv_shaper-1.3.0 lib/csv_shaper/config.rb
csv_shaper-1.2.0 lib/csv_shaper/config.rb
csv_shaper-1.1.1 lib/csv_shaper/config.rb
csv_shaper-1.1.0 lib/csv_shaper/config.rb