Class: MARCSpec::Map
- Inherits:
-
Object
- Object
- MARCSpec::Map
- Includes:
- Logback::Simple
- Defined in:
- lib/marcspec/map.rb
Overview
A Map is just a named lookup table. The access (via []) takes, in adition to a key, an optional default value to return (e.g., val = map[key, defaultIfNotFound])
We don’t have the default be a part of the map because it might be used in several different contexts.
NOTE: THIS IS AN ABSTRACT SUPERCLASS. DO NOT INSTANTIATE IT DIRECTLY
Direct Known Subclasses
Instance Attribute Summary collapse
-
#map ⇒ Object
Returns the value of attribute map.
-
#mapname ⇒ Object
Returns the value of attribute mapname.
Class Method Summary collapse
-
.fromFile(filename) ⇒ Object
Load a map from a file, determining what kind it is along the way.
-
.fromHash(rawmap) ⇒ Object
Produce a map from the data structure produced by asPPString.
-
.fromPPString(str) ⇒ Object
Take the output of pretty_print and eval it to get rawmap; pass it tp fromHash to get the map object.
Instance Method Summary collapse
-
#==(other) ⇒ Object
Check for map equality.
-
#initialize(mapname, map) ⇒ Map
constructor
Create a new map.
-
#pretty_print(pp) ⇒ Object
Generic pretty_print; used mostly for translating from solrmarc.
Constructor Details
#initialize(mapname, map) ⇒ Map
Create a new map. The passed map is either a standard hash (KVMap) or a list of duples (for a MultiValueMap)
22 23 24 25 |
# File 'lib/marcspec/map.rb', line 22 def initialize(mapname, map) @mapname = mapname @map = map end |
Instance Attribute Details
#map ⇒ Object
Returns the value of attribute map.
15 16 17 |
# File 'lib/marcspec/map.rb', line 15 def map @map end |
#mapname ⇒ Object
Returns the value of attribute mapname.
15 16 17 |
# File 'lib/marcspec/map.rb', line 15 def mapname @mapname end |
Class Method Details
.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.
34 35 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 |
# File 'lib/marcspec/map.rb', line 34 def self.fromFile filename begin str = File.open(filename).read rescue Exception => e Logback::Simple::Logger.fatal "Problem opening #{filename}: #{e.}" raise e end begin rawmap = eval(str) rescue Exception => e Logback::Simple::Logger.fatal "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 Logback::Simple::Logger.fatal "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 |
.fromHash(rawmap) ⇒ Object
Produce a map from the data structure produced by asPPString
81 82 83 |
# File 'lib/marcspec/map.rb', line 81 def self.fromHash rawmap return self.new(rawmap[:mapname], rawmap[:map]) end |
.fromPPString(str) ⇒ Object
Take the output of pretty_print and eval it to get rawmap; pass it tp fromHash to get the map object
87 88 89 90 |
# File 'lib/marcspec/map.rb', line 87 def self.fromPPString str rawmap = eval(str) return self.fromHash rawmap end |
Instance Method Details
#==(other) ⇒ Object
Check for map equality
70 71 72 |
# File 'lib/marcspec/map.rb', line 70 def == other return ((other.mapname == @mapname) and (other.map == @map)) end |
#pretty_print(pp) ⇒ Object
Generic pretty_print; used mostly for translating from solrmarc
75 76 77 |
# File 'lib/marcspec/map.rb', line 75 def pretty_print pp pp.pp eval(self.asPPString) end |