Class: ContentDm::Mapper

Inherits:
GenericMapper show all
Extended by:
URI
Defined in:
lib/contentdm/mapper.rb

Overview

A Mapper provides information about field label, visibility, and output order for a specific CONTENTdm collection. This information can be screen-scraped from a CONTENTdm installation, or defined programatically.

Constant Summary collapse

@@maps =
{}
@@auto_init =
true

Constants inherited from GenericMapper

GenericMapper::SaveOptions

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from URI

normalize

Constructor Details

#initialize(base_uri, collection, fields, order = nil) ⇒ Mapper

Creates a map based on the hash of fields



173
174
175
176
177
178
# File 'lib/contentdm/mapper.rb', line 173

def initialize(base_uri, collection, fields, order = nil)
  @base_uri = base_uri
  @collection = collection
  @fields = fields
  @order = order
end

Class Attribute Details

.auto_initObject

Returns the value of attribute auto_init.



98
99
100
# File 'lib/contentdm/mapper.rb', line 98

def auto_init
  @auto_init
end

Instance Attribute Details

#fieldsObject

Returns the value of attribute fields.



94
95
96
# File 'lib/contentdm/mapper.rb', line 94

def fields
  @fields
end

#orderObject

Returns the value of attribute order.



94
95
96
# File 'lib/contentdm/mapper.rb', line 94

def order
  @order
end

Class Method Details

.assign_map(base_uri, collection, *args) ⇒ Object

Assigns a map (either an initialized Map or a Hash/Array combination indicating the field mapping and field order) to a given collection.



151
152
153
154
155
156
157
158
# File 'lib/contentdm/mapper.rb', line 151

def assign_map(base_uri, collection, *args)
  uri = self.normalize(base_uri)
  if args[0].is_a?(self)
    @@maps[self.signature(uri,collection)] = args[0]
  else
    @@maps[self.signature(uri,collection)] = self.new(uri, collection, *args)
  end
end

.from(uri, collection) ⇒ Object

Returns the appropriate Mapper for the given collection at the specified base URI. If it has not been initialized or the collection does not exist, returns nil.



162
163
164
165
166
167
168
169
# File 'lib/contentdm/mapper.rb', line 162

def from(uri, collection)
  if @@auto_init and (collection != 'DC_MAPPING')
    unless self.mapped?(uri, collection)
      self.init_map(uri, collection)
    end
  end
  @@maps[self.signature(uri,collection)]
end

.init_all(base_uri) ⇒ Object

Initializes Mappers for all collections at the specified base URI.



110
111
112
113
114
115
116
117
# File 'lib/contentdm/mapper.rb', line 110

def init_all(base_uri)
  uri = self.normalize(base_uri)
  response = Nokogiri::XML(open(uri.merge('cgi-bin/oai.exe?verb=ListSets')))
  sets = response.search('//xmlns:set/xmlns:setSpec/text()',response.namespaces).collect { |set| set.text }
  sets.each { |set|
    self.init_map(uri, set)
  }
end

.init_map(base_uri, collection) ⇒ Object

Initializes the Mapper for the given collection at the specified base URI.



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/contentdm/mapper.rb', line 120

def init_map(base_uri, collection)
  uri = self.normalize(base_uri)

  dc_map = self.from(uri, 'DC_MAPPING')
  if dc_map.nil?
    fields = open(uri.merge("dc.txt")) { |res| res.read }
    dc_map = {}
    fields.each_line { |field|
      field_properties = field.chomp.split(/:/)
      dc_field = self.normalize_field_name(field_properties[0])
      field_code = field_properties[1]
      dc_map[field_code] = dc_field
    }
    @@maps[self.signature(uri, 'DC_MAPPING')] = dc_map
  end

  fields = open(uri.merge("#{collection}/index/etc/config.txt")) { |res| res.read }
  map = { :fields => Hash.new { |h,k| h[k] = [] }, :order => [] }
  fields.each_line { |field|
    field_properties = field.chomp.split(/:/)
    field_label = field_properties.first
    field_code = field_properties.last
    map[:fields][dc_map[field_code]] << field_label
    map[:order] << field_label unless field_properties[-3] == 'HIDE'
  }
  map[:fields]['dc.identifier'] << 'Permalink'
  @@maps[self.signature(uri,collection)] = self.new(uri, collection, map[:fields], map[:order])
end

.mapped?(uri, collection) ⇒ Boolean

Returns true if a Mapper has been initialized for the given collection at the specified base URI.

Returns:

  • (Boolean)


105
106
107
# File 'lib/contentdm/mapper.rb', line 105

def mapped?(uri, collection)
  return @@maps.include?(self.signature(uri,collection))
end

.mapsObject



100
101
102
# File 'lib/contentdm/mapper.rb', line 100

def maps
  @@maps.keys
end

Instance Method Details

#map(record) ⇒ Object

Returns a hash of field labels and data



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/contentdm/mapper.rb', line 186

def map(record)
  data = record.
  result = {}
  @fields.each_pair { |k,v|
    v.each_with_index { |key,index|
      if data[k]
        value = data[k][index]
        unless value.nil?
          result[key] = value.split(/;\s*/)
          if result[key].length == 1
            result[key] = result[key].first
          end
        end
      end
    }
  }
  result
end

#rename(old_field, new_field) ⇒ Object



180
181
182
183
# File 'lib/contentdm/mapper.rb', line 180

def rename(old_field,new_field)
  @fields.each_pair { |k,v| v.collect! { |name| name == old_field ? new_field : name } }
  @order.collect! { |name| name == old_field ? new_field : name }
end

#to_html(record, vars = {}) ⇒ Object

Serialize the given Record to an HTML string



234
235
236
237
238
239
240
# File 'lib/contentdm/mapper.rb', line 234

def to_html(record, vars = {})
  erb = vars.delete(:template) || DEFAULT_TEMPLATE
  data = self.map(record)
  field_order = @order || []
  template = ERB.new(erb,nil,'%')
  template.result(binding)
end

#to_xml(record, opts = {}) ⇒ Object

Serialize the given Record to a Qualified Dublin Core XML string



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/contentdm/mapper.rb', line 206

def to_xml(record, opts = {})
  save_options = { :encoding => 'UTF-8', :save_with => SaveOptions::AS_XML, :indent => 2 }.merge(opts)
  data = self.map(record)
  field_order = @order || []
  builder = Nokogiri::XML::Builder.new do |doc|
    doc.qualifieddc('xmlns:qdc' => "http://epubs.cclrc.ac.uk/xmlns/qdc/", 
      'xmlns:dc' => "http://purl.org/dc/elements/1.1/", 
      'xmlns:dcterms' => "http://purl.org/dc/terms/") {
        field_order.each { |fieldname|
          field_info = @fields.find { |k,v| v.include?(fieldname) }
          unless field_info.nil? or field_info[0].nil?
            (prefix,tag) = field_info[0].split(/\./)
            index = field_info[1].index(fieldname)
            value = data[fieldname]
            if value.is_a?(Array)
              value = value[index]
            end
            doc[prefix].send("#{tag}_".to_sym) {
              doc.text(value)
            }
          end
        }
      }
  end
  builder.to_xml
end