Class: FileConfig
- Inherits:
-
Object
- Object
- FileConfig
- Defined in:
- lib/jirametrics/file_config.rb
Instance Attribute Summary collapse
-
#issues ⇒ Object
readonly
Returns the value of attribute issues.
-
#project_config ⇒ Object
readonly
Returns the value of attribute project_config.
Instance Method Summary collapse
- #assert_only_one_filetype_config_set ⇒ Object
- #children ⇒ Object
- #columns(&block) ⇒ Object
- #file_suffix(suffix = nil) ⇒ Object
- #html_report(&block) ⇒ Object
-
#initialize(project_config:, block:, today: Date.today) ⇒ FileConfig
constructor
A new instance of FileConfig.
- #only_use_row_if(&block) ⇒ Object
- #output_filename ⇒ Object
- #prepare_grid ⇒ Object
- #run ⇒ Object
-
#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.
- #to_date(object) ⇒ Object
- #to_datetime(object) ⇒ Object
- #to_integer(object) ⇒ Object
- #to_string(object) ⇒ Object
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
#issues ⇒ Object (readonly)
Returns the value of attribute issues.
6 7 8 |
# File 'lib/jirametrics/file_config.rb', line 6 def issues @issues end |
#project_config ⇒ Object (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_set ⇒ Object
99 100 101 |
# File 'lib/jirametrics/file_config.rb', line 99 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 |
#children ⇒ Object
132 133 134 135 136 137 |
# File 'lib/jirametrics/file_config.rb', line 132 def children result = [] result << @columns if @columns result << @html_report if @html_report result end |
#columns(&block) ⇒ Object
84 85 86 87 |
# File 'lib/jirametrics/file_config.rb', line 84 def columns &block assert_only_one_filetype_config_set @columns = ColumnsConfig.new file_config: self, block: block end |
#file_suffix(suffix = nil) ⇒ Object
127 128 129 130 |
# File 'lib/jirametrics/file_config.rb', line 127 def file_suffix suffix = nil @file_suffix = suffix unless suffix.nil? @file_suffix end |
#html_report(&block) ⇒ Object
89 90 91 92 93 94 95 96 97 |
# File 'lib/jirametrics/file_config.rb', line 89 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
103 104 105 |
# File 'lib/jirametrics/file_config.rb', line 103 def only_use_row_if &block @only_use_row_if = block end |
#output_filename ⇒ Object
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_grid ⇒ Object
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 |
#run ⇒ Object
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 |
# File 'lib/jirametrics/file_config.rb', line 67 def sort_output all_lines all_lines.each_with_index.sort do |(a, a_idx), (b, b_idx)| result = if a[0] == b[0] a[1..] <=> b[1..] elsif a[0].nil? 1 elsif b[0].nil? -1 else a[0] <=> b[0] end # When objects aren't comparable, preserve original order for a stable sort. result.nil? || result.zero? ? a_idx <=> b_idx : result end.map(&:first) end |
#to_date(object) ⇒ Object
107 108 109 |
# File 'lib/jirametrics/file_config.rb', line 107 def to_date object to_datetime(object)&.to_date end |
#to_datetime(object) ⇒ Object
111 112 113 114 115 116 117 |
# File 'lib/jirametrics/file_config.rb', line 111 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
123 124 125 |
# File 'lib/jirametrics/file_config.rb', line 123 def to_integer object object.to_i end |
#to_string(object) ⇒ Object
119 120 121 |
# File 'lib/jirametrics/file_config.rb', line 119 def to_string object object.to_s end |