class CollectionSpace::RefName

CollectionSpace RefName

There are four patterns we need to handle:

Attributes

domain[R]
identifier[R]
label[R]
subtype[R]
type[R]

Public Class Methods

new(refname) click to toggle source
# File lib/collectionspace/client/refname.rb, line 17
def initialize(refname)
  @refname = refname
  @domain = nil
  @type = nil
  @subtype = nil
  @identifier = nil
  @label = nil
  parse
end
parse(refname, return_class = nil) click to toggle source

Convenience class method, so new instance of RefName does not have to be instantiated in order to parse

As of v0.13.1, return_class is added and defaults to nil for backward compatibility Eventually this default will be deprecated, and a parsed RefName object will be returned as the default.

Any new code written using this method should set the return_class parameter to :refname_obj
# File lib/collectionspace/client/refname.rb, line 48
def self.parse(refname, return_class = nil)
  (return_class == :refname_obj) ? new(refname) : new(refname).to_h
end

Public Instance Methods

parse() click to toggle source
# File lib/collectionspace/client/refname.rb, line 27
def parse
  scanner = StringScanner.new(@refname)
  scanner.skip("urn:cspace:")
  @domain = to_next_colon(scanner)
  @type = to_next_colon(scanner)

  case next_segment(scanner)
  when "name"
    set_subtype(scanner)
  when "id"
    set_identifier(scanner)
  end

  self
end
to_h() click to toggle source

Returns a parsed RefName object as a hash. As of v0.13.1, this is equivalent to calling RefName.parse(‘refnamevalue’, :hash) This was added to simplify the process of updating existing code that expects a hash when calling RefName.parse

# File lib/collectionspace/client/refname.rb, line 55
def to_h
  {
    domain: domain,
    type: type,
    subtype: subtype,
    identifier: identifier,
    label: label
  }
end

Private Instance Methods

next_segment(scanner) click to toggle source
# File lib/collectionspace/client/refname.rb, line 67
def next_segment(scanner)
  segment = scanner.check_until(/\(/)
  return nil unless segment

  segment.delete_suffix("(")
end
set_identifier(scanner) click to toggle source
# File lib/collectionspace/client/refname.rb, line 74
def set_identifier(scanner)
  scanner.skip("id(")
  @identifier = to_end_paren(scanner)
  return if scanner.eos?

  set_label(scanner)
end
set_label(scanner) click to toggle source
# File lib/collectionspace/client/refname.rb, line 82
def set_label(scanner)
  scanner.skip("'")
  @label = scanner.rest.delete_suffix("'")
end
set_subtype(scanner) click to toggle source
# File lib/collectionspace/client/refname.rb, line 87
def set_subtype(scanner)
  scanner.skip("name(")
  @subtype = to_end_paren(scanner)

  case next_segment(scanner)
  when nil
    set_label(scanner)
  when ":item:name"
    set_term_identifier(scanner)
  end
end
set_term_identifier(scanner) click to toggle source
# File lib/collectionspace/client/refname.rb, line 99
def set_term_identifier(scanner)
  scanner.skip(":item:name(")
  @identifier = to_end_paren(scanner)
  scanner.skip("'")
  set_label(scanner)
end
to_end_paren(scanner) click to toggle source
# File lib/collectionspace/client/refname.rb, line 106
def to_end_paren(scanner)
  scanner.scan_until(/\)/).delete_suffix(")")
end
to_next_colon(scanner) click to toggle source
# File lib/collectionspace/client/refname.rb, line 110
def to_next_colon(scanner)
  scanner.scan_until(/:/).delete_suffix(":")
end