Module: Krikri::FieldEnrichment

Overview

Enrich a specific field or list of fields, setting the property to the supplied value

Constant Summary

Constants included from SoftwareAgent

SoftwareAgent::Logger

Instance Method Summary collapse

Methods included from Enrichment

#enrich!, #enrich_value, #list_fields

Methods included from SoftwareAgent

#agent_name, #log, #run

Instance Method Details

#enrich(record, *fields) ⇒ ActiveTriples::Resource

The main enrichment method; runs the enrichment against a stated set of fields for a record.

This is a narrower case of `Krikri::Enrichment` which runs the enrichment against each of the specified fields in turn, setting the field's value to the result.

For example:

delete_empty_string_literals.enrich(record,
  {:sourceResource => {:creator => :name}})

To apply the enrichment across all fields, leave the fields parameter empty, or use `:all`:

delete_empty_string_literals.enrich(record)
delete_empty_string_literals.enrich(record, :all)

Parameters:

  • record (ActiveTriples::Resource)

    the record to enrich

  • fields (Array)

    the fields on which to apply the enrichment

Returns:

  • (ActiveTriples::Resource)

    the enriched record

See Also:


33
34
35
36
37
38
# File 'lib/krikri/field_enrichment.rb', line 33

def enrich(record, *fields)
  record = record.clone
  return enrich_all(record) if fields.empty? || fields == [:all]
  fields.each { |f| enrich_field(record, field_to_chain(f)) }
  record
end

#enrich_all(record) ⇒ Object


53
54
55
56
57
58
# File 'lib/krikri/field_enrichment.rb', line 53

def enrich_all(record)
  list_fields(record).each do |field|
    enrich_field(record, field_to_chain(field))
  end
  record
end

#enrich_field(record, field_chain) ⇒ Object


40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/krikri/field_enrichment.rb', line 40

def enrich_field(record, field_chain)
  field = field_chain.first
  return record unless record.respond_to? field
  values = record.send(field)
  if field_chain.length == 1
    new_values = values.map { |v| enrich_value(v) }.flatten.compact
    record.send("#{field}=".to_sym, new_values)
  else
    resources(values).each { |v| enrich_field(v, field_chain[1..-1]) }
  end
  record
end