Sha256: 2f5f1f219fcc2e26e8e06628b95d9af6aad1d9ba0a986128a9edf71038f1783e

Contents?: true

Size: 853 Bytes

Versions: 6

Compression:

Stored size: 853 Bytes

Contents

# frozen_string_literal: true

module Acb
  class Column
    attr_reader :name

    def initialize(name, **options)
      @name = name
      @index = case options[:index]
               when Proc
                 options[:index]
               when String
                 options[:index].split('.')
               else
                 [name]
               end
      @format = options[:format]
    end

    def digest(row)
      data = _digest(row)
      @format && data ? format(data) : data
    end

    private

    def _digest(row)
      if @index.is_a?(Array)
        @index.reduce(row) do |acc, mthd|
          acc.send(mthd)
        end
      else
        @index.call(row)
      end
    end

    def format(data)
      case data
      when Date, Time
        data.strftime(@format)
      else
        @format % data
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
acb-0.2.1 lib/acb/column.rb
acb-0.2.0 lib/acb/column.rb
acb-0.1.5 lib/acb/column.rb
acb-0.1.4 lib/acb/column.rb
acb-0.1.3 lib/acb/column.rb
acb-0.1.2 lib/acb/column.rb