Class: MARCSpec::ControlFieldSpec

Inherits:
Object
  • Object
show all
Includes:
Logback::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.



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

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.



22
23
24
# File 'lib/marcspec/controlfieldspec.rb', line 22

def range
  @range
end

#rangehistoryObject

Returns the value of attribute rangehistory.



22
23
24
# File 'lib/marcspec/controlfieldspec.rb', line 22

def rangehistory
  @rangehistory
end

#tagObject

Returns the value of attribute tag.



22
23
24
# File 'lib/marcspec/controlfieldspec.rb', line 22

def tag
  @tag
end

Class Method Details

.fromPPString(str) ⇒ Object

Deprecated.

Use the DSL

Recreate from an asPPString call



114
115
116
117
# File 'lib/marcspec/controlfieldspec.rb', line 114

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

Instance Method Details

#==(other) ⇒ Object



33
34
35
36
# File 'lib/marcspec/controlfieldspec.rb', line 33

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

#asDSLStringObject

Print out as a DSL segment



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

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.



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

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



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

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



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

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