Sha256: 88192d9530691e52a0de0c3ebc947de0784b0c402443148b1c41acd2ee501b2b

Contents?: true

Size: 1.4 KB

Versions: 16

Compression:

Stored size: 1.4 KB

Contents

# frozen_string_literal: true

module Blacklight
  module Hierarchy
    class FacetTree
      def self.build(prefix:, facet_display:, facet_field:)
        new(prefix: prefix, facet_display: facet_display, facet_field: facet_field).build
      end

      def initialize(prefix:, facet_display:, facet_field:)
        @prefix = prefix
        @facet_config = facet_display.dig(:hierarchy, prefix)
        @facet_field = facet_field
      end

      attr_reader :prefix, :facet_config, :data

      def build
        return unless facet_config
        {}.tap do |tree|
          facet_config.first.each do |key|
            # TODO: remove baked in notion of underscores being part of the blacklight facet field names
            facet_field = [prefix, key].compact.join('_')
            tree[facet_field] ||= {}
            data = @facet_field.display_facet
            next if data.nil?
            data.items.each do |facet_item|
              path = facet_item.value.split(split_regex)
              loc = tree[facet_field]
              loc = loc[path.shift] ||= {} while path.length > 0
              loc[:_] = HierarchicalFacetItem.new(facet_item.value, facet_item.value.split(split_regex).last, facet_item.hits)
            end
          end
        end
      end

      def split_regex
        @split_regex ||= Regexp.new("\s*#{Regexp.escape(facet_config.length >= 2 ? facet_config[1] : ':')}\s*")
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
blacklight-hierarchy-6.4.0 app/models/blacklight/hierarchy/facet_tree.rb
blacklight-hierarchy-6.3.0 app/models/blacklight/hierarchy/facet_tree.rb
blacklight-hierarchy-6.2.2 app/models/blacklight/hierarchy/facet_tree.rb
blacklight-hierarchy-6.2.1 app/models/blacklight/hierarchy/facet_tree.rb
blacklight-hierarchy-6.2.0 app/models/blacklight/hierarchy/facet_tree.rb
blacklight-hierarchy-6.1.2 app/models/blacklight/hierarchy/facet_tree.rb
blacklight-hierarchy-6.1.1 app/models/blacklight/hierarchy/facet_tree.rb
blacklight-hierarchy-6.1.0 app/models/blacklight/hierarchy/facet_tree.rb
blacklight-hierarchy-6.0.2 app/models/blacklight/hierarchy/facet_tree.rb
blacklight-hierarchy-6.0.1 app/models/blacklight/hierarchy/facet_tree.rb
blacklight-hierarchy-6.0.0 app/models/blacklight/hierarchy/facet_tree.rb
blacklight-hierarchy-5.4.0 app/models/blacklight/hierarchy/facet_tree.rb
blacklight-hierarchy-5.3.0 app/models/blacklight/hierarchy/facet_tree.rb
blacklight-hierarchy-5.2.0 app/models/blacklight/hierarchy/facet_tree.rb
blacklight-hierarchy-5.1.0 app/models/blacklight/hierarchy/facet_tree.rb
blacklight-hierarchy-5.0.0 app/models/blacklight/hierarchy/facet_tree.rb