Class: MARCSpec::KVMap
Overview
A KVMap is, when push comes to shove, just a hash with a name, and the option of adding a default value for each lookup.
The map portion of a kvmap is simply a hash.
Instance Attribute Summary
Attributes inherited from Map
Class Method Summary collapse
-
.from_solrmarc_file(filename) ⇒ MARCSpec::KVMap
Translate from a solrmarc map file that has already been determined to be a KV map.
Instance Method Summary collapse
-
#[](key, default = nil) ⇒ Object
Basic lookup which takes a lookup key and an optional default value, which will be returned iff the map doesn't have the passed key.
-
#[]=(key, value) ⇒ Object
(also: #add)
Set an element in the map, just like for a regular hash.
-
#asPPString ⇒ String
Produce a configuration file that will round-trip to this object.
Methods inherited from Map
#==, fromFile, fromHash, fromPPString, #initialize, #pretty_print
Constructor Details
This class inherits a constructor from MARCSpec::Map
Class Method Details
.from_solrmarc_file(filename) ⇒ MARCSpec::KVMap
Translate from a solrmarc map file that has already been determined to be a KV map
Uses the underlying java Properties class to avoid having to rewrite all the esacping logic.
74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/marcspec/kvmap.rb', line 74 def self.from_solrmarc_file filename mapname = File.basename(filename).sub(/\..+?$/, '') map = {} File.open(filename) do |smf| prop = Java::java.util.Properties.new prop.load(smf.to_inputstream) prop.each do |k,v| map[k] = v end end return self.new(mapname, map) end |
Instance Method Details
#[](key, default = nil) ⇒ Object
Basic lookup which takes a lookup key and an optional default value, which will be returned iff the map doesn't have the passed key
default value
31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/marcspec/kvmap.rb', line 31 def [] key, default=nil if @map.has_key? key @map[key] else if default == :passthrough return key else return default end end end |
#[]=(key, value) ⇒ Object Also known as: add
Set an element in the map, just like for a regular hash
44 45 46 |
# File 'lib/marcspec/kvmap.rb', line 44 def []= key, value @map[key] = value end |
#asPPString ⇒ String
Produce a configuration file that will round-trip to this object.
this object using MARCSpec::Map#fromFile
55 56 57 58 59 60 61 62 63 |
# File 'lib/marcspec/kvmap.rb', line 55 def asPPString s = StringIO.new s.print "{\n :maptype=>:kv,\n :mapname=>" PP.singleline_pp(@mapname, s) s.print ",\n :map => " PP.pp(@map, s) s.puts "\n}" return s.string end |