class MARC::Spec::Queries::Tag

Constants

LDR

Constants

Attributes

index[R]

Attributes

tag_exact[R]

Attributes

tag_re[R]

Attributes

Public Class Methods

new(tag, index = nil) click to toggle source

Initializer

# File lib/marc/spec/queries/tag.rb, line 23
def initialize(tag, index = nil)
  raise ArgumentError, 'Tag cannot be nil' unless tag

  @tag_exact = tag.to_s unless (@tag_re = tag_re_from(tag))
  @index = ensure_type(index, PositionOrRange, allow_nil: true)
end

Public Instance Methods

leader?() click to toggle source

Public methods

# File lib/marc/spec/queries/tag.rb, line 33
def leader?
  tag_exact == LDR
end
to_s() click to toggle source

Object overrides

# File lib/marc/spec/queries/tag.rb, line 40
def to_s
  StringIO.new.tap do |out|
    out << tag_str
    out << "[#{index}]" if index
  end.string
end

Protected Instance Methods

can_apply?(marc_obj) click to toggle source

Applicable

# File lib/marc/spec/queries/tag.rb, line 55
def can_apply?(marc_obj)
  marc_obj.is_a?(MARC::Record)
end
do_apply(marc_record) click to toggle source
# File lib/marc/spec/queries/tag.rb, line 59
def do_apply(marc_record)
  return [marc_record.leader] if leader?

  all_fields = all_fields(marc_record)
  index ? index.select_from(all_fields) : all_fields
end
equality_attrs() click to toggle source
# File lib/marc/spec/queries/tag.rb, line 76
def equality_attrs
  %i[tag_str index]
end
to_s_inspect() click to toggle source

Predicate

# File lib/marc/spec/queries/tag.rb, line 69
def to_s_inspect
  StringIO.new.tap do |out|
    out << (tag_re ? tag_re.inspect : tag_exact)
    out << "[#{index.inspect}]" if index
  end.string
end

Private Instance Methods

all_fields(marc_record) click to toggle source

Private methods

# File lib/marc/spec/queries/tag.rb, line 85
def all_fields(marc_record)
  return marc_record.fields(tag_exact) if tag_exact

  [].tap do |ff|
    ff << marc_record.leader if LDR =~ tag_re
    marc_record.each { |field| ff << field if field.tag =~ tag_re }
  end
end
tag_re_from(tag) click to toggle source
# File lib/marc/spec/queries/tag.rb, line 94
def tag_re_from(tag)
  return tag if tag.is_a?(Regexp)

  tag_s = tag.to_s
  Regexp.compile("^#{tag_s}$") if tag_s.include?('.')
end
tag_str() click to toggle source
# File lib/marc/spec/queries/tag.rb, line 101
def tag_str
  @tag_str ||= tag_exact || tag_re.source.gsub(/^\^(.*)\$$/, '\\1')
end