Sha256: b01f95e8b8dc40cc1259bd4502cae4f7dd9198e8d2dba18f689523c42f1daa71

Contents?: true

Size: 1.42 KB

Versions: 1

Compression:

Stored size: 1.42 KB

Contents

# frozen_string_literal: true

module Pakyow
  module Presenter
    class ProcessorCaller
      def initialize(instances)
        @processors = normalize(instances)
      end

      def process(content, extension)
        processors_for_extension(extension).each do |processor|
          content = processor.process(content)
        end

        unless extension == :html
          processors_for_extension(:html).each do |processor|
            content = processor.process(content)
          end
        end

        content
      end

      def process?(extension)
        @processors.key?(extension.tr(".", "").to_sym)
      end

      protected

      def processors_for_extension(extension)
        @processors[extension] || []
      end

      def normalize(instances)
        instances.each_with_object({}) { |instance, processors|
          instance.extensions.each do |extension|
            (processors[extension] ||= []) << instance
          end
        }
      end
    end

    class Processor
      extend Support::Makeable

      class << self
        attr_reader :name, :extensions, :block

        def make(name, *extensions, **kwargs, &block)
          # name is expected to also be an extension
          extensions.unshift(name).map!(&:to_sym)
          super(name, extensions: extensions, block: block, **kwargs) {}
        end

        def process(content)
          block.call(content)
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pakyow-presenter-1.0.0.rc1 lib/pakyow/presenter/processor.rb