Class: DK::Reporter
- Inherits:
-
Object
- Object
- DK::Reporter
- Defined in:
- lib/draftking/reporter.rb
Overview
Generate report of object data
Instance Attribute Summary collapse
-
#fields ⇒ Object
Returns the value of attribute fields.
-
#headers ⇒ Object
Returns the value of attribute headers.
-
#last ⇒ Object
Returns the value of attribute last.
-
#objects ⇒ Object
Returns the value of attribute objects.
-
#rows ⇒ Object
Returns the value of attribute rows.
-
#title ⇒ Object
Returns the value of attribute title.
Instance Method Summary collapse
-
#build_title(opts) ⇒ Object
Report Title.
-
#initialize(opts) ⇒ Reporter
constructor
A new instance of Reporter.
-
#populate_fields(fields, obj = nil) ⇒ [Symbol]
Determine Field List.
-
#populate_headers(headers) ⇒ [String]
Determine Display Headers.
-
#populate_report_rows ⇒ Object
Collect report data.
-
#show ⇒ Object
(also: #to_s)
Display Report.
Constructor Details
#initialize(opts) ⇒ Reporter
Returns a new instance of Reporter.
7 8 9 10 11 12 13 |
# 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 |
Instance Attribute Details
#fields ⇒ Object
Returns the value of attribute fields.
6 7 8 |
# File 'lib/draftking/reporter.rb', line 6 def fields @fields end |
#headers ⇒ Object
Returns the value of attribute headers.
6 7 8 |
# File 'lib/draftking/reporter.rb', line 6 def headers @headers end |
#last ⇒ Object
Returns the value of attribute last.
6 7 8 |
# File 'lib/draftking/reporter.rb', line 6 def last @last end |
#objects ⇒ Object
Returns the value of attribute objects.
6 7 8 |
# File 'lib/draftking/reporter.rb', line 6 def objects @objects end |
#rows ⇒ Object
Returns the value of attribute rows.
6 7 8 |
# File 'lib/draftking/reporter.rb', line 6 def rows @rows end |
#title ⇒ Object
Returns the value of attribute title.
6 7 8 |
# File 'lib/draftking/reporter.rb', line 6 def title @title end |
Instance Method Details
#build_title(opts) ⇒ Object
Report Title
18 19 20 |
# 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) ⇒ [Symbol]
Determine Field List
26 27 28 29 30 31 32 33 |
# 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) ⇒ [String]
Determine Display Headers
38 39 40 41 42 |
# 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 ⇒ Object
Collect report data
45 46 47 48 49 50 51 52 |
# 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 ⇒ Object Also known as: to_s
Display Report
55 56 57 58 59 |
# 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 |