Sha256: e4c510acda0187f3bd1c9187a84109f78f383c6882dd755a769153604d3acad1

Contents?: true

Size: 1.97 KB

Versions: 9

Compression:

Stored size: 1.97 KB

Contents

# frozen_string_literal: true

require "pakyow/support/class_state"

module Pakyow
  module Presenter
    class Processor
      extend Support::ClassState
      class_state :name
      class_state :block
      class_state :extensions, default: [], reader: false

      extend Support::Makeable

      def initialize(app)
        @app = app
      end

      def call(content)
        self.class.process(content)
      end

      class << self
        # @api private
        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

        def extensions(*extensions)
          if extensions.any?
            @extensions ||= []
            @extensions.concat(extensions.map(&:to_sym)).uniq
          else
            @extensions
          end
        end
      end
    end

    # @api private
    class ProcessorCaller
      def initialize(instances)
        @processors = normalize(instances)
      end

      def process(content, extension)
        content = content.to_s

        processors_for_extension(extension).each do |processor|
          content = processor.call(content)
        end

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

        content
      end

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

      private

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

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

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
pakyow-presenter-1.0.6 lib/pakyow/presenter/processor.rb
pakyow-presenter-1.0.5 lib/pakyow/presenter/processor.rb
pakyow-presenter-1.0.4 lib/pakyow/presenter/processor.rb
pakyow-presenter-1.0.3 lib/pakyow/presenter/processor.rb
pakyow-presenter-1.0.2 lib/pakyow/presenter/processor.rb
pakyow-presenter-1.0.1 lib/pakyow/presenter/processor.rb
pakyow-presenter-1.0.0 lib/pakyow/presenter/processor.rb
pakyow-presenter-1.0.0.rc5 lib/pakyow/presenter/processor.rb
pakyow-presenter-1.0.0.rc4 lib/pakyow/presenter/processor.rb