Sha256: 106addb75d7135cf4a388f72b13113795b3b0fed390c3b69cffc26cac323dca4

Contents?: true

Size: 1.68 KB

Versions: 3

Compression:

Stored size: 1.68 KB

Contents

require 'marcspec/map'
require 'pp'


module MARCSpec

  # A MultiValueMap (in this conectex) is an array of duples of the form
  # [thingToMatch, 'Value'] (or [thingToMatch, [array,of,values]])
  # along with an associated name.
  #
  # Accessing via [] will give you an array of non-nil values that match (via ===)
  # the corresponding keys.
  #
  # Keys can be either strings or regular expressions (e.g., /^Bil/). 
  #
  # Again, note that if several keys are === to the passed argument, all the values will be returned. 
  
  class MultiValueMap   < Map
    
    attr_accessor :mapname,:map
    

    def [] key, default=nil
      rv =  @map.map {|pv| pv[0] === key ? pv[1] : nil}
      rv.flatten!
      rv.compact!
      rv.uniq!
      if rv.size > 0
        return rv
      else
        return [default]
      end
    end
    
    def self.from_solrmarc_file filename
      mapname = File.basename(filename).sub(/\..+?$/, '')
      kvlist = []
      File.open(filename) do |f|
        prop = Java::java.util.Properties.new
        prop.load(f.to_inputstream)
        prop.each do |patstring,kv|
          unless patstring =~ /^pattern/ and kv =~ /.+=>.+/
            $LOG.warn "MultiValueMap import skipping weird line in #{filename}\n  #{l}"
            next
          end
          match = /^\s*(.+?)\s*=>\s*(.+?)\s*$/.match(kv)
          kvlist << [Regexp.new(match[1]), match[2]]
        end
      end        
      return self.new(mapname, kvlist)
    end
        
    def asPPString
      s = StringIO.new
      s.print "{\n :maptype=>:multi,\n :mapname=>"
      PP.singleline_pp(@mapname, s)
      s.print ",\n :map => "
      PP.pp(@map, s)
      s.puts "\n}"
      return s.string
    end
  end
end
      

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
marcspec-1.0.0 lib/marcspec/multivaluemap.rb
marcspec-0.8.1 lib/marcspec/multivaluemap.rb
marcspec-0.7.3 lib/marcspec/multivaluemap.rb