Class: FileConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/jirametrics/file_config.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project_config:, block:, today: Date.today, issues: nil) ⇒ FileConfig

Returns a new instance of FileConfig.



8
9
10
11
12
13
14
# File 'lib/jirametrics/file_config.rb', line 8

def initialize project_config:, block:, today: Date.today, issues: nil
  @project_config = project_config
  @block = block
  @columns = nil
  @today = today
  @issues = issues
end

Instance Attribute Details

#issuesObject (readonly)

Returns the value of attribute issues.



6
7
8
# File 'lib/jirametrics/file_config.rb', line 6

def issues
  @issues
end

#project_configObject (readonly)

Returns the value of attribute project_config.



6
7
8
# File 'lib/jirametrics/file_config.rb', line 6

def project_config
  @project_config
end

Instance Method Details

#assert_only_one_filetype_config_setObject



106
107
108
# File 'lib/jirametrics/file_config.rb', line 106

def assert_only_one_filetype_config_set
  raise 'Can only have one columns or html_report declaration inside a file' if @columns || @html_report
end

#childrenObject



139
140
141
142
143
144
# File 'lib/jirametrics/file_config.rb', line 139

def children
  result = []
  result << @columns if @columns
  result << @html_report if @html_report
  result
end

#columns(&block) ⇒ Object



91
92
93
94
# File 'lib/jirametrics/file_config.rb', line 91

def columns &block
  assert_only_one_filetype_config_set
  @columns = ColumnsConfig.new file_config: self, block: block
end

#compare_rows(left, right) ⇒ Object

Compare two rows by their first column, falling back to the remaining columns on a tie, and sorting nil first columns to the bottom.



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/jirametrics/file_config.rb', line 79

def compare_rows left, right
  if left[0] == right[0]
    left[1..] <=> right[1..]
  elsif left[0].nil?
    1
  elsif right[0].nil?
    -1
  else
    left[0] <=> right[0]
  end
end

#file_suffix(suffix = nil) ⇒ Object



134
135
136
137
# File 'lib/jirametrics/file_config.rb', line 134

def file_suffix suffix = nil
  @file_suffix = suffix unless suffix.nil?
  @file_suffix
end

#html_report(&block) ⇒ Object



96
97
98
99
100
101
102
103
104
# File 'lib/jirametrics/file_config.rb', line 96

def html_report &block
  assert_only_one_filetype_config_set
  if block.nil?
    project_config.file_system.warning 'No charts were specified for the report. This is almost certainly a mistake.'
    block = ->(_) {}
  end

  @html_report = HtmlReportConfig.new file_config: self, block: block
end

#only_use_row_if(&block) ⇒ Object



110
111
112
# File 'lib/jirametrics/file_config.rb', line 110

def only_use_row_if &block
  @only_use_row_if = block
end

#output_filenameObject



57
58
59
60
61
62
63
# File 'lib/jirametrics/file_config.rb', line 57

def output_filename
  segments = []
  segments << project_config.target_path
  segments << project_config.get_file_prefix
  segments << (@file_suffix || "-#{@today}.csv")
  segments.join
end

#prepare_gridObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/jirametrics/file_config.rb', line 32

def prepare_grid
  @columns.run

  all_lines = issues.collect do |issue|
    line = []
    @columns.columns.each do |type, _name, block|
      # Invoke the block that will retrieve the result from Issue
      result = instance_exec(issue, &block)
      # Convert that result to the appropriate type
      line << __send__(:"to_#{type}", result)
    end
    line
  end

  all_lines = all_lines.select(&@only_use_row_if) if @only_use_row_if
  all_lines = sort_output(all_lines)

  if @columns.write_headers
    line = @columns.columns.collect { |_type, label, _proc| label }
    all_lines.insert 0, line
  end

  all_lines
end

#runObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/jirametrics/file_config.rb', line 16

def run
  @issues = project_config.issues
  instance_eval(&@block)

  if @columns
    all_lines = prepare_grid

    content = all_lines.collect { |line| CSV.generate_line line }.join
    project_config.exporter.file_system.save_file content: content, filename: output_filename
  elsif @html_report
    @html_report.run
  else
    raise 'Must specify one of "columns" or "html_report"'
  end
end

#sort_output(all_lines) ⇒ Object

We'll probably make sorting configurable at some point but for now it's hard coded for our most common usecase - the Team Dashboard from FocusedObjective.com. The rule for that one is that all empty values in the first column should be at the bottom.



68
69
70
71
72
73
74
75
# File 'lib/jirametrics/file_config.rb', line 68

def sort_output all_lines
  all_lines.each_with_index.sort do |(left, left_idx), (right, right_idx)|
    result = compare_rows left, right

    # When objects aren't comparable, preserve original order for a stable sort.
    result.nil? || result.zero? ? left_idx <=> right_idx : result
  end.map(&:first)
end

#to_date(object) ⇒ Object



114
115
116
# File 'lib/jirametrics/file_config.rb', line 114

def to_date object
  to_datetime(object)&.to_date
end

#to_datetime(object) ⇒ Object



118
119
120
121
122
123
124
# File 'lib/jirametrics/file_config.rb', line 118

def to_datetime object
  return nil if object.nil?

  object = object.to_time.to_datetime
  object = object.new_offset(@timezone_offset) if @timezone_offset
  object
end

#to_integer(object) ⇒ Object



130
131
132
# File 'lib/jirametrics/file_config.rb', line 130

def to_integer object
  object.to_i
end

#to_string(object) ⇒ Object



126
127
128
# File 'lib/jirametrics/file_config.rb', line 126

def to_string object
  object.to_s
end