rsinger-enhanced_marc 0.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,38 @@
1
+ module MARC
2
+
3
+ # A class that represents an individual MARC record. Every record
4
+ # is made up of a collection of MARC::Field objects.
5
+
6
+ class ScoreRecord < Record
7
+
8
+ def initialize
9
+ super
10
+ @leader[6] = 'c' if @leader[6,1] == ' '
11
+ @leader[7] = 'm' if @leader[7,1] == ' '
12
+ @record_type = 'SCO'
13
+ @bibliographic_level = @leader.get_blvl
14
+ self.extend ScoreType
15
+ self.inspect_fixed_fields
16
+ end
17
+
18
+ def is_valid_type?
19
+ return false unless @leader[6,1].match(/[cd]{1}/)
20
+ return true
21
+ end
22
+ def is_valid_blvl?
23
+ return true if @leader[7,1].match(/[acdim]{1}/) and @leader[6,1].match('d')
24
+ return true if @leader[7,1].match(/[abcdims]{1}/) and @leader[6,1].match('c')
25
+ return false
26
+ end
27
+ def self.new_from_record(record)
28
+ rec = ScoreRecord.new
29
+ record.instance_variables.each { | var |
30
+ rec.instance_variable_set(var, record.instance_variable_get(var))
31
+ }
32
+ return Exception.new("Incorrect type declaration in leader") unless rec.is_valid_type?
33
+ return Exception.new("Incorrect bibliographic declaration in leader") unless rec.is_valid_blvl?
34
+ return rec
35
+ end
36
+
37
+ end
38
+ end
@@ -0,0 +1,4 @@
1
+ module ScoreType
2
+ include RecordType
3
+ public :accompanying_matter, :audience_level, :form, :composition_form, :music_format, :is_conference?
4
+ end
@@ -0,0 +1,37 @@
1
+ module MARC
2
+
3
+ # A class that represents an individual MARC record. Every record
4
+ # is made up of a collection of MARC::Field objects.
5
+
6
+ class SerialRecord < Record
7
+
8
+ def initialize
9
+ super
10
+ @leader[6] = 'a' if @leader[6,1] == ' '
11
+ @leader[7] = 's' if @leader[7,1] == ' '
12
+ @record_type = 'SER'
13
+ @bibliographic_level = @leader.get_blvl
14
+ self.extend SerialType
15
+ self.inspect_fixed_fields
16
+ end
17
+
18
+ def is_valid_type?
19
+ return false unless @leader[6,1].match('a')
20
+ return true
21
+ end
22
+ def is_valid_blvl?
23
+ return false unless @leader[7,1].match(/[bis]{1}/)
24
+ return true
25
+ end
26
+ def self.new_from_record(record)
27
+ rec = SerialRecord.new
28
+ record.instance_variables.each { | var |
29
+ rec.instance_variable_set(var, record.instance_variable_get(var))
30
+ }
31
+ return Exception.new("Incorrect type declaration in leader") unless rec.is_valid_type?
32
+ return Exception.new("Incorrect bibliographic declaration in leader") unless rec.is_valid_blvl?
33
+ return rec
34
+ end
35
+
36
+ end
37
+ end
@@ -0,0 +1,87 @@
1
+ module SerialType
2
+ include RecordType
3
+ public :is_govdoc?, :nature_of_contents, :is_conference?, :form
4
+ def alphabet(human_readable=false)
5
+ alph_map = {'a'=>'Roman', 'b'=>'Extended Roman', 'c'=>'Cyrillic',
6
+ 'd'=>'Japanese', 'e'=>'Chinese', 'f'=>'Arabic', 'g'=>'Greek',
7
+ 'h'=>'Hebrew', 'i'=>'Thai', 'j'=>'Devanagari', 'k'=>'Korean', 'l'=>'Tamil',
8
+ 'u'=>'Unknown', 'z'=>'Other'
9
+ }
10
+ human_readable = alph_map if human_readable
11
+ return self.field_parser({:match=>'SER', :start=>33,:end=>1}, {:match=>'s', :start=>16,:end=>1}, human_readable)
12
+ end
13
+
14
+ def frequency(human_readable=false)
15
+ freq_map = {'a'=>'Annual','b'=>'Bimonthly','c'=>'Semiweekly','d'=>'Daily',
16
+ 'e'=>'Biweekly','f'=>'Semiannual','g'=>'Biennial','h'=>'Triennial','i'=>'3 times/week',
17
+ 'j'=>'3 times/month', 'k'=>'Continuously updated','m'=>'Monthly','q'=>'Quarterly',
18
+ 's'=>'Semimonthly','t'=>'3 times/year','u'=>'Unknown','w'=>'Weekly','z'=>'Other'
19
+ }
20
+ human_readable = freq_map if human_readable
21
+ resp = self.field_parser({:match=>'SER', :start=>18,:end=>1}, {:match=>'s', :start=>1,:end=>1}, human_readable)
22
+ return resp if resp
23
+ if human_readable
24
+ return 'No determinable frequency'
25
+ else
26
+ return false
27
+ end
28
+ end
29
+
30
+ def regularity(human_readable=false)
31
+ regl_map = {'n'=>'Normalized irregular','r'=>'Regular','u'=>'Unknown','x'=>'Completely irregular'}
32
+ human_readable = regl_map if human_readable
33
+ return self.field_parser({:match=>'SER', :start=>19,:end=>1}, {:match=>'s', :start=>2,:end=>1}, human_readable)
34
+ end
35
+
36
+ def serial_type(human_readable=false)
37
+ srtp_map = {'d'=>'Database','l'=>'Loose-leaf','m'=>'Monographic series','n'=>'Newspaper','p'=>'Periodical','w'=>'Website'}
38
+ human_readable = srtp_map if human_readable
39
+ resp = self.field_parser({:match=>'SER', :start=>21,:end=>1}, {:match=>'s', :start=>4,:end=>1}, human_readable)
40
+ return resp if resp
41
+ if human_readable
42
+ return 'Other'
43
+ else
44
+ return false
45
+ end
46
+ end
47
+
48
+ def original_form(human_readable=false)
49
+ orig_map = {'a'=>'Microfilm','b'=>'Microfiche','c'=>'Microopaque','d'=>'Large print',
50
+ 'e'=>'Newspaper format','f'=>'Braille','s'=>'Electronic'}
51
+ human_readable = orig_map if human_readable
52
+ resp = self.field_parser({:match=>'SER', :start=>22,:end=>1}, {:match=>'s', :start=>5,:end=>1}, human_readable)
53
+ return resp if resp
54
+
55
+ if human_readable
56
+ return 'Other'
57
+ else
58
+ return false
59
+ end
60
+
61
+ end
62
+
63
+ def nature_of_work(human_readable=false)
64
+ entw_map = {'a'=>'Abstracts','b'=>'Bibliography','c'=>'Catalog','d'=>'Dictionary',
65
+ 'e'=>'Encyclopedia', 'f'=>'Handbook', 'g'=>'Legal article', 'h'=>'Biography', 'i'=>'Index',
66
+ 'j'=>'Patent document', 'k'=>'Discography', 'l'=>'Legislation', 'm'=>'Thesis', 'n'=>'Literature survey',
67
+ 'o'=>'Review', 'p'=>'Programmed text', 'q'=>'Filmography', 'r'=>'Directory', 's'=>'Statistics',
68
+ 't'=>'Technical report', 'u'=>'Standard/specification', 'v'=>'Legal case', 'w'=>'Law report', 'x'=>'Other report',
69
+ 'z'=>'Treaty'}
70
+
71
+ human_readable = entw_map if human_readable
72
+ resp = self.field_parser({:match=>'SER', :start=>24,:end=>1}, {:match=>'s', :start=>7,:end=>1}, human_readable)
73
+ return resp if resp
74
+ if human_readable
75
+ return 'Not specified'
76
+ else
77
+ return false
78
+ end
79
+
80
+ end
81
+
82
+ def entry(human_readable=false)
83
+ entry_map = {'0'=>'Successive','1'=>'Latest','2'=>'Integrated'}
84
+ human_readable = entry_map if human_readable
85
+ return self.field_parser({:match=>'SER', :start=>34,:end=>1}, {:match=>'s', :start=>17,:end=>1}, human_readable)
86
+ end
87
+ end
@@ -0,0 +1,37 @@
1
+ module MARC
2
+
3
+ # A class that represents an individual MARC record. Every record
4
+ # is made up of a collection of MARC::Field objects.
5
+
6
+ class SoundRecord < Record
7
+
8
+ def initialize
9
+ super
10
+ @leader[6] = 'i' if @leader[6,1] == ' '
11
+ @leader[7] = 'm' if @leader[7,1] == ' '
12
+ @record_type = 'REC'
13
+ @bibliographic_level = @leader.get_blvl
14
+ self.extend SoundType
15
+ self.inspect_fixed_fields
16
+ end
17
+
18
+ def is_valid_type?
19
+ return false unless @leader[6,1].match(/[ij]{1}/)
20
+ return true
21
+ end
22
+ def is_valid_blvl?
23
+ return false unless @leader[7,1].match(/[abcdims]{1}/)
24
+ return true
25
+ end
26
+ def self.new_from_record(record)
27
+ rec = SoundRecord.new
28
+ record.instance_variables.each { | var |
29
+ rec.instance_variable_set(var, record.instance_variable_get(var))
30
+ }
31
+ return Exception.new("Incorrect type declaration in leader") unless rec.is_valid_type?
32
+ return Exception.new("Incorrect bibliographic declaration in leader") unless rec.is_valid_blvl?
33
+ return rec
34
+ end
35
+
36
+ end
37
+ end
@@ -0,0 +1,4 @@
1
+ module SoundType
2
+ include RecordType
3
+ public :accompanying_matter, :audience_level, :form, :composition_form, :music_format, :is_conference?
4
+ end
@@ -0,0 +1,37 @@
1
+ module MARC
2
+
3
+ # A class that represents an individual MARC record. Every record
4
+ # is made up of a collection of MARC::Field objects.
5
+
6
+ class VisualRecord < Record
7
+
8
+ def initialize
9
+ super
10
+ @leader[6] = 'g' if @leader[6,1] == ' '
11
+ @leader[7] = 'm' if @leader[7,1] == ' '
12
+ @record_type = 'VIS'
13
+ @bibliographic_level = @leader.get_blvl
14
+ self.extend VisualType
15
+ self.inspect_fixed_fields
16
+ end
17
+
18
+ def is_valid_type?
19
+ return false unless @leader[6,1].match(/[gkro]{1}/)
20
+ return true
21
+ end
22
+ def is_valid_blvl?
23
+ return false unless @leader[7,1].match(/[abcdims]{1}/)
24
+ return true
25
+ end
26
+ def self.new_from_record(record)
27
+ rec = VisualRecord.new
28
+ record.instance_variables.each { | var |
29
+ rec.instance_variable_set(var, record.instance_variable_get(var))
30
+ }
31
+ return Exception.new("Incorrect type declaration in leader") unless rec.is_valid_type?
32
+ return Exception.new("Incorrect bibliographic declaration in leader") unless rec.is_valid_blvl?
33
+ return rec
34
+ end
35
+
36
+ end
37
+ end
@@ -0,0 +1,38 @@
1
+ module VisualType
2
+ include RecordType
3
+ public :form, :audience_level, :is_govdoc?
4
+
5
+ def running_time
6
+ if @record_type == 'VIS'
7
+ unless self['008'].value[18,3] == 'nnn'
8
+ return "Unknown" if self['008'].value[18,3] == "---"
9
+ return self['008'].value[18,3].sub(/^0*/, '')
10
+ end
11
+ end
12
+ @fields.find_all {|f| ('006') === f.tag}.each { | fxd_fld |
13
+ next unless fxd_fld.value[0,1].match(/[gkor]{1}/)
14
+ unless fxd_fld.value[1,3] == 'nnn'
15
+ return "Unknown" if fxd_fld.value[1,3] == "---"
16
+ return fxd_fld.value[1,3].sub(/^0*/, '')
17
+ end
18
+ }
19
+ return false
20
+ end
21
+
22
+ def material_type(human_readable=false)
23
+ tmat_map = {'a'=>'Art original', 'b'=>'Kit','c'=>'Art reproduction','d'=>'Diorama',
24
+ 'f'=>'Filmstrip','g'=>'Game','i'=>'Picture','k'=>'Graphic','l'=>'Technical drawing',
25
+ 'm'=>'Motion picture','n'=>'Chart','o'=>'Flash card','p'=>'Microscope slide',
26
+ 'q'=>'Model','r'=>'Realia','s'=>'Slide','t'=>'Transparency','v'=>'Videorecording',
27
+ 'w'=>'Toy','z'=>'Other'}
28
+ human_readable = tmat_map if human_readable
29
+ return self.field_parser({:match=>'VIS', :start=>33,:end=>1}, {:match=>/[gkor]{1}/, :start=>16,:end=>1}, human_readable)
30
+ end
31
+
32
+ def technique(human_readable=false)
33
+ tech_map = {'a'=>'Animation','c'=>'Animation and live action','l'=>'Live action',
34
+ 'n'=>'N/A','u'=>'Unknown','z'=>'Other'}
35
+ human_readable = tmat_map if human_readable
36
+ return self.field_parser({:match=>'VIS', :start=>34,:end=>1}, {:match=>/[gkor]{1}/, :start=>17,:end=>1}, human_readable)
37
+ end
38
+ end
@@ -0,0 +1,58 @@
1
+ module MARC
2
+ class XMLReader
3
+ # will accept parse events until a record has been built up
4
+ #
5
+ def build_record
6
+ record = MARC::Record.new
7
+ data_field = nil
8
+ control_field = nil
9
+ subfield = nil
10
+ text = ''
11
+ attrs = nil
12
+
13
+ while @parser.has_next?
14
+ event = @parser.pull
15
+
16
+ if event.text?
17
+ text += REXML::Text::unnormalize(event[0])
18
+ next
19
+ end
20
+
21
+ if event.start_element?
22
+ text = ''
23
+ attrs = event[1]
24
+ case strip_ns(event[0])
25
+ when 'controlfield'
26
+ text = ''
27
+ control_field = MARC::ControlField.new(attrs['tag'])
28
+ when 'datafield'
29
+ text = ''
30
+ data_field = MARC::DataField.new(attrs['tag'], attrs['ind1'],
31
+ attrs['ind2'])
32
+ when 'subfield'
33
+ text = ''
34
+ subfield = MARC::Subfield.new(attrs['code'])
35
+ end
36
+ end
37
+
38
+ if event.end_element?
39
+ case strip_ns(event[0])
40
+ when 'leader'
41
+ record.leader = text
42
+ when 'record'
43
+ return record
44
+ when 'controlfield'
45
+ control_field.value = text
46
+ record.append(control_field)
47
+ when 'datafield'
48
+ record.append(data_field)
49
+ when 'subfield'
50
+ subfield.value = text
51
+ data_field.append(subfield)
52
+ end
53
+ end
54
+ end
55
+ record.to_typed_record
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,22 @@
1
+ require 'marc'
2
+ require 'enhanced_marc/reader'
3
+ require 'enhanced_marc/record'
4
+ require 'enhanced_marc/xmlreader'
5
+ require 'enhanced_marc/leader'
6
+ require 'enhanced_marc/book_record'
7
+ require 'enhanced_marc/serial_record'
8
+ require 'enhanced_marc/visual_record'
9
+ require 'enhanced_marc/mixed_record'
10
+ require 'enhanced_marc/map_record'
11
+ require 'enhanced_marc/score_record'
12
+ require 'enhanced_marc/sound_record'
13
+ require 'enhanced_marc/computer_record'
14
+ require 'enhanced_marc/record_type'
15
+ require 'enhanced_marc/book_type'
16
+ require 'enhanced_marc/computer_type'
17
+ require 'enhanced_marc/map_type'
18
+ require 'enhanced_marc/mixed_type'
19
+ require 'enhanced_marc/score_type'
20
+ require 'enhanced_marc/serial_type'
21
+ require 'enhanced_marc/sound_type'
22
+ require 'enhanced_marc/visual_type'
@@ -0,0 +1 @@
1
+ 2009-08-07: Refactored to monkeypatch ruby-marc rather than replace it.
@@ -0,0 +1,21 @@
1
+ The MIT License
2
+
3
+ Copyright (c) 2007 Ross Singer
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,51 @@
1
+ Enhanced MARC is a set of classes, modules and methods that sit on top of ruby-marc (http://rubyforge.org/projects/marc) to help parse the contents of MARC records more easily and conveniently.
2
+ Installation:
3
+ gem sources -a http://gems.github.com
4
+ sudo gem install rsinger-enhanced_marc
5
+
6
+ Usage:
7
+
8
+ require 'enhanced_marc'
9
+
10
+ reader = MARC::Reader.new('marc.dat')
11
+
12
+ records = []
13
+
14
+ reader.each do | record |
15
+ records << record
16
+ end
17
+
18
+ >> records[0].class
19
+ => MARC::BookRecord
20
+
21
+ >> records[0].is_conference?
22
+ => false
23
+
24
+ >> records[0].is_manuscript?
25
+ => false
26
+
27
+ # Send a boolean true if you want human readable forms, rather than MARC codes.
28
+ >> records[0].literary_form(true)
29
+ => "Non-fiction"
30
+
31
+ >> records[0].nature_of_contents(true)
32
+ => ["Bibliography", "Catalog"]
33
+
34
+ >> records[1].class
35
+ => MARC::SoundRecord
36
+
37
+ >> records[1].composition_form(true)
38
+ => "Jazz"
39
+
40
+ >> records[2].class
41
+ => MARC::MapRecord
42
+
43
+ >> records[2].projection(true)
44
+ => ["Cylindrical", "Mercator"]
45
+
46
+ >> records[2].relief(true)
47
+ => ["Color"]
48
+
49
+
50
+
51
+