class MARCSpec::CustomSolrSpec

A CustomSolrSpec is a SolrFieldSpec that derives all its values from a custom function. The custom function must me a module function that takes a hash-like document object, a MARC4J4R record, and an array of other arguments and returns a (possibly empty) list of resulting values.

See the example file simple_sample/index.rb in the marc2solr project for configuration examples.

@example A sample custom function, to be placed in the configuration directory's lib/ subdir

module MARC2Solr
 module MyCustomStuff
   def self.uppercaseTitle r, args=[]
     vals = []
     vals.push r['245'].value.upcase
     return vals
   end
 end

end

@example A simple custom spec made by hand css = MARCSpec::CustomSolrSpec.new(:module => MARC2Solr::MyCustomStuff,

 :functionSymbol => :uppercaseTitle,
 :map => ss.map('mapname')
)

Attributes

functionArgs[RW]
functionSymbol[RW]
module[RW]

Public Class Methods

fromHash(h) click to toggle source

Produce one from the results of eval'ing a asPPString string @deprecated Use the DSL

# File lib/marcspec/customspec.rb, line 93
def self.fromHash h
  return self.new(h)
end
new(opts) click to toggle source

Get a new Custom Solr Spec based on the passed in options. @param [Hash] opts Initialization options @option opts [String, Array<String>] :solrField the name(s) of the Solr field(s) that will receive the data derived from this spec @option opts [Module] :module the actual module constant (not a string or symbol representation) which holds the custom function we'll be calling @option opts [Symbol] :functionSymbol A symbol of the name of the custom function @option opts [Boolean] :firstOnly (false) Whether we should return the first found value @option opts [String] :default (nil) The value to return if the custom function returns no values @option opts [MARC2Solr::Map] :map (nil) An optional Map used to translate resulting values @option opts [String] :noMapKeyDefault (nil) The value to return if (a) a value is found, (b) a map is defined, but © there's no key in the map that matches the value.

Note that the last four options don't make sense if multiple :solrFields are given, and are illegal in that case.

Also note that using the DSL to produce these is easier from config file.

# File lib/marcspec/customspec.rb, line 53
def initialize(opts)
  @solrField  = opts[:solrField]
  @module = opts[:module] || nil
  @functionSymbol = opts[:functionSymbol] || nil

  @functionArgs = opts[:functionArgs] || []
  
  @first = opts[:firstOnly] || false      
  @defaultValue = opts[:default] || nil
  @map = opts[:map] || nil
  @noMapKeyDefault = opts[:noMapKeyDefault] || nil
  
  if @solrField.is_a? Array
    @arity = @solrField.size
    if @first or @defaultValue or @map or @noMapKeyDefault 
      raise ArgumentError, "Custom spec with multiple solrFields can't have :first, :map, :default, or :noMapKeyDefault set"
    end
  else
    @arity = 1
  end
  
  
end

Public Instance Methods

args(*arg_or_args) click to toggle source
# File lib/marcspec/dsl.rb, line 161
def args(*arg_or_args)
  self.functionArgs = arg_or_args
end
asDSLString() click to toggle source

Produce DSL code that will reproduce this object

# File lib/marcspec/customspec.rb, line 98
def asDSLString
  s = StringIO.new
  s.puts "custom('#{@solrField}') do"
  s.puts "  firstOnly" if @first
  if @defaultValue
    s.puts "  default " + 
    PP.singleline_pp(@defaultValue + "\n", s)
  end
  if @map
    s.print "  mapname "
    PP.pp(@map.mapname, s)
  end
  if @noMapKeyDefault
    s.print("  mapMissDefault ")
    PP.singleline_pp(@noMapKeyDefault, s)
    s.print("\n ")
  end

  s.puts("  function(:#{@functionSymbol}) {")
  s.puts("    mod  #{@module}")
  if  @functionArgs and @functionArgs.size > 0
    args = @functionArgs.map{|a| a.inspect}.join(', ')
    s.puts "    args #{args}"
  end
  s.puts "  }"
  s.puts "end"
  return s.string
end
asPPString() click to toggle source

Print out as a ruby hash. @deprecated Use the DSL

# File lib/marcspec/customspec.rb, line 129
def asPPString
  s = StringIO.new
  s.print "{\n :solrField=> "
  PP.singleline_pp(@solrField, s)
  s.print(",\n ")
  s.print ":firstOnly => true,\n " if @first
  if @defaultValue
    s.print(":default => ")
    PP.singleline_pp(@defaultValue, s)
    s.print(",\n ")
  end
  if @map
    s.print(":mapname => ")
    PP.singleline_pp(@map.mapname, s)
    s.print(",\n ")
  end
  if @noMapKeyDefault
    s.print(":noMapKeyDefault => ")
    PP.singleline_pp(@noMapKeyDefault, s)
    s.print(",\n ")
  end
  
  s.print(":module => ")
  PP.singleline_pp(@module, s)
  s.print(",\n :functionSymbol => ")
  PP.singleline_pp(@functionSymbol, s)
  if @functionArgs
    s.print(",\n :functionArgs => ")
    PP.singleline_pp(@functionArgs, s)
  end
  s.print "\n}"
  return  s.string
end
function(name, &blk) click to toggle source
# File lib/marcspec/dsl.rb, line 141
def function(name, &blk)
  self.functionSymbol = name.to_sym
  self.instance_eval(&blk)
end
mod(constant) click to toggle source
# File lib/marcspec/dsl.rb, line 146
def mod(constant)
  # Is the module loaded?
  
  unless defined? constant
    raise ArgumentError, "Module #{constant} unknown; do you need to make sure it gets loaded?"
  end
  
  # Check to see if the functionSymbol exists
  unless constant.singleton_methods.include? self.functionSymbol.to_s
    raise ArgumentError, "Function #{self.functionSymbol} not defined in module #{constant}"
  end
  
  self.module = constant
end
raw_marc_values(r, doc) click to toggle source

Get values from a MARC object and/or the prevously-filled document object.

Note that the doc is read-write here, but for the love of god, just leave it alone.

@param [MARC4J4R::Record] r A marc record @param [SolrInputDocument, Hash] doc The document we're constructing. @return [Array<String>] An array of values returned by the custom method

# File lib/marcspec/customspec.rb, line 85
def raw_marc_values r, doc
  return @module.send(@functionSymbol, doc, r, *@functionArgs)
end