Class: Inq::ReportCollection

Inherits:
Object
  • Object
show all
Includes:
Okay::WarningHelpers
Defined in:
lib/inq/report_collection.rb

Overview

A class representing a collection of Reports.

Instance Method Summary collapse

Constructor Details

#initialize(config, date) ⇒ ReportCollection

Returns a new instance of ReportCollection.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/inq/report_collection.rb', line 12

def initialize(config, date)
  @config = config

  # If the config is in the old format, convert it to the new one.
  unless @config["repositories"]
    @config["repositories"] = [{
      "repository" => @config.delete("repository"),
      "reports" => @config.delete("reports"),
    }]
  end

  @date = date
  @reports = config["repositories"].map(&method(:fetch_report)).to_h
end

Instance Method Details

#save_allArray<String>

Save all of the reports to the corresponding files.

Returns:

  • (Array<String>)

    An array of file paths.



104
105
106
107
108
109
110
111
# File 'lib/inq/report_collection.rb', line 104

def save_all
  reports = to_h
  reports.each do |file, report|
    File.write(file, report)
  end

  reports.keys
end

#to_hObject

Converts a ReportCollection to a Hash.

Also good for giving programmers nightmares, I suspect.



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/inq/report_collection.rb', line 66

def to_h
  results = {}
  defaults = @config["default_reports"] || {}

  @config["repositories"].map { |repo_config|
    repo = repo_config["repository"]
    config = config_for(repo)

    config["reports"].map { |format, report_config|
      # Sometimes report_data has unused keys, which generates a warning, but
      # we're okay with it, so we wrap it with silence_warnings {}.
      filename = silence_warnings {
        tmp_filename = report_config["filename"] || defaults[format]["filename"]
        tmp_filename % (repo)
      }

      directory = report_config["directory"] || defaults[format]["directory"]
      file = File.join(directory, filename)

      # Export +report+ to the specified +format+ with the specified
      # +frontmatter+.
      frontmatter = report_config["frontmatter"] || {}
      if defaults.has_key?(format) && defaults[format].has_key?("frontmatter")
        frontmatter = defaults[format]["frontmatter"].merge(frontmatter)
      end
      frontmatter = nil if frontmatter == {}

      export = @reports[repo].send("to_#{format}", frontmatter)

      results[file] = export
    }
  }
  results
end