Sha256: 98947822838f53adff5430984829d10bb0039aa4e5d4f4e87e95a4f9256b9e2b

Contents?: true

Size: 1.4 KB

Versions: 3

Compression:

Stored size: 1.4 KB

Contents

# frozen_string_literal: true

module Bridgetown
  module Model
    class BuilderOrigin < Origin
      # @return [Pathname]
      attr_reader :relative_path

      class << self
        def handle_scheme?(scheme)
          scheme == "builder"
        end

        def id_for_builder_path(builder, path)
          "builder://#{builder.class.name.gsub("::", ".")}/#{path}"
        end
      end

      def initialize(id, site: Bridgetown::Current.site)
        super
        @relative_path = Pathname.new(url.path.delete_prefix("/"))
      end

      def url
        @url ||= URI.parse(id)
      end

      def read
        @data = block_given? ? yield : read_data_from_builder
        @data[:_id_] = id
        @data[:_origin_] = self
        @relative_path = Pathname.new(@data[:_relative_path_]) if @data[:_relative_path_]

        @data
      end

      def front_matter_line_count = @data[:_front_matter_line_count_]

      def original_path = @data[:_original_path_] || relative_path

      def exists? = false

      def read_data_from_builder
        builder = Kernel.const_get(url.host.gsub(".", "::"))
        raise NameError unless builder.instance_methods.include?(:resource_data_for_id)

        builder.new.resource_data_for_id(id) || raise(NameError)
      rescue NameError
        raise(
          Bridgetown::Errors::FatalException,
          "Builder not found which can read #{id}"
        )
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
bridgetown-core-2.0.0.beta3 lib/bridgetown-core/model/builder_origin.rb
bridgetown-core-2.0.0.beta2 lib/bridgetown-core/model/builder_origin.rb
bridgetown-core-2.0.0.beta1 lib/bridgetown-core/model/builder_origin.rb