Class: MARCSpec::KVMap

Inherits:
Map
  • Object
show all
Defined in:
lib/marcspec/kvmap.rb

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

#map, #mapname

Class Method Summary collapse

Instance Method Summary collapse

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.

Parameters:

  • filename (String)

    The path to the solrmarc kv map file

Returns:



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

Examples:

kvmap = MARCSpec::KVMap.new("sample_map", {1=>'one'})
kvmap[1]  #=> 'one'
kvmap[2]  #=> nil
kvmap[2, 'Not Found'] #=> 'Not Found'

Parameters:

  • key (Object)

    The key to look up

  • default (Object) (defaults to: nil)

    The value to return if the lookup fails

Returns:

  • (Object)

    The value associated with the passed key, or the



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

#asPPStringString

Produce a configuration file that will round-trip to this object.

this object using MARCSpec::Map#fromFile

Returns:

  • (String)

    A string representation of valid ruby code that can be turned back into



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