Sha256: 23ec826f1665a804a28cccd69f555228d566f582289aca5a5d9b9eaeb5af3159

Contents?: true

Size: 1.18 KB

Versions: 27

Compression:

Stored size: 1.18 KB

Contents

# frozen_string_literal: true

module Bridgetown
  module Resource
    class TaxonomyType
      # @return [Bridgetown::Site]
      attr_reader :site

      # @return [String] aka `category`, `tag`, `region`, etc.
      attr_reader :label

      # @return [String] the key used in front matter
      attr_reader :key

      # @return [HashWithDotAccess::Hash] any associated metadata
      attr_reader :metadata

      # @param site [Bridgetown::Site]
      # @param label [String]
      # @param key [String]
      def initialize(site:, label:, key:, metadata:)
        @site = site
        @label = label
        @key = key
        @metadata = metadata
      end

      def terms
        site.resources.map do |resource|
          resource.taxonomies[label].terms
        end.flatten.group_by(&:label).with_dot_access
      end

      def inspect
        "#<#{self.class} label=#{label}>"
      end

      def to_liquid
        {
          "label"    => label,
          "key"      => key,
          "metadata" => metadata,
        }
      end
      alias_method :to_h, :to_liquid

      def as_json(*)
        to_h
      end

      def to_json(...)
        as_json(...).to_json(...)
      end
    end
  end
end

Version data entries

27 entries across 27 versions & 1 rubygems

Version Path
bridgetown-core-1.0.0.beta2 lib/bridgetown-core/resource/taxonomy_type.rb
bridgetown-core-1.0.0.beta1 lib/bridgetown-core/resource/taxonomy_type.rb
bridgetown-core-1.0.0.alpha11 lib/bridgetown-core/resource/taxonomy_type.rb
bridgetown-core-1.0.0.alpha10 lib/bridgetown-core/resource/taxonomy_type.rb
bridgetown-core-1.0.0.alpha9 lib/bridgetown-core/resource/taxonomy_type.rb
bridgetown-core-1.0.0.alpha8 lib/bridgetown-core/resource/taxonomy_type.rb
bridgetown-core-1.0.0.alpha7 lib/bridgetown-core/resource/taxonomy_type.rb