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) ⇒ FileConfig

Returns a new instance of FileConfig.



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

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



101
102
103
# File 'lib/jirametrics/file_config.rb', line 101

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



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

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

#columns(&block) ⇒ Object



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

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

#file_suffix(suffix = nil) ⇒ Object



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

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

#html_report(&block) ⇒ Object



91
92
93
94
95
96
97
98
99
# File 'lib/jirametrics/file_config.rb', line 91

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



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

def only_use_row_if &block
  @only_use_row_if = block
end

#output_filenameObject



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

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



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

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



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

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.



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/jirametrics/file_config.rb', line 67

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

    # This will only happen if one of the objects isn't comparable. Seen in production.
    result = -1 if result.nil?
    result
  end
end

#to_date(object) ⇒ Object



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

def to_date object
  to_datetime(object)&.to_date
end

#to_datetime(object) ⇒ Object



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

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



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

def to_integer object
  object.to_i
end

#to_string(object) ⇒ Object



121
122
123
# File 'lib/jirametrics/file_config.rb', line 121

def to_string object
  object.to_s
end