class MARCSpec::SpecSet

Re-open SpecSet to add the necessary methods

A collection of the solr field specifications and maps necessary to turn a MARC record into a set of key=>value pairs suitable for sending to Solr

Attributes

benchmarks[RW]
solrfieldspecs[RW]
tmaps[RW]

Public Class Methods

new() click to toggle source

Generic new

# File lib/marcspec/specset.rb, line 39
def initialize
  @tmaps = {}
  @solrfieldspecs = []
  @benchmarks = {}
end

Public Instance Methods

<<(solrfieldspec)
Alias for: add_spec
add_map(map) click to toggle source

Add a map to self, using its name (map#mapname) as a key @param [MARCSpec::Map] map the map to add.

# File lib/marcspec/specset.rb, line 73
def add_map map
  self.tmaps[map.mapname] = map
end
add_spec(solrfieldspec) click to toggle source

Add a spec, making sure there's a slot in the benchmarking stats to keep track of it

@param [MARCSpec::SolrFieldSpec] solrfieldspec The spec to add

# File lib/marcspec/specset.rb, line 143
def add_spec solrfieldspec
  self.solrfieldspecs << solrfieldspec
  @benchmarks[solrfieldspec.solrField.to_s] = Benchmark::Tms.new(0,0,0,0, 0, solrfieldspec.solrField)      
end
Also aliased as: <<
buildSpecsFromDSLFile(f) click to toggle source

Build up a specset from the configuration in the given DSL file Note that this assumes that the maps have already been loaded!!

@param [String, IO] f The name of the file, or an open IO object @return [MARCSpec::SpecSet] the new object

# File lib/marcspec/specset.rb, line 84
def buildSpecsFromDSLFile f
  f = File.open(f) if f.is_a? String

  unless f
    log.error("Can't open file #{file}; unable to configure") 
    Process.abort("Can't open file #{file}; unable to configure")
  end
  self.instance_eval(f.read)
  self.check_and_fill_maps
end
buildSpecsFromList(speclist) click to toggle source

Build a specset from the result of eval'ing an old-style pp hash. @deprecated Use the DSL

# File lib/marcspec/specset.rb, line 114
def buildSpecsFromList speclist
  speclist.each do |spechash|
    if spechash[:module]
      solrspec = MARCSpec::CustomSolrSpec.fromHash(spechash)
    elsif spechash[:constantValue]
      solrspec = MARCSpec::ConstantSolrSpec.fromHash(spechash)
    else
      solrspec = MARCSpec::SolrFieldSpec.fromHash(spechash)
    end
    if spechash[:mapname]
      map = self.map(spechash[:mapname])
      unless map
        log.error "Cannot find map #{spechash[:mapname]} for field #{spechash[:solrField]}"
        Process.abort("Cannot find map #{spechash[:mapname]} for field #{spechash[:solrField]}")
      else
        log.debug "  Found map #{spechash[:mapname]} for field #{spechash[:solrField]}"
        solrspec.map = map
      end
    end
    self.add_spec solrspec
    log.debug "Added spec #{solrspec.solrField}"
  end
end
check_and_fill_maps() click to toggle source
# File lib/marcspec/specset.rb, line 95
def check_and_fill_maps
  @solrfieldspecs.each do |sfs|
    if sfs._mapname
      map = self.map(sfs._mapname)
      if map
        log.debug "  Found map #{map.mapname} for solr field #{sfs.solrField}"
        sfs.map = map
      else
        log.error "  Cannot find map #{sfs._mapname} for solr field #{sfs.solrField}"
        Process.abort("Cannot find map #{sfs._mapname} for solr field #{sfs.solrField}")
      end
    end
  end
end
constant(name, &blk) click to toggle source

Create a constant field

# File lib/marcspec/dsl.rb, line 24
def constant(name, &blk)
  log.debug "Creating constant field #{name}"
  
  constant = ConstantSolrSpec.new(:solrField=>name)
  constant.instance_eval(&blk)
  self << constant
  return constant
end
custom(name, &blk) click to toggle source
# File lib/marcspec/dsl.rb, line 33
def custom(name, &blk)
  log.debug "Creating custom field #{name}"
  custom = CustomSolrSpec.new(:solrField=>name)
  custom.instance_eval(&blk)
  
  ##### Check to make sure it's all ok in here#####
  self << custom
  return custom
end
doc_from_marc(r, timeit = false) click to toggle source

Get a new SolrInputDocument based on the record passed in. Statistics will optionally be kept, and can be accessed via the @benchmarks intance varible later on.

@param [MARC4J4R::Record] r The record @param [Boolean] timeit Whether to keep cumulative benchmarking statistics or not @return [SolrInputDocument] Thew new, filled SolrInputDocument

# File lib/marcspec/specset.rb, line 213
def doc_from_marc r, timeit = false
  doc = SolrInputDocument.new
  if timeit
    fill_hashlike_from_marc_benchmark r, doc
  else 
    fill_hashlike_from_marc r, doc
  end
  return doc
end
each() { |fs| ... } click to toggle source

Iterate over each of the solr field specs

# File lib/marcspec/specset.rb, line 152
def each
  @solrfieldspecs.each do |fs|
    yield fs
  end
end
field(name, &blk) click to toggle source

create a normal field

# File lib/marcspec/dsl.rb, line 15
def field(name, &blk)
  log.debug "Creating regular field #{name}"
  sfs = SolrFieldSpec.new(:solrField=>name)
  sfs.instance_eval(&blk)
  self << sfs
  return sfs
end
fill_hashlike_from_marc(r, hashlike) click to toggle source

Fill a hashlike (either a hash or a SolrInputDocument) based on the specs, maps, and passed-in record.

Result is the hashlike getting new data added to it. Nothing is returned; it's all side-effects.

@param [MARC4J4R::Record] r The record @param [Hash, SolrInputDocument] hashlike The hash-like object that contains previously-generated content

# File lib/marcspec/specset.rb, line 168
def fill_hashlike_from_marc r, hashlike
  @solrfieldspecs.each do |sfs|
    if sfs.arity == 1
      hashlike.add(sfs.solrField,sfs.marc_values(r, hashlike))
    else 
      vals = sfs.marc_values(r, hashlike)
      (0..(sfs.arity - 1)).each do |i|
        hashlike.add(sfs.solrField[i], vals[i])
      end
    end
  end
end
fill_hashlike_from_marc_benchmark(r, hashlike) click to toggle source

Same as fill_hashlike_from_marc, but keeps track of how long each solr field takes (cumulative; it's added to every time you get data from a record).

@param [MARC4J4R::Record] r The record @param [Hash, SolrInputDocument] hashlike The hash-like object that contains previously-generated content

# File lib/marcspec/specset.rb, line 189
def fill_hashlike_from_marc_benchmark r, hashlike
  @solrfieldspecs.each do |sfs|
    @benchmarks[sfs.solrField.to_s] += Benchmark.measure do
      if sfs.arity == 1
        hashlike.add(sfs.solrField,sfs.marc_values(r, hashlike))
      else 
        vals = sfs.marc_values(r, hashlike)
        (0..(sfs.arity - 1)).each do |i|
          hashlike.add(sfs.solrField[i], vals[i])
        end
      end
    end
  end
end
hash_from_marc(r, timeit = false) click to toggle source

Exactly the same as doc_from_marc, but the return object is a subclass of Hash

@param [MARC4J4R::Record] r The record @param [Boolean] timeit Whether to keep cumulative benchmarking statistics or not @return [MockSolrDoc] Thew new, filled Hash

# File lib/marcspec/specset.rb, line 230
def hash_from_marc r, timeit = false
  h = MARCSpec::MockSolrDoc.new
  if timeit
     fill_hashlike_from_marc_benchmark r, h
   else 
     fill_hashlike_from_marc r, h
   end
  return h
end
loadMapsFromDir(dir) click to toggle source

Get all the *.rb files in a directory, assume they're maps, and create entries for them in self

Simple wrapper around MARCSpec::Map#fromFile. Note that if a mapname is not found in the map structure, the name of the file (without the trailing '.rb') will be used.

@param [String] dir The directory to look in. Not recursive.

# File lib/marcspec/specset.rb, line 61
def loadMapsFromDir dir
  unless File.exist? dir
    raise ArgumentError, "Cannot load maps from #{dir}: does not exist"
  end
  Dir.glob("#{dir}/*.rb").each do |tmapfile|
    self.add_map(MARCSpec::Map.fromFile(tmapfile))
  end
end
map(name) click to toggle source

Get the map object associated with the given name @param [String] name The name of the map you want @return [MARCSpec::Map, nil] Either the map or nil (if not found)

# File lib/marcspec/specset.rb, line 49
def map name
  return self.tmaps[name]
end