Class: BerkeleyLibrary::AV::Metadata::Field

Inherits:
Object
  • Object
show all
Includes:
BerkeleyLibrary::AV::Marc::Util, Comparable
Defined in:
lib/berkeley_library/av/metadata/field.rb

Constant Summary collapse

SPEC_TAG_RE =

Constants

/^([0-9a-z.]{3})/

Constants included from Util

Util::DEFAULT_USER_AGENT

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from BerkeleyLibrary::AV::Marc::Util

#group_subfield_values, #group_subfields

Methods included from Util

#class_name, #compare_by_attributes, #compare_values, #do_get, #tidy_value

Constructor Details

#initialize(order:, label:, spec:, tag: nil, subfields_separator: ' ', subfield_order: []) ⇒ Field

rubocop:disable Metrics/ParameterLists



27
28
29
30
31
32
33
34
35
# File 'lib/berkeley_library/av/metadata/field.rb', line 27

def initialize(order:, label:, spec:, tag: nil, subfields_separator: ' ', subfield_order: [])
  @order = order
  @label = label
  @spec = spec
  @query = MARC::Spec.parse_query(spec)
  @tag = tag || query.tag_str
  @subfields_separator = subfields_separator
  @subfield_order = subfield_order
end

Instance Attribute Details

#labelObject (readonly)


Accessors



21
22
23
# File 'lib/berkeley_library/av/metadata/field.rb', line 21

def label
  @label
end

#orderObject (readonly)


Accessors



21
22
23
# File 'lib/berkeley_library/av/metadata/field.rb', line 21

def order
  @order
end

#queryObject (readonly)


Accessors



21
22
23
# File 'lib/berkeley_library/av/metadata/field.rb', line 21

def query
  @query
end

#specObject (readonly)


Accessors



21
22
23
# File 'lib/berkeley_library/av/metadata/field.rb', line 21

def spec
  @spec
end

#subfield_orderObject (readonly)


Accessors



21
22
23
# File 'lib/berkeley_library/av/metadata/field.rb', line 21

def subfield_order
  @subfield_order
end

#subfields_separatorObject (readonly)


Accessors



21
22
23
# File 'lib/berkeley_library/av/metadata/field.rb', line 21

def subfields_separator
  @subfields_separator
end

#tagObject (readonly)


Accessors



21
22
23
# File 'lib/berkeley_library/av/metadata/field.rb', line 21

def tag
  @tag
end

Instance Method Details

#<=>(other) ⇒ Object


Comparable



76
77
78
# File 'lib/berkeley_library/av/metadata/field.rb', line 76

def <=>(other)
  compare_by_attributes(self, other, *sort_attrs)
end

#hashObject



69
70
71
# File 'lib/berkeley_library/av/metadata/field.rb', line 69

def hash
  sort_attrs.map { |a| send(a) }.hash
end

#inspectObject



65
66
67
# File 'lib/berkeley_library/av/metadata/field.rb', line 65

def inspect
  "#<#{class_name(self)} #{self}>"
end

#same_metadata?(other) ⇒ Boolean

Returns:

  • (Boolean)

Raises:

  • (ArgumentError)


47
48
49
50
51
52
53
# File 'lib/berkeley_library/av/metadata/field.rb', line 47

def same_metadata?(other)
  raise ArgumentError, "Not a #{class_name(self)}: #{other}" unless other.is_a?(Field)

  %i[tag query subfields_separator subfield_order].all? do |attr|
    (other.respond_to?(attr) && send(attr) == other.send(attr))
  end
end

#to_sObject


Object



58
59
60
61
62
63
# File 'lib/berkeley_library/av/metadata/field.rb', line 58

def to_s
  "#{order}. #{query} #{label.inspect}".tap do |str|
    str << " #{subfields_separator.inspect}" unless subfields_separator == ' '
    str << " $#{subfield_order.join('$')}" unless subfield_order.empty?
  end
end

#value_from(marc_record) ⇒ Object


Public methods



41
42
43
44
45
# File 'lib/berkeley_library/av/metadata/field.rb', line 41

def value_from(marc_record)
  results = MARC::Spec.execute_query(query, marc_record)
  subfield_groups = subfield_groups_from_result(results)
  Value.value_for(self, subfield_groups)
end