Method: MARCSpec::Map.fromFile
- Defined in:
- lib/marcspec/map.rb
.fromFile(filename) ⇒ Object
Load a map from a file, determining what kind it is along the way.
The file is valid ruby code; see the subclasses KVMap and MutlValueMap for examples.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/marcspec/map.rb', line 36 def self.fromFile filename begin str = File.open(filename).read rescue Exception => e "Problem opening #{filename}: #{e}" raise e end begin rawmap = eval(str) rescue Exception => e log.error "Problem evaluating (with 'eval') file #{filename}: #{e}" raise e end # Derive a name if there isn't one unless rawmap[:mapname] name = File.basename(filename) name.gsub! /\..*$/, '' # remove the extension rawmap[:mapname] = name end case rawmap[:maptype] when :kv return KVMap.new(rawmap[:mapname], rawmap[:map]) when :multi return MultiValueMap.new(rawmap[:mapname], rawmap[:map]) else log.error "Map file #{filename} doesn't seem to be either a KV map or a MuliValueMap according to :maptype (#{rawmap[:maptype]})" raise ArgumentError, "File #{filename} doesn't evaluate to a valid map" end end |