Class: Hyrax::Ingest::Reporting::Report

Inherits:
Object
  • Object
show all
Defined in:
lib/hyrax/ingest/reporting/report.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeReport

Returns a new instance of Report.



11
12
13
# File 'lib/hyrax/ingest/reporting/report.rb', line 11

def initialize
  @stat = default_stat
end

Instance Attribute Details

#statObject (readonly)

Returns the value of attribute stat.



9
10
11
# File 'lib/hyrax/ingest/reporting/report.rb', line 9

def stat
  @stat
end

Instance Method Details

#default_statObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/hyrax/ingest/reporting/report.rb', line 15

def default_stat
  # Initialize a FunctionalHash to serve as a stat tracker, and
  # add some default values in the same way you would a Hash.
  @stat = FunctionalHash.new.tap do |stat|
    # Stores an array of all SIP paths.
    stat[:sip_paths] = []

    # Stores a list of all files from SIPs that are part of the ingest.
    stat[:files] = []

    # Stores a hash where keys are Fetcher classes, and values are
    # the number of occurrences of missing rquired values.
    stat[:missing_required_values] = {}

    stat[:total_missing_required_values] = Proc.new do |s|
      stat[:missing_required_values].reduce(0) do |total, key_and_value|
        # When reducing a Hash, the 2nd arg to the block is a
        # 2-element array, where the 1st element is the key, and the
        # 2nd element is the value the key points to.
        occurrences = key_and_value.last
        total + occurrences.count
      end
    end

    # Filters the :missing_required_values hash to those for XML files.
    # stat[:xml_files_missing_required_values] = Proc.new do |s|
    #   s[:missing_required_values].select { |fetcher_class, params| fetcher_class.to_s =~ /XMLFile$/ }
    # end

    # Define a functional hash value that returns the count of the given key.
    stat[:count] = Proc.new do |s, key_to_count|
      s[key_to_count].respond_to?(:count) ? s[key_to_count].count : 0
    end

    stat[:models_saved] = []
    stat[:models_failed] = []
  end
end

#errorsObject



69
70
71
# File 'lib/hyrax/ingest/reporting/report.rb', line 69

def errors
  @errors ||= []
end

#failed?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/hyrax/ingest/reporting/report.rb', line 73

def failed?
  !errors.empty?
end

#failed_with(error) ⇒ Object



65
66
67
# File 'lib/hyrax/ingest/reporting/report.rb', line 65

def failed_with(error)
  errors << error
end

#render(template_path: nil) ⇒ Object



54
55
56
57
58
# File 'lib/hyrax/ingest/reporting/report.rb', line 54

def render(template_path: nil)
  template_path ||= Reporting.config.default_template_path
  template_content = File.read(File.expand_path(template_path))
  ERB.new(template_content).result(binding)
end

#write_to_file(filename: nil, template_path: nil) ⇒ Object



60
61
62
63
# File 'lib/hyrax/ingest/reporting/report.rb', line 60

def write_to_file(filename: nil, template_path: nil)
  filename ||= Reporting.config.default_output_file
  File.write(filename, render(template_path: template_path))
end