Class: FileConfig

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project_config:, block:) ⇒ FileConfig

Returns a new instance of FileConfig.



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

def initialize project_config:, block:
  @project_config = project_config
  @block = block
  @columns = nil
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



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

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

#columns(&block) ⇒ Object



81
82
83
84
# File 'lib/jirametrics/file_config.rb', line 81

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

#file_suffix(suffix = nil) ⇒ Object



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

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

#html_report(&block) ⇒ Object



86
87
88
89
# File 'lib/jirametrics/file_config.rb', line 86

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

#only_use_row_if(&block) ⇒ Object



95
96
97
# File 'lib/jirametrics/file_config.rb', line 95

def only_use_row_if &block
  @only_use_row_if = block
end

#output_filenameObject



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

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

#prepare_gridObject



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

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



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

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

  if @columns
    all_lines = prepare_grid

    File.open(output_filename, 'w') do |file|
      all_lines.each do |output_line|
        file.puts CSV.generate_line(output_line)
      end
    end
  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.



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/jirametrics/file_config.rb', line 69

def sort_output all_lines
  all_lines.sort do |a, b|
    if a[0].nil?
      1
    elsif b[0].nil?
      -1
    else
      a[0] <=> b[0]
    end
  end
end

#to_date(object) ⇒ Object



99
100
101
# File 'lib/jirametrics/file_config.rb', line 99

def to_date object
  to_datetime(object)&.to_date
end

#to_datetime(object) ⇒ Object



103
104
105
106
107
108
109
# File 'lib/jirametrics/file_config.rb', line 103

def to_datetime object
  return nil if object.nil?

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

#to_string(object) ⇒ Object



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

def to_string object
  object.to_s
end