Sha256: 45596e39da960aec68df8829bcb6c282e65a14b5b66ace6590572c46738e7b67

Contents?: true

Size: 1.17 KB

Versions: 4

Compression:

Stored size: 1.17 KB

Contents

module RailsData::Export

  # extend RailsData::Export
  # config do
  #   collect -> (params) { User.default_where(params) }
  #   column :name, header: 'My name', field: -> {}
  #   column :email, header: 'Email', field: -> {}
  # end
  attr_reader :collection,
              :columns,
              :parameters

  def config(*args, &block)
    block.call(*args) if block_given?
  end

  def collect(collection)
    @collection = collection
    @parameters ||= []
    if collection.respond_to?(:call)
      _params = collection.parameters.to_combined_h
      @parameters << _params[:key] if _params[:key]
    end
  end

  def column(name, header: nil, field: nil, footer: nil)
    @columns ||= {}
    name = name.to_sym

    if @columns.keys.include?(name)
      warn 'The column is repeated'
    end

    @columns[name] = {}

    if header.nil?
      @columns[name][:header] = name.titleize
    else
      @columns[name][:header] = header
    end
    @columns[name][:field] = field
    @columns[name][:footer] = footer if footer

    if field.respond_to?(:call)
      _params = field.parameters.to_combined_h
      @parameters << _params[:key] if _params[:key]
    end

    self
  end

end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rails_data-1.0.2 lib/rails_data/export.rb
rails_data-1.0.1 lib/rails_data/export.rb
rails_data-1.0.0 lib/rails_data/export.rb
rails_data-0.0.1 lib/rails_data/export.rb