Class: OcflTools::OcflResults

Inherits:
Object
  • Object
show all
Defined in:
lib/ocfl_tools/ocfl_results.rb

Overview

Class for collating results of validation and verification checks.

Instance Method Summary collapse

Constructor Details

#initializeOcflResults

Returns a new instance of OcflResults.



6
7
8
9
10
11
12
13
14
# File 'lib/ocfl_tools/ocfl_results.rb', line 6

def initialize
  @my_results             = {}
  @my_results['error']    = {}
  @my_results['warn']     = {}
  @my_results['info']     = {}
  @my_results['ok']       = {}

  @my_contexts = {}
end

Instance Method Details

#add_results(source) ⇒ OcflTools::OcflResults

Given another OcflTools::OcflResults instance, copy that object’s data into this one. Used to ‘roll up’ Results from different levels of validation or process into a single results instance.

Parameters:

Returns:



233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/ocfl_tools/ocfl_results.rb', line 233

def add_results(source)
  unless source.is_a?(OcflTools::OcflResults)
    raise "#{source} is not a Results object!"
  end

  source.get_ok.each do |code, contexts|
    contexts.each do |context, descriptions|
      descriptions.each do |description|
        ok(code, context, description)
      end
    end
  end

  source.get_info.each do |code, contexts|
    contexts.each do |context, descriptions|
      descriptions.each do |description|
        info(code, context, description)
      end
    end
  end

  source.get_warnings.each do |code, contexts|
    contexts.each do |context, descriptions|
      descriptions.each do |description|
        warn(code, context, description)
      end
    end
  end

  source.get_errors.each do |code, contexts|
    contexts.each do |context, descriptions|
      descriptions.each do |description|
        error(code, context, description)
      end
    end
  end
  self
end

#allHash

Convenience method for obtaining a hash of results.

Returns:

  • (Hash)

    of results stored in this instance.



24
25
26
# File 'lib/ocfl_tools/ocfl_results.rb', line 24

def all
  @my_results
end

#error(code, context, description) ⇒ String

Creates an ‘Error’ message in the object with the specified code and context.

Parameters:

  • code (String)

    the appropriate ‘error’ code for this event.

  • context (String)

    the process or class that is creating this event.

  • description (String)

    the details of this specific event.

Returns:

  • (String)

    description of posted Error statement.



214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/ocfl_tools/ocfl_results.rb', line 214

def error(code, context, description)
  if @my_results['error'].key?(code) == false
    @my_results['error'][code] = {}
  end
  if @my_results['error'][code].key?(context) == false
    @my_results['error'][code][context] = []
  end
  # Only put unique values into description
  if @my_results['error'][code][context].include?(description)
    return description
  else
    @my_results['error'][code][context] = (@my_results['error'][code][context] << description)
  end
end

#error_countInteger

Gets the total number of error events contained within this instance.

Returns:

  • (Integer)

    the number of errors.



109
110
111
112
113
114
115
116
117
# File 'lib/ocfl_tools/ocfl_results.rb', line 109

def error_count
  my_count = 0
  @my_results['error'].each do |_code, contexts|
    contexts.each do |_context, description|
      my_count += description.size
    end
  end
  my_count
end

#get_context(my_context) ⇒ Hash

Get all results for a specific context (e.g. ‘verify_checksums’)

Parameters:

  • my_context (String)

    a string value of the context (e.g. ‘verify_checksums’) to query

Returns:

  • (Hash)

    a hash of results for the specified context, arranged by ‘code’ => [ descriptions ].



103
104
105
# File 'lib/ocfl_tools/ocfl_results.rb', line 103

def get_context(my_context)
  get_contexts[my_context]
end

#get_contextsHash

Processes all of @my_results and creates a nested hash of context => level => code => [ descriptions ] Useful if you want to get all the info/error/warn/ok results for a specific context.

Returns:

  • (Hash)

    a nested hash of results, organized with ‘context’ as a top level key.



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
# File 'lib/ocfl_tools/ocfl_results.rb', line 66

def get_contexts
  @my_results.each do |level, codes| # levels are warn, info, ok, error
    codes.each do |code, contexts|
      contexts.each do |context, description|
        # puts "got  : #{level} #{code} #{context} #{description}"
        # puts "want : #{context} #{level} #{code} #{description}"
        if @my_contexts.key?(context)
          my_levels = @my_contexts[context]
          if my_levels.key?(level)
            my_codes = my_levels[level]
            if my_codes.key?(code)
              # what should I do here? Nothing, apparently, as it's soft-copied already.
            else
              my_codes[code] = description # new code for this level! Add it.
            end
          else
            # if the context key already exists, but the level key
            # does not, we can add everything beneath context in one go.
            my_levels[level] = { code => description }
          end
        else
          # If the context (the top level key) doesn't exist already,
          # we can just slam everything in at once.
          @my_contexts[context] = {}
          my_level = {}
          my_level[code] = description
          @my_contexts[context] = { level => my_level }
        end
      end
    end
  end
  @my_contexts
end

#get_errorsHash

Returns a hash of all the ‘error’ entries stored in this instance.

Returns:

  • (Hash)

    a hash of all the ‘error’ entries stored in this instance.



43
44
45
# File 'lib/ocfl_tools/ocfl_results.rb', line 43

