Sha256: 5ff838b920490bb3be6685b21ed6cf8cce73595bf0863e8152cb90076302cf7a

Contents?: true

Size: 1.89 KB

Versions: 21

Compression:

Stored size: 1.89 KB

Contents

module Krikri
  ##
  # Enrich a specific field or list of fields, setting the property to the
  # supplied value
  module FieldEnrichment
    include Enrichment

    ##
    # 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)
    #
    # @see Krikri::Enrichment#enrich for documentation about field
    #   formatting
    #
    # @param record [ActiveTriples::Resource] the record to enrich
    # @param fields [Array] the fields on which to apply the enrichment
    # @return [ActiveTriples::Resource] the enriched record
    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

    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

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

Version data entries

21 entries across 21 versions & 2 rubygems

Version Path
audumbla-0.2.1 lib/audumbla/field_enrichment.rb~
audumbla-0.2.0 lib/audumbla/field_enrichment.rb~
krikri-0.6.0 lib/krikri/field_enrichment.rb
audumbla-0.1.0 lib/audumbla/field_enrichment.rb~
krikri-0.5.7 lib/krikri/field_enrichment.rb
krikri-0.5.6 lib/krikri/field_enrichment.rb
krikri-0.5.5 lib/krikri/field_enrichment.rb
krikri-0.5.4 lib/krikri/field_enrichment.rb
krikri-0.5.3 lib/krikri/field_enrichment.rb
krikri-0.5.2 lib/krikri/field_enrichment.rb
krikri-0.5.1 lib/krikri/field_enrichment.rb
krikri-0.5.0 lib/krikri/field_enrichment.rb
krikri-0.4.0 lib/krikri/field_enrichment.rb
krikri-0.3.3 lib/krikri/field_enrichment.rb
krikri-0.3.2 lib/krikri/field_enrichment.rb
krikri-0.3.1 lib/krikri/field_enrichment.rb
krikri-0.2.1 lib/krikri/field_enrichment.rb
krikri-0.2.0 lib/krikri/field_enrichment.rb
krikri-0.1.3 lib/krikri/field_enrichment.rb
krikri-0.1.2 lib/krikri/field_enrichment.rb