class DK::Reporter

Generate report of object data

Attributes

fields[RW]
headers[RW]
last[RW]
objects[RW]
rows[RW]
title[RW]

Public Class Methods

new(opts) click to toggle source
# File lib/draftking/reporter.rb, line 7
def initialize(opts)
  @objects = opts[:objects]
  @title   = build_title(opts)
  @rows    = opts[:rows]
  @fields  = @rows ? nil : populate_fields(opts[:fields], @objects.first)
  @headers = populate_headers(opts[:headers])
end

Public Instance Methods

build_title(opts) click to toggle source

Report Title @param opts [Boolean] Show simulation indicator @param opts [String] Report Title

# File lib/draftking/reporter.rb, line 18
def build_title(opts)
  "#{opts[:title]}#{"\n" + REPORT_SIM if opts[:simulate]}"
end
populate_fields(fields, obj = nil) click to toggle source

Determine Field List @param fields [[Symbol]] Field Symbol Array @param obj [Object] Example Object @return [[Symbol]] Field List

# File lib/draftking/reporter.rb, line 26
def populate_fields(fields, obj = nil)
  # Report all fields, unless specified.
  return fields if fields
  obj.instance_variables.map do |x|
    x = x.to_s.delete('@')
    obj.respond_to?(x) ? x : nil
  end.compact if obj
end
populate_headers(headers) click to toggle source

Determine Display Headers @param headers [[String]] Column Headers @return [[String]]

# File lib/draftking/reporter.rb, line 38
def populate_headers(headers)
  # Use field names as headers, unless specified
  return headers if headers
  return @fields.map(&:to_s) if @fields
end
populate_report_rows() click to toggle source

Collect report data

# File lib/draftking/reporter.rb, line 45
def populate_report_rows
  # Read data based on field list
  return if @rows
  @rows = []
  @objects.each do |object|
    @rows << @fields.map { |field| object.send(field.to_sym) }
  end
end
show() click to toggle source

Display Report

# File lib/draftking/reporter.rb, line 55
def show
  populate_report_rows
  opts = { rows: @rows, headings: @headers || [], title: @title }
  puts Terminal::Table.new(opts) unless @rows.empty?
end
Also aliased as: to_s
to_s()
Alias for: show