Class: MARCSpec::CustomSolrSpec

Inherits:
SolrFieldSpec show all
Defined in:
lib/marcspec/dsl.rb,
lib/marcspec/customspec.rb

Instance Attribute Summary collapse

Attributes inherited from SolrFieldSpec

#_mapname, #arity, #defaultValue, #first, #map, #marcfieldspecs, #noMapKeyDefault, #solrField

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from SolrFieldSpec

#<<, #==, #default, #firstOnly, fromPPString, #mapMissDefault, #mapname, #marc_values, #pretty_print, #spec

Constructor Details

#initialize(opts) ⇒ CustomSolrSpec

Get a new Custom Solr Spec based on the passed in options. custom function we’ll be calling 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.

Parameters:

  • opts (Hash)

    Initialization options

Options Hash (opts):

  • :solrField (String, Array<String>)

    the name(s) of the Solr field(s) that will receive the data derived from this spec

  • :module (Module)

    the actual module constant (not a string or symbol representation) which holds the

  • :functionSymbol (Symbol)

    A symbol of the name of the custom function

  • :firstOnly (Boolean) — default: false

    Whether we should return the first found value

  • :default (String) — default: nil

    The value to return if the custom function returns no values

  • :map (MARC2Solr::Map) — default: nil

    An optional Map used to translate resulting values

  • :noMapKeyDefault (String) — default: nil

    The value to return if (a) a value is found, (b) a map is defined, but © there’s



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# 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

Instance Attribute Details

#functionArgsObject

Returns the value of attribute functionArgs.



35
36
37
# File 'lib/marcspec/customspec.rb', line 35

def functionArgs
  @functionArgs
end

#functionSymbolObject

Returns the value of attribute functionSymbol.



35
36
37
# File 'lib/marcspec/customspec.rb', line 35

def functionSymbol
  @functionSymbol
end

#moduleObject

Returns the value of attribute module.



35
36
37
# File 'lib/marcspec/customspec.rb', line 35

def module
  @module
end

Class Method Details

.fromHash(h) ⇒ Object

Deprecated.

Use the DSL

Produce one from the results of eval’ing a asPPString string



93
94
95
# File 'lib/marcspec/customspec.rb', line 93

def self.fromHash h
  return self.new(h)
end

Instance Method Details

#args(*arg_or_args) ⇒ Object



155
156
157
# File 'lib/marcspec/dsl.rb', line 155

def args(*arg_or_args)
  self.functionArgs = arg_or_args
end

#asDSLStringObject

Produce DSL code that will reproduce this object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# 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

#asPPStringObject

Deprecated.

Use the DSL

Print out as a ruby hash.



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# 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) ⇒ Object



135
136
137
138
# File 'lib/marcspec/dsl.rb', line 135

def function(name, &blk)
  self.functionSymbol = name.to_sym
  self.instance_eval(&blk)
end

#mod(constant) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/marcspec/dsl.rb', line 140

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) ⇒ Array<String>

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.

Parameters:

  • r (MARC4J4R::Record)

    A marc record

  • doc (SolrInputDocument, Hash)

    The document we’re constructing.

Returns:

  • (Array<String>)

    An array of values returned by the custom method



85
86
87
# File 'lib/marcspec/customspec.rb', line 85

def raw_marc_values r, doc
  return @module.send(@functionSymbol, doc, r, *@functionArgs)
end