Class: Mead::EadValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/mead/ead_validator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(directory) ⇒ EadValidator

Creates a new EadValidator when given the path to a directory as a String



6
7
8
9
10
11
# File 'lib/mead/ead_validator.rb', line 6

def initialize(directory)
  @directory = directory
  @valid = []
  @invalid = []
  @invalid_full = []
end

Instance Attribute Details

#directoryObject

Returns the value of attribute directory.



3
4
5
# File 'lib/mead/ead_validator.rb', line 3

def directory
  @directory
end

#invalidObject

Returns the value of attribute invalid.



3
4
5
# File 'lib/mead/ead_validator.rb', line 3

def invalid
  @invalid
end

#invalid_fullObject

Returns the value of attribute invalid_full.



3
4
5
# File 'lib/mead/ead_validator.rb', line 3

def invalid_full
  @invalid_full
end

#validObject

Returns the value of attribute valid.



3
4
5
# File 'lib/mead/ead_validator.rb', line 3

def valid
  @valid
end

Instance Method Details

#metadataObject



41
42
43
# File 'lib/mead/ead_validator.rb', line 41

def 
  {:valid => @valid.sort, :invalid => @invalid.sort}
end

#record_invalid(eadid, ead, error = nil) ⇒ Object



36
37
38
39
# File 'lib/mead/ead_validator.rb', line 36

def record_invalid(eadid, ead, error=nil)
  @invalid << eadid
  @invalid_full << {:eadid => eadid, :error => error, :dups => ead.dups, :containers => ead.invalid}
end

#validate!Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/mead/ead_validator.rb', line 13

def validate!
  files = Dir.glob(File.join(@directory, '*.xml')).sort
  threads = []
  files.map do |path|
    threads << Thread.new(path) do |path_t|
      eadid = File.basename(path_t, '.xml')
      begin
        ead = Mead::Ead.new({:file => File.open(path_t), :eadid => eadid})
      rescue => e
        record_invalid(eadid, ead, e)
        next
      end
      if ead.valid?
        @valid << eadid
      else
        record_invalid(eadid, ead)
      end
    end
  end
  threads.each { |thread|  thread.join }
  
end