Sha256: 4ff383aacc16dd8d62f610273f976d1d74561853ecb1a29dd304aa7f5f71da92

Contents?: true

Size: 1.42 KB

Versions: 9

Compression:

Stored size: 1.42 KB

Contents

# frozen_string_literal: true

module HoneyFormat
  # Default row builder
  class Row < Struct
    # Create a row
    # @return [Row] returns an instantiated Row
    # @example
    #   row_klass = Row.new(:id, :username)
    #   row = row_klass.call('1', 'buren')
    #   # => #<struct id="1", username="buren">
    def self.call(row)
      new(*row)
    end

    # Represent row as CSV
    # @param columns [Array<Symbol>, Set<Symbol>, NilClass]
    #   the columns to output, nil means all columns (default: nil)
    # @return [String] CSV-string representation.
    def to_csv(columns: nil)
      attributes = members
      attributes = columns & attributes if columns

      row = attributes.map! { |column| to_csv_value(column) }

      ::CSV.generate_line(row)
    end

    # Describe the contents of this row in a string.
    # @return [String] content of this row
    def inspect
      attributes = members.map do |field|
        value = self[field]
        value = "\"#{value}\"" if value.is_a?(String)

        [field, value].join('=')
      end.join(', ')

      "#<Row #{attributes}>"
    end
    alias_method :to_s, :inspect

    private

    # Returns the column in CSV format
    # @param [Symbol] column name
    # @return [String] column value as CSV string
    def to_csv_value(column)
      value = public_send(column)
      return if value.nil?
      return value.to_csv if value.respond_to?(:to_csv)

      value.to_s
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
honey_format-0.27.0 lib/honey_format/matrix/row.rb
honey_format-0.26.0 lib/honey_format/matrix/row.rb
honey_format-0.25.0 lib/honey_format/matrix/row.rb
honey_format-0.24.0 lib/honey_format/matrix/row.rb
honey_format-0.23.0 lib/honey_format/matrix/row.rb
honey_format-0.22.0 lib/honey_format/matrix/row.rb
honey_format-0.21.1 lib/honey_format/matrix/row.rb
honey_format-0.21.0 lib/honey_format/matrix/row.rb
honey_format-0.20.0 lib/honey_format/matrix/row.rb