Sha256: 3f7fa72caddac1c250b8aec17d1e0346c214da0fcf3c725e73c29c9757e6b0c0

Contents?: true

Size: 1.55 KB

Versions: 2

Compression:

Stored size: 1.55 KB

Contents

# frozen_string_literal: true

module Nanoc
  module Int
    # @api private
    class Site
      include Nanoc::Core::ContractsSupport

      attr_reader :code_snippets
      attr_reader :config
      attr_accessor :data_source

      contract C::KeywordArgs[config: Nanoc::Core::Configuration, code_snippets: C::IterOf[Nanoc::Core::CodeSnippet], data_source: C::Named['Nanoc::DataSource']] => C::Any
      def initialize(config:, code_snippets:, data_source:)
        @config = config
        @code_snippets = code_snippets
        @data_source = data_source

        @preprocessed = false

        ensure_identifier_uniqueness(@data_source.items, 'item')
        ensure_identifier_uniqueness(@data_source.layouts, 'layout')
      end

      def mark_as_preprocessed
        @preprocessed = true
      end

      def preprocessed?
        @preprocessed
      end

      def items
        @data_source.items
      end

      def layouts
        @data_source.layouts
      end

      contract C::None => self
      def freeze
        config.freeze
        items.freeze
        layouts.freeze
        code_snippets.__nanoc_freeze_recursively
        self
      end

      contract C::IterOf[C::Or[Nanoc::Core::Item, Nanoc::Core::Layout]], String => self
      def ensure_identifier_uniqueness(objects, type)
        seen = Set.new
        objects.each do |obj|
          if seen.include?(obj.identifier)
            raise Nanoc::Int::Errors::DuplicateIdentifier.new(obj.identifier, type)
          end

          seen << obj.identifier
        end
        self
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
nanoc-4.11.2 lib/nanoc/base/entities/site.rb
nanoc-4.11.1 lib/nanoc/base/entities/site.rb