class Krikri::Enrichments::SplitProvidedLabelAtDelimiter

Splits a Resource into multiple resources of its own class by a given delimiter in its `#providedLabel`. The full original resource is retained with the first value from the split label.

@example

splitter = SplitProvidedLabelAtDelimiter.new
concept = DPLA::MAP::Concept.new
concept.providedLabel = 'abc; 123'
concept.exactMatch = RDF::URI('http://example.org/alphabet')

results = splitter.enrich_value(concept)

results.map(&:providedLabel)
# => [['abc'], ['123']]

results.map(&:exactMatch)
# => [[#<ActiveTriple::Resource:...>], []]

@see Audumbla::FieldEnrichment

Public Class Methods

new(delimiter = ';') click to toggle source

@param delimiter [String] a substring on which to split `#providedLabel`

# File lib/krikri/enrichments/split_provided_label_at_delimiter.rb, line 27
def initialize(delimiter = ';')
  @delimiter = delimiter
end

Public Instance Methods

enrich_value(value) click to toggle source

@param value [Object] the value to split @see Audumbla::FieldEnrichment

# File lib/krikri/enrichments/split_provided_label_at_delimiter.rb, line 34
def enrich_value(value)
  return value unless value.is_a?(ActiveTriples::Resource) &&
                      value.respond_to?(:providedLabel)

  construct_results(value)
end

Private Instance Methods

build_resource(klass, providedLabel) click to toggle source

@param klass [Class] @param providedLabel [String]

@return [klass] a new resource of `klass` with the providedLabel given

# File lib/krikri/enrichments/split_provided_label_at_delimiter.rb, line 64
def build_resource(klass, providedLabel)
  resource = klass.new
  resource.providedLabel = providedLabel
  resource
end
construct_results(value) click to toggle source

@param value [ActiveTriples::Resource]

@return [Array<ActiveTriples::Resource>] an array of resources derived

from the split `#providedLabel` of `value`
# File lib/krikri/enrichments/split_provided_label_at_delimiter.rb, line 48
def construct_results(value)
  values = split_value(value)

  value.providedLabel = values.shift
  results = [value]

  values.each { |v| results << build_resource(value.class, v) }

  results
end
split_value(value) click to toggle source

@param value [#providedLabel] @return [Array<String>] a flat array of provided labels split from

the original
# File lib/krikri/enrichments/split_provided_label_at_delimiter.rb, line 74
def split_value(value)
  value.providedLabel.map { |l| l.split(';').map(&:strip) }.flatten
end