def get_errors
  @my_results['error']
end

#get_infoHash

Returns a hash of all the ‘info’ entries stored in this instance.

Returns:

  • (Hash)

    a hash of all the ‘info’ entries stored in this instance.



53
54
55
# File 'lib/ocfl_tools/ocfl_results.rb', line 53

def get_info
  @my_results['info']
end

#get_okHash

Returns a hash of all the ‘OK’ entries stored in this instance.

Returns:

  • (Hash)

    a hash of all the ‘OK’ entries stored in this instance.



58
59
60
# File 'lib/ocfl_tools/ocfl_results.rb', line 58

def get_ok
  @my_results['ok']
end

#get_warningsHash

Returns a hash of all the ‘warn’ entries stored in this instance.

Returns:

  • (Hash)

    a hash of all the ‘warn’ entries stored in this instance.



48
49
50
# File 'lib/ocfl_tools/ocfl_results.rb', line 48

def get_warnings
  @my_results['warn']
end

#info(code, context, description) ⇒ String

Creates an ‘info’ message in the object with the specified code and context.

Parameters:

  • code (String)

    the appropriate ‘Info’ code for this event.

  • context (String)

    the process or class that is creating this event.

  • description (String)

    the details of this specific event.

Returns:

  • (String)

    description of posted Info statement.



178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/ocfl_tools/ocfl_results.rb', line 178

def info(code, context, description)
  @my_results['info'][code] = {} if @my_results['info'].key?(code) == false
  if @my_results['info'][code].key?(context) == false
    @my_results['info'][code][context] = []
  end
  # Only put unique values into description
  if @my_results['info'][code][context].include?(description)
    return description
  else
    @my_results['info'][code][context] = (@my_results['info'][code][context] << description)
  end
end

#info_countInteger

Gets the total number of ‘info’ events contained within this instance.

Returns:

  • (Integer)

    the number of informational messages.



133
134
135
136
137
138
139
140
141
# File 'lib/ocfl_tools/ocfl_results.rb', line 133

def info_count
  my_count = 0
  @my_results['info'].each do |_code, contexts|
    contexts.each do |_context, description|
      my_count += description.size
    end
  end
  my_count
end

#ok(code, context, description) ⇒ String

Creates an ‘OK’ message in the object with the specified code and context.

Parameters:

  • code (String)

    the appropriate ‘ok’ code for this event.

  • context (String)

    the process or class that is creating this event.

  • description (String)

    the details of this specific event.

Returns:

  • (String)

    description of posted OK statement.



160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/ocfl_tools/ocfl_results.rb', line 160

def ok(code, context, description)
  @my_results['ok'][code] = {} if @my_results['ok'].key?(code) == false
  if @my_results['ok'][code].key?(context) == false
    @my_results['ok'][code][context] = []
  end
  # Only put unique values into description
  if @my_results['ok'][code][context].include?(description)
    return description
  else
    @my_results['ok'][code][context] = (@my_results['ok'][code][context] << description)
  end
end

#ok_countInteger

Gets the total number of ‘ok’ events contained within this instance.

Returns:

  • (Integer)

    the number of OK messages.



145
146
147
148
149
150
151
152
153
# File 'lib/ocfl_tools/ocfl_results.rb', line 145

def ok_count
  my_count = 0
  @my_results['ok'].each do |_code, contexts|
    contexts.each do |_context, description|
      my_count += description.size
    end
  end
  my_count
end

Convenience method to print out the results hash to stdout.



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/ocfl_tools/ocfl_results.rb', line 29

def print
  @my_results.each do | level, status_codes |
    puts "#{level.upcase}" unless status_codes.size == 0
    status_codes.each do | code, contexts |
      contexts.each do | context, descriptions |
        descriptions.each do | desc |
          puts "  #{code}:#{context}:#{desc}"
        end
      end
    end
  end
end

#resultsHash

Convenience method for obtaining a hash of results.

Returns:

  • (Hash)

    of results stored in this instance.



18
19
20
# File 'lib/ocfl_tools/ocfl_results.rb', line 18

def results
  @my_results
end

#warn(code, context, description) ⇒ String

Creates a ‘Warn’ message in the object with the specified code and context.

Parameters:

  • code (String)

    the appropriate ‘warn’ code for this event.

  • context (String)

    the process or class that is creating this event.

  • description (String)

    the details of this specific event.

Returns:

  • (String)

    description of posted Warn statement.



196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/ocfl_tools/ocfl_results.rb', line 196

def warn(code, context, description)
  @my_results['warn'][code] = {} if @my_results['warn'].key?(code) == false
  if @my_results['warn'][code].key?(context) == false
    @my_results['warn'][code][context] = []
  end
  # Only put unique values into description
  if @my_results['warn'][code][context].include?(description)
    return description
  else
    @my_results['warn'][code][context] = (@my_results['warn'][code][context] << description)
  end
end

#warn_countInteger

Gets the total number of warning events contained within this instance.

Returns:

  • (Integer)

    the number of warnings.



121
122
123
124
125
126
127
128
129
# File 'lib/ocfl_tools/ocfl_results.rb', line 121

def warn_count
  my_count = 0
  @my_results['warn'].each do |_code, contexts|
    contexts.each do |_context, description|
      my_count += description.size
    end
  end
  my_count
end