Class: Mead::Identifier

Inherits:
Object
  • Object
show all
Includes:
Validations
Defined in:
lib/mead/identifier.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Validations

#errors, included, #valid?, #valid_format?, #validate, #validate_format

Constructor Details

#initialize(mead, ead_location = nil) ⇒ Identifier

If a location is given then extraction can take place



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/mead/identifier.rb', line 15

def initialize(mead, ead_location=nil)
  @mead = mead
  @metadata = nil
  parse_mead 'eadid', 'series', 'container', 'folder', 'sequence'
  @ead_location = parse_ead_location(ead_location)
  split_container
  split_folder
  split_page
  clean_zeros 'series', 'sequence', 'page'      
  self
end

Instance Attribute Details

#containerObject

Returns the value of attribute container.



4
5
6
# File 'lib/mead/identifier.rb', line 4

def container
  @container
end

#ead_locationObject

Returns the value of attribute ead_location.



4
5
6
# File 'lib/mead/identifier.rb', line 4

def ead_location
  @ead_location
end

#eadidObject

Returns the value of attribute eadid.



4
5
6
# File 'lib/mead/identifier.rb', line 4

def eadid
  @eadid
end

#folderObject

Returns the value of attribute folder.



4
5
6
# File 'lib/mead/identifier.rb', line 4

def folder
  @folder
end

#meadObject

Returns the value of attribute mead.



4
5
6
# File 'lib/mead/identifier.rb', line 4

def mead
  @mead
end

#metadataObject

Returns the value of attribute metadata.



4
5
6
# File 'lib/mead/identifier.rb', line 4

def 
  @metadata
end

#pageObject

Returns the value of attribute page.



4
5
6
# File 'lib/mead/identifier.rb', line 4

def page
  @page
end

#sequenceObject

Returns the value of attribute sequence.



4
5
6
# File 'lib/mead/identifier.rb', line 4

def sequence
  @sequence
end

#seriesObject

Returns the value of attribute series.



4
5
6
# File 'lib/mead/identifier.rb', line 4

def series
  @series
end

Instance Method Details

#clean_zeros(*args) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/mead/identifier.rb', line 59

def clean_zeros(*args)
  args.each do |field|
    instance_var = instance_variable_get('@' + field)
    if instance_var
      cleaned_value = strip_zeros(instance_var)
      instance_variable_set('@' + field, cleaned_value)
    end
  end
end

#container_number_transforms(string) ⇒ Object



55
56
57
# File 'lib/mead/identifier.rb', line 55

def container_number_transforms(string)
  string.gsub('_','.').gsub('~', '-').gsub(/^0*/,'')
end

#extractObject



97
98
99
100
# File 'lib/mead/identifier.rb', line 97

def extract
  @metadata = Mead::Extractor.new(self).extract
  self
end

#parse_ead_location(loc) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/mead/identifier.rb', line 81

def parse_ead_location(loc)
  return nil if loc.nil?
  if loc
    if loc.is_a? File 
      loc.rewind if loc.eof?
      @ead_location = loc
    elsif loc.include?('http://')
      if loc.include?(@eadid)
        @ead_location = loc
      else
        @ead_location = File.join(loc, @eadid + '.xml')
      end
    end
  end
end

#parse_mead(*args) ⇒ Object



27
28
29
30
31
32
# File 'lib/mead/identifier.rb', line 27

def parse_mead(*args)
  parts = @mead.split('-')
  args.each_with_index do |field, i|
    instance_variable_set('@' + field, parts[i])
  end    
end

#split_containerObject



34
35
36
37
38
# File 'lib/mead/identifier.rb', line 34

def split_container
  type = CONTAINER_MAPPING[ @container[0,2] ]
  number = strip_zeros(container_number_transforms(@container[2,10]))
  @container = {:type=> type, :number=> number}
end

#split_folderObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/mead/identifier.rb', line 40

def split_folder
  if CONTAINER_MAPPING.keys.include?(@folder[0,2])
    type = CONTAINER_MAPPING[ @folder[0,2] ] 
    number = strip_zeros(container_number_transforms(@folder[2,10]))
  else
    type = 'folder'
    number = strip_zeros(container_number_transforms(@folder))
  end
  if number.nil? or (number and number.empty?)
    @folder = nil
  else
    @folder = {:type=> type, :number=> number}
  end      
end

#split_pageObject



77
78
79
# File 'lib/mead/identifier.rb', line 77

def split_page
  @sequence, @page = sequence.split('_')
end

#strip_zeros(num) ⇒ Object



69
70
71
72
73
74
75
# File 'lib/mead/identifier.rb', line 69

def strip_zeros(num)
  if num == '000'
    '0'
  else
    num.sub(/^0+/,'')
  end
end