Class: MARCSpec::ControlFieldSpec

Inherits:
Object
  • Object
show all
Includes:
JLogger::Simple
Defined in:
lib/marcspec/controlfieldspec.rb,
lib/marcspec/dsl.rb

Overview

A ControlFieldSpec takes a control tag (generally 001..009) and an optional zero-based range When called with marc_values(record), it returns either the complete value of all occurances of the field in question (in the order they appear in the record), or the zero-based substrings based on the passed range.

cfs = MARCSpec::ControlTagSpec.new('001')

cfs = MARCSpec::ControlTagSpec.new('001', 0..2)

Note that the use of the zero-based range in this manner conforms to the way MARC substrings are specified.

Examples:

Get the whole 001

Get the first three characters of the 008

Direct Known Subclasses

LeaderSpec

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tag, range = nil) ⇒ ControlFieldSpec

Returns a new instance of ControlFieldSpec.



26
27
28
29
30
31
32
33
# File 'lib/marcspec/controlfieldspec.rb', line 26

def initialize (tag, range=nil)
  unless MARC4J4R::ControlField.control_tag? tag
    raise ArgumentError, "Tag must be a control tag"
  end
  @tag = tag
  self.range = range
  @rangehistory = []
end

Instance Attribute Details

#rangeObject

Returns the value of attribute range.



24
25
26
# File 'lib/marcspec/controlfieldspec.rb', line 24

def range
  @range
end

#rangehistoryObject

Returns the value of attribute rangehistory.



24
25
26
# File 'lib/marcspec/controlfieldspec.rb', line 24

def rangehistory
  @rangehistory
end

#tagObject

Returns the value of attribute tag.



24
25
26
# File 'lib/marcspec/controlfieldspec.rb', line 24

def tag
  @tag
end

Class Method Details

.fromPPString(str) ⇒ Object

Deprecated.

Use the DSL

Recreate from an asPPString call



116
117
118
119
# File 'lib/marcspec/controlfieldspec.rb', line 116

def self.fromPPString str
  a = eval(str)
  return self.new(*a)
end

Instance Method Details

#==(other) ⇒ Object



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

def == other
  return ((self.tag == other.tag) and
          (self.range == other.range))
end

#asDSLStringObject

Print out as a DSL segment



92
93
94
95
96
97
98
# File 'lib/marcspec/controlfieldspec.rb', line 92

def asDSLString
  if (@range)
    return "spec('#{@tag}') {chars #{@range}}"
  else
    return "spec('#{@tag}')"
  end
end

#asPPStringObject

Deprecated.

Use the DSL

Print out as a ruby hash.



103
104
105
106
107
108
109
110
111
# File 'lib/marcspec/controlfieldspec.rb', line 103

def asPPString
  s = StringIO.new
  if @range
    PP.pp([@tag, @range], s)
  else
    PP.pp([@tag], s)
  end
  return s.string
end

#char(c) ⇒ Object Also known as: chars



116
117
118
119
# File 'lib/marcspec/dsl.rb', line 116

def char c
  self.range = c
  return self
end

#marc_values(r) ⇒ Object



75
76
77
78
79
80
81
82
# File 'lib/marcspec/controlfieldspec.rb', line 75

def marc_values r
  vals = r.find_by_tag(@tag).map {|f| f.value}
  if @range
    return vals.map {|v| v[@range]}
  else
    return vals
  end
end

#pretty_print(pp) ⇒ Object

Deprecated.

Use the DSL

Print it out has a ruby hash



87
88
89
# File 'lib/marcspec/controlfieldspec.rb', line 87

def pretty_print pp
  pp.pp eval(self.asPPString)
end