Class: Mead::Ead
- Inherits:
-
Object
- Object
- Mead::Ead
- Defined in:
- lib/mead/ead.rb
Instance Attribute Summary collapse
-
#baseurl ⇒ Object
factor out :baseurl, :file and :url into an options object?.
-
#c01s_series_answer ⇒ Object
factor out :baseurl, :file and :url into an options object?.
-
#containers ⇒ Object
factor out :baseurl, :file and :url into an options object?.
-
#doc ⇒ Object
factor out :baseurl, :file and :url into an options object?.
-
#ead ⇒ Object
factor out :baseurl, :file and :url into an options object?.
-
#eadid ⇒ Object
factor out :baseurl, :file and :url into an options object?.
-
#file ⇒ Object
factor out :baseurl, :file and :url into an options object?.
-
#series_present ⇒ Object
factor out :baseurl, :file and :url into an options object?.
-
#url ⇒ Object
factor out :baseurl, :file and :url into an options object?.
Class Method Summary collapse
Instance Method Summary collapse
- #c01s ⇒ Object
- #c01s_series? ⇒ Boolean
- #concat_title(did) ⇒ Object
- #container_type(container) ⇒ Object
- #crawl_for_containers ⇒ Object
- #create_mead(did, i) ⇒ Object
- #dups ⇒ Object
- #find_eadid ⇒ Object
- #get_ead ⇒ Object
-
#initialize(opts = {}) ⇒ Ead
constructor
options include :file and :base_url.
- #invalid ⇒ Object
- #long_meads ⇒ Object
- #make_box(container, padding = 4) ⇒ Object
- #meads ⇒ Object
- #series_c01s ⇒ Object
- #short_meads ⇒ Object
- #short_meads? ⇒ Boolean
- #specific_containers(did) ⇒ Object
- #text_containers(did) ⇒ Object
- #to_csv ⇒ Object
- #unique_meads ⇒ Object
- #valid? ⇒ Boolean
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
#baseurl ⇒ Object
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_answer ⇒ Object
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 |
#containers ⇒ Object
factor out :baseurl, :file and :url into an options object?
4 5 6 |
# File 'lib/mead/ead.rb', line 4 def containers @containers end |
#doc ⇒ Object
factor out :baseurl, :file and :url into an options object?
4 5 6 |
# File 'lib/mead/ead.rb', line 4 def doc @doc end |
#ead ⇒ Object
factor out :baseurl, :file and :url into an options object?
4 5 6 |
# File 'lib/mead/ead.rb', line 4 def ead @ead end |
#eadid ⇒ Object
factor out :baseurl, :file and :url into an options object?
4 5 6 |
# File 'lib/mead/ead.rb', line 4 def eadid @eadid end |
#file ⇒ Object
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_present ⇒ Object
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 |
#url ⇒ Object
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
#c01s ⇒ Object
72 73 74 |
# File 'lib/mead/ead.rb', line 72 def c01s @doc.xpath('//xmlns:dsc/xmlns:c01') end |
#c01s_series? ⇒ 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_containers ⇒ Object
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 |
#dups ⇒ Object
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_eadid ⇒ Object
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_ead ⇒ Object
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 |
#invalid ⇒ Object
200 201 202 203 |
# File 'lib/mead/ead.rb', line 200 def invalid duplicates = dups @containers.select{|container| duplicates.include?(container[:mead])} end |
#long_meads ⇒ Object
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 |
#meads ⇒ Object
209 210 211 |
# File 'lib/mead/ead.rb', line 209 def meads @containers.collect{|container| container[:mead]} end |
#series_c01s ⇒ Object
80 81 82 |
# File 'lib/mead/ead.rb', line 80 def series_c01s @doc.xpath("//xmlns:dsc/xmlns:c01[@level='series']") end |
#short_meads ⇒ Object
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
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_csv ⇒ Object
147 148 149 |
# File 'lib/mead/ead.rb', line 147 def to_csv Mead::Ead.to_csv(self.containers) end |
#unique_meads ⇒ Object
180 181 182 |
# File 'lib/mead/ead.rb', line 180 def unique_meads @containers.collect{|container| container[:mead]}.uniq end |
#valid? ⇒ 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 |