Class: MARCSpec::CustomSolrSpec
- Inherits:
-
SolrFieldSpec
- Object
- SolrFieldSpec
- MARCSpec::CustomSolrSpec
- Defined in:
- lib/marcspec/dsl.rb,
lib/marcspec/customspec.rb
Instance Attribute Summary collapse
-
#functionArgs ⇒ Object
Returns the value of attribute functionArgs.
-
#functionSymbol ⇒ Object
Returns the value of attribute functionSymbol.
-
#module ⇒ Object
Returns the value of attribute module.
Attributes inherited from SolrFieldSpec
#_mapname, #arity, #defaultValue, #first, #map, #marcfieldspecs, #noMapKeyDefault, #solrField
Class Method Summary collapse
-
.fromHash(h) ⇒ Object
deprecated
Deprecated.
Use the DSL
Instance Method Summary collapse
- #args(*arg_or_args) ⇒ Object
-
#asDSLString ⇒ Object
Produce DSL code that will reproduce this object.
-
#asPPString ⇒ Object
deprecated
Deprecated.
Use the DSL
- #function(name, &blk) ⇒ Object
-
#initialize(opts) ⇒ CustomSolrSpec
constructor
Get a new Custom Solr Spec based on the passed in options.
- #mod(constant) ⇒ Object
-
#raw_marc_values(r, doc) ⇒ Array<String>
Get values from a MARC object and/or the prevously-filled document object.
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.
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
#functionArgs ⇒ Object
Returns the value of attribute functionArgs.
35 36 37 |
# File 'lib/marcspec/customspec.rb', line 35 def functionArgs @functionArgs end |
#functionSymbol ⇒ Object
Returns the value of attribute functionSymbol.
35 36 37 |
# File 'lib/marcspec/customspec.rb', line 35 def functionSymbol @functionSymbol end |
#module ⇒ Object
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
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
161 162 163 |
# File 'lib/marcspec/dsl.rb', line 161 def args(*arg_or_args) self.functionArgs = arg_or_args end |
#asDSLString ⇒ Object
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 |
#asPPString ⇒ Object
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
141 142 143 144 |
# File 'lib/marcspec/dsl.rb', line 141 def function(name, &blk) self.functionSymbol = name.to_sym self.instance_eval(&blk) end |
#mod(constant) ⇒ Object
146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# 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) ⇒ 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.
85 86 87 |
# File 'lib/marcspec/customspec.rb', line 85 def raw_marc_values r, doc return @module.send(@functionSymbol, doc, r, *@functionArgs) end |