Class: MARCSpec::SolrFieldSpec
- Inherits:
-
Object
- Object
- MARCSpec::SolrFieldSpec
- Includes:
- JLogger::Simple
- Defined in:
- lib/marcspec/solrfieldspec.rb,
lib/marcspec/dsl.rb
Overview
The basic Solr Field spec -- a specification object that knows how to extract data from a MARC record.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#_mapname ⇒ Object
Returns the value of attribute _mapname.
-
#arity ⇒ Object
readonly
Returns the value of attribute arity.
-
#defaultValue ⇒ Object
Returns the value of attribute defaultValue.
-
#first ⇒ Object
Returns the value of attribute first.
-
#map ⇒ Object
Returns the value of attribute map.
-
#marcfieldspecs ⇒ Object
Returns the value of attribute marcfieldspecs.
-
#noMapKeyDefault ⇒ Object
Returns the value of attribute noMapKeyDefault.
-
#solrField ⇒ Object
Returns the value of attribute solrField.
Class Method Summary collapse
-
.fromHash(h) ⇒ Object
deprecated
Deprecated.
Use the DSL
-
.fromPPString(str) ⇒ Object
deprecated
Deprecated.
Use the DSL
Instance Method Summary collapse
-
#<<(tagspec) ⇒ Object
Add a new tag specification.
-
#==(other) ⇒ Boolean
Basic equality.
-
#asDSLString ⇒ String
Create a string representation suitable for inclusion in a DSL file.
-
#asPPString ⇒ Object
deprecated
Deprecated.
Use the DSL
- #default(val) ⇒ Object
- #firstOnly(val = true) ⇒ Object
-
#initialize(opts) ⇒ SolrFieldSpec
constructor
Get a new object.
- #mapMissDefault(str) ⇒ Object
- #mapname(str) ⇒ Object
-
#marc_values(r, doc = {}) ⇒ Array
Get the values from the MARC, provide a default or mapping as necessary.
-
#pretty_print(pp) ⇒ Object
deprecated
Deprecated.
Use the DSL
-
#raw_marc_values(r, doc) ⇒ Array
Get raw (not translated by a map or anything) values from the MARC.
- #spec(tag, &blk) ⇒ Object
Constructor Details
#initialize(opts) ⇒ SolrFieldSpec
Get a new object
19 20 21 22 23 24 25 26 27 |
# File 'lib/marcspec/solrfieldspec.rb', line 19 def initialize(opts) @solrField = opts[:solrField] @first = opts[:firstOnly] || false @defaultValue = opts[:default] || nil @map = opts[:map] || nil @noMapKeyDefault = opts[:noMapKeyDefault] || nil @arity = 1 @marcfieldspecs = [] end |
Instance Attribute Details
#_mapname ⇒ Object
Returns the value of attribute _mapname.
14 15 16 |
# File 'lib/marcspec/solrfieldspec.rb', line 14 def _mapname @_mapname end |
#arity ⇒ Object (readonly)
Returns the value of attribute arity.
15 16 17 |
# File 'lib/marcspec/solrfieldspec.rb', line 15 def arity @arity end |
#defaultValue ⇒ Object
Returns the value of attribute defaultValue.
14 15 16 |
# File 'lib/marcspec/solrfieldspec.rb', line 14 def defaultValue @defaultValue end |
#first ⇒ Object
Returns the value of attribute first.
14 15 16 |
# File 'lib/marcspec/solrfieldspec.rb', line 14 def first @first end |
#map ⇒ Object
Returns the value of attribute map.
14 15 16 |
# File 'lib/marcspec/solrfieldspec.rb', line 14 def map @map end |
#marcfieldspecs ⇒ Object
Returns the value of attribute marcfieldspecs.
14 15 16 |
# File 'lib/marcspec/solrfieldspec.rb', line 14 def marcfieldspecs @marcfieldspecs end |
#noMapKeyDefault ⇒ Object
Returns the value of attribute noMapKeyDefault.
14 15 16 |
# File 'lib/marcspec/solrfieldspec.rb', line 14 def noMapKeyDefault @noMapKeyDefault end |
#solrField ⇒ Object
Returns the value of attribute solrField.
14 15 16 |
# File 'lib/marcspec/solrfieldspec.rb', line 14 def solrField @solrField end |
Class Method Details
.fromHash(h) ⇒ Object
Use the DSL
Build an object from an eval'd asPPString string
113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/marcspec/solrfieldspec.rb', line 113 def self.fromHash h sfs = self.new(h) h[:specs].each do |s| if MARC4J4R::ControlField.control_tag? s[0] sfs << MARCSpec::ControlFieldSpec.new(*s) else sfs << MARCSpec::VariableFieldSpec.new(s[0], s[1], s[2]) end end return sfs end |
.fromPPString(str) ⇒ Object
Use the DSL
Build an object from a asPPString string
106 107 108 |
# File 'lib/marcspec/solrfieldspec.rb', line 106 def self.fromPPString str return self.fromHash eval(str) end |
Instance Method Details
#<<(tagspec) ⇒ Object
Add a new tag specification
32 33 34 |
# File 'lib/marcspec/solrfieldspec.rb', line 32 def << @marcfieldspecs << end |
#==(other) ⇒ Boolean
Basic equality
95 96 97 98 99 100 101 102 |
# File 'lib/marcspec/solrfieldspec.rb', line 95 def == other return ((other.solrField == self.solrField) and (other.first == self.first) and (other.map == self.map) and (other.defaultValue == self.defaultValue) and (other.noMapKeyDefault == self.noMapKeyDefault) and (other.marcfieldspecs == self.marcfieldspecs)) end |
#asDSLString ⇒ String
Create a string representation suitable for inclusion in a DSL file
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/marcspec/solrfieldspec.rb', line 134 def asDSLString s = StringIO.new s.puts "field('#{@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 @marcfieldspecs.each do |spec| s.puts " " + spec.asDSLString end s.puts "end" return s.string end |
#asPPString ⇒ Object
Use the DSL
Output as a string representation of a ruby hash
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/marcspec/solrfieldspec.rb', line 162 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(":specs => [\n") @marcfieldspecs.each do |ts| s.print ' ' PP.singleline_pp(ts, s) s.print(",\n") end s.print " ]\n}" return s.string end |
#default(val) ⇒ Object
101 102 103 |
# File 'lib/marcspec/dsl.rb', line 101 def default val @defaultValue = val end |
#firstOnly(val = true) ⇒ Object
97 98 99 |
# File 'lib/marcspec/dsl.rb', line 97 def firstOnly val=true @first = val end |
#mapMissDefault(str) ⇒ Object
109 110 111 |
# File 'lib/marcspec/dsl.rb', line 109 def mapMissDefault str @noMapKeyDefault = str end |
#mapname(str) ⇒ Object
105 106 107 |
# File 'lib/marcspec/dsl.rb', line 105 def mapname str @_mapname = str end |
#marc_values(r, doc = {}) ⇒ Array
Get the values from the MARC, provide a default or mapping as necessary
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/marcspec/solrfieldspec.rb', line 56 def marc_values r, doc = {} vals = raw_marc_values r, doc return vals if @arity > 1 unless vals.is_a? Array vals = [vals] end if @first vals = [vals.compact.first].compact end # If we got nothing, just return either nothing or the defualt, # if there is one. Don't screw around with mapping. if vals.size == 0 if @defaultValue.nil? # unless there's a default value, just return nothing return [] else return [@defaultValue] end end # If we've got a map, map it. if (@map) vals.map! {|v| @map[v, @noMapKeyDefault]} end # Flatten it all out vals.flatten! vals.uniq! vals.compact! return vals end |
#pretty_print(pp) ⇒ Object
Use the DSL
Output as a ruby hash
128 129 130 |
# File 'lib/marcspec/solrfieldspec.rb', line 128 def pretty_print pp pp.pp eval(self.asPPString) end |
#raw_marc_values(r, doc) ⇒ Array
Get raw (not translated by a map or anything) values from the MARC
42 43 44 45 46 47 48 |
# File 'lib/marcspec/solrfieldspec.rb', line 42 def raw_marc_values r, doc vals = [] @marcfieldspecs.each do |ts| vals.concat ts.marc_values(r) end return vals end |
#spec(tag, &blk) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/marcspec/dsl.rb', line 47 def spec(tag, &blk) subs = nil if tag =~ /^(...)(.+)$/ tag = $1 subs = $2 end if tag.to_i == tag tag = '%03d' % tag end marcfieldspec = nil if tag == 'LDR' marcfieldspec = MARCSpec::LeaderSpec.new('LDR') elsif MARC4J4R::ControlField.control_tag? tag marcfieldspec = MARCSpec::ControlFieldSpec.new(tag) else marcfieldspec = MARCSpec::VariableFieldSpec.new(tag) end # Did we get subs? If so, add them now. if subs marcfieldspec.codes = subs end marcfieldspec.instance_eval(&blk) if block_given? # If we had multiple sub calls, get them from the codehistory if marcfieldspec.is_a? MARCSpec::VariableFieldSpec marcfieldspec.codehistory.uniq.compact.each do |c| newmfs = marcfieldspec.clone newmfs.codes = c self << newmfs end end if marcfieldspec.is_a? MARCSpec::ControlFieldSpec marcfieldspec.rangehistory.uniq.compact.each do |r| newcfs = marcfieldspec.clone newcfs.range = r self << newcfs end end self << marcfieldspec return marcfieldspec end |