Sha256: 4d2f220af614c552e21e96a91637b41571944767ccbffbe691610b838f711120

Contents?: true

Size: 1.25 KB

Versions: 13

Compression:

Stored size: 1.25 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)
        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 exists?
        false
      end

      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

13 entries across 13 versions & 1 rubygems

Version Path
bridgetown-core-1.1.0 lib/bridgetown-core/model/builder_origin.rb
bridgetown-core-1.1.0.beta3 lib/bridgetown-core/model/builder_origin.rb
bridgetown-core-1.1.0.beta2 lib/bridgetown-core/model/builder_origin.rb
bridgetown-core-1.1.0.beta1 lib/bridgetown-core/model/builder_origin.rb
bridgetown-core-1.0.0 lib/bridgetown-core/model/builder_origin.rb
bridgetown-core-1.0.0.beta3 lib/bridgetown-core/model/builder_origin.rb
bridgetown-core-1.0.0.beta2 lib/bridgetown-core/model/builder_origin.rb
bridgetown-core-1.0.0.beta1 lib/bridgetown-core/model/builder_origin.rb
bridgetown-core-1.0.0.alpha11 lib/bridgetown-core/model/builder_origin.rb
bridgetown-core-1.0.0.alpha10 lib/bridgetown-core/model/builder_origin.rb
bridgetown-core-1.0.0.alpha9 lib/bridgetown-core/model/builder_origin.rb
bridgetown-core-1.0.0.alpha8 lib/bridgetown-core/model/builder_origin.rb
bridgetown-core-1.0.0.alpha7 lib/bridgetown-core/model/builder_origin.rb