Sha256: b54a4ef7c38a720ef98579238312f7c85d464421ee87ddbade95b30b6886822f

Contents?: true

Size: 1.43 KB

Versions: 9

Compression:

Stored size: 1.43 KB

Contents

# frozen_string_literal: true

module ModsDisplay
  class Field
    def initialize(values)
      @values = values
    end

    def fields
      return_fields = @values.map do |value|
        ModsDisplay::Values.new(
          label: displayLabel(value) || label,
          values: [element_text(value)],
          field: self
        )
      end
      collapse_fields(return_fields)
    end

    def label
      return nil if @values.nil?

      displayLabel(@values.first)
    end

    def to_html(view_context = ApplicationController.renderer)
      view_context.render ModsDisplay::FieldComponent.with_collection(fields, delimiter: delimiter), layout: false
    end

    def render_in(view_context)
      to_html(view_context)
    end

    private

    def delimiter
      nil
    end

    def displayLabel(element)
      return unless element.respond_to?(:attributes) &&
                    element.attributes['displayLabel'].respond_to?(:value)

      "#{element.attributes['displayLabel'].value}:"
    end

    def collapse_fields(display_fields)
      return display_fields if display_fields.length == 1

      display_fields.slice_when { |before, after| before.label != after.label }.map do |group|
        next group.first if group.length == 1

        ModsDisplay::Values.new(label: group.first.label, values: group.map(&:values).flatten(1))
      end
    end

    def element_text(element)
      element.xpath('.//text()').to_html.strip
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
mods_display-1.4.0 lib/mods_display/fields/field.rb
mods_display-1.3.5 lib/mods_display/fields/field.rb
mods_display-1.3.4 lib/mods_display/fields/field.rb
mods_display-1.3.3 lib/mods_display/fields/field.rb
mods_display-1.3.2 lib/mods_display/fields/field.rb
mods_display-1.3.1 lib/mods_display/fields/field.rb
mods_display-1.3.0 lib/mods_display/fields/field.rb
mods_display-1.2.1 lib/mods_display/fields/field.rb
mods_display-1.2.0 lib/mods_display/fields/field.rb