Class: Mead::Ead

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Ead

options include :file and :base_url



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/mead/ead.rb', line 8

def initialize(opts={})
  @eadid = opts[:eadid] || nil
  @file    = opts[:file] || nil
  @baseurl = opts[:baseurl] || nil
  @url     = opts[:url] || nil
  @containers = []

  get_ead
  find_eadid unless @eadid
  crawl_for_containers
end

Instance Attribute Details

#baseurlObject

factor out :baseurl, :file and :url into an options object?



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

def baseurl
  @baseurl
end

#c01s_series_answerObject

factor out :baseurl, :file and :url into an options object?



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

def c01s_series_answer
  @c01s_series_answer
end

#containersObject

factor out :baseurl, :file and :url into an options object?



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

def containers
  @containers
end

#docObject

factor out :baseurl, :file and :url into an options object?



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

def doc
  @doc
end

#eadObject

factor out :baseurl, :file and :url into an options object?



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

def ead
  @ead
end

#eadidObject

factor out :baseurl, :file and :url into an options object?



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

def eadid
  @eadid
end

#fileObject

factor out :baseurl, :file and :url into an options object?



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

def file
  @file
end

#series_presentObject

factor out :baseurl, :file and :url into an options object?



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

def series_present
  @series_present
end

#urlObject

factor out :baseurl, :file and :url into an options object?



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

def url
  @url
end

Class Method Details

.to_csv(container_list) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/mead/ead.rb', line 151

def self.to_csv(container_list)
  if CSV.const_defined? :Reader
    csv_class = FasterCSV # old CSV was loaded
  else
    csv_class = CSV # use CSV from 1.9
  end
  csv_string = csv_class.generate do |csv|
    # FIXME
    csv << ['mead','title','series', 'containers']
    #csv << ['mead','title','series']
    container_list.each do |container|
      csv << [container[:mead], container[:title], container[:series], container[:containers].join(', ')]
      #csv << [container[:mead], container[:title], container[:series]]
    end
  end
end

Instance Method Details

#c01sObject



72
73
74
# File 'lib/mead/ead.rb', line 72

def c01s
  @doc.xpath('//xmlns:dsc/xmlns:c01')
end

#c01s_series?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/mead/ead.rb', line 76

def c01s_series?
  @c01s_series_answer ||= c01s.length == series_c01s.length
end

#concat_title(did) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
# File 'lib/mead/ead.rb', line 100

def concat_title(did)
  title = ''
  title << did.xpath('xmlns:unittitle').text if did.xpath('xmlns:unittitle')
  if did.xpath('xmlns:unittitle') and did.xpath('xmlns:unitdate') and !did.xpath('xmlns:unitdate').text.empty?
    title << ', ' << did.xpath('xmlns:unitdate').text
  end
  if did.xpath('xmlns:unitid') and !did.xpath('xmlns:unitid').text.empty?
    title << ' (' + did.xpath('xmlns:unitid').text + ')'
  end
  title
end

#container_type(container) ⇒ Object



136
137
138
139
140
141
142
143
144
145
# File 'lib/mead/ead.rb', line 136

def container_type(container)
  match =''
  CONTAINER_MAPPING.each do |k,v|
    if container.attribute('type').text == v or
        container.attribute('type').text.downcase == v
      match = k
    end
  end
  match
end

#crawl_for_containersObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/mead/ead.rb', line 43

def crawl_for_containers
  c01s.each_with_index do |c, i|
    dids = c.xpath('.//xmlns:container').map{|c| c.parent}.uniq
    #c.xpath('xmlns:c02/xmlns:did').map do |did|
    dids.map do |did|
      info = {}
      if c01s_series?
        info[:series] = i + 1 # if all the c01s are at the file level this fails
      else
        info[:series] = 0
      end
      info[:mead] = create_mead(did, i)
      info[:title] = concat_title(did)
      # FIXME
      info[:containers] = text_containers(did)
      @containers << info
    end
  end
end

#create_mead(did, i) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/mead/ead.rb', line 84

def create_mead(did, i)
  mead = [@eadid.dup]
  if c01s_series?
    mead << "%03d" % (i + 1) #series
  else
    mead << '001'
  end
  begin
    mead << specific_containers(did)
  rescue
    return @mead = mead.flatten.join('-')        
  end
  mead << '001' # stub for first record
  @mead = mead.flatten.join('-')
