Class: Blacklight::Hierarchy::FacetTree

Inherits:
Object
  • Object
show all
Defined in:
app/models/blacklight/hierarchy/facet_tree.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(prefix:, facet_display:, facet_field:) ⇒ FacetTree

Returns a new instance of FacetTree.



10
11
12
13
14
# File 'app/models/blacklight/hierarchy/facet_tree.rb', line 10

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

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



16
17
18
# File 'app/models/blacklight/hierarchy/facet_tree.rb', line 16

def data
  @data
end

#facet_configObject (readonly)

Returns the value of attribute facet_config.



16
17
18
# File 'app/models/blacklight/hierarchy/facet_tree.rb', line 16

def facet_config
  @facet_config
end

#prefixObject (readonly)

Returns the value of attribute prefix.



16
17
18
# File 'app/models/blacklight/hierarchy/facet_tree.rb', line 16

def prefix
  @prefix
end

Class Method Details

.build(prefix:, facet_display:, facet_field:) ⇒ Object



6
7
8
# File 'app/models/blacklight/hierarchy/facet_tree.rb', line 6

def self.build(prefix:, facet_display:, facet_field:)
  new(prefix: prefix, facet_display: facet_display, facet_field: facet_field).build
end

Instance Method Details

#buildObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/models/blacklight/hierarchy/facet_tree.rb', line 18

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

#split_regexObject



37
38
39
# File 'app/models/blacklight/hierarchy/facet_tree.rb', line 37

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