end

#dupsObject



205
206
207
# File 'lib/mead/ead.rb', line 205

def dups
  meads.inject({}) {|h,v| h[v]=h[v].to_i+1; h}.reject{|k,v| v==1}.keys.sort
end

#find_eadidObject



35
36
37
38
39
40
41
# File 'lib/mead/ead.rb', line 35

def find_eadid
  begin
    @eadid = @doc.xpath('//xmlns:eadid').first.text
  rescue => e
    raise 'Need an eadid and none has been given and it cannot be found in the EAD XML.'
  end
end

#get_eadObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/mead/ead.rb', line 20

def get_ead
  if @eadid.nil? and @url.nil? and @file.nil? and @baseurl
    raise 'Cannot get EAD based on params.'
  end
  if @file and @file.is_a? File
    @file.rewind if @file.eof?
    @ead = @file.read
  elsif @url
    @ead = open(@url).read
  elsif @baseurl
    @ead = open(File.join(@baseurl, @eadid + '.xml')).read
  end
  @doc = Nokogiri::XML(@ead)
end

#invalidObject



200
201
202
203
# File 'lib/mead/ead.rb', line 200

def invalid
  duplicates = dups
  @containers.select{|container| duplicates.include?(container[:mead])}
end

#long_meadsObject



184
185
186
# File 'lib/mead/ead.rb', line 184

def long_meads
  unique_meads.select{|m| m.split('-').length > 2}
end

#make_box(container, padding = 4) ⇒ Object



129
130
131
132
133
134
# File 'lib/mead/ead.rb', line 129

def make_box(container, padding=4)
  # FIXME: pad based on first part of range for folder +++
  padder = "%0" + padding.to_s + 's'
  text = (padder % container.text).gsub(' ','0').gsub('.','_').gsub('-', '~')
  container_type(container) + text
end

#meadsObject



209
210
211
# File 'lib/mead/ead.rb', line 209

def meads
  @containers.collect{|container| container[:mead]}
end

#series_c01sObject



80
81
82
# File 'lib/mead/ead.rb', line 80

def series_c01s
  @doc.xpath("//xmlns:dsc/xmlns:c01[@level='series']")
end

#short_meadsObject



188
189
190
# File 'lib/mead/ead.rb', line 188

def short_meads
  unique_meads.select{|m| m.split('-').length <= 2}
end

#short_meads?Boolean

Returns:

  • (Boolean)


192
193
194
195
196
197
198
# File 'lib/mead/ead.rb', line 192

def short_meads?
  if unique_meads.length == long_meads.length
    false
  else
    true
  end
end

#specific_containers(did) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/mead/ead.rb', line 112

def specific_containers(did)
  containers = did.xpath('xmlns:container')
  container_values = []
  if containers.length == 1
    container_values << make_box(did.xpath('xmlns:container')[0])
    container_values << '000'
  elsif containers.length == 2
    container_values << make_box(did.xpath('xmlns:container')[0])
    container_values << make_box(did.xpath('xmlns:container')[1],3)
  elsif containers.length > 2
    raise "I can't create a mead identifier with more than 2 containers in a did!"
  else
    raise "Do we really have zero containers?!"
  end
  return container_values
end

#text_containers(did) ⇒ Object



63
64
65
66
67
68
69
70
# File 'lib/mead/ead.rb', line 63

def text_containers(did)
  did.xpath('xmlns:container').map do |container|
    text = ''
    text << container.attribute('type').text + ' ' if container.attribute('type')
    text << container.text if container.text
    text
  end
end

#to_csvObject



147
148
149
# File 'lib/mead/ead.rb', line 147

def to_csv
  Mead::Ead.to_csv(self.containers)
end

#unique_meadsObject



180
181
182
# File 'lib/mead/ead.rb', line 180

def unique_meads
  @containers.collect{|container| container[:mead]}.uniq
end

#valid?Boolean

Returns:

  • (Boolean)


168
169
170
171
172
173
174
175
176
177
178
# File 'lib/mead/ead.rb', line 168

def valid?
  if unique_meads.length == @containers.length
    if short_meads?
      false
    else
      true
    end
  else
    false
  end
end