Sha256: 9ec3388dc8ee101c2c2d2c933ed81c193cc56ef4b235afa8b167dbfc13dc30ba

Contents?: true

Size: 1.75 KB

Versions: 4

Compression:

Stored size: 1.75 KB

Contents

# Frozen-string-literal: true
# Copyright: 2012-2015 - MIT License
# Encoding: utf-8

module Jekyll
  module Assets
    class Hook
      class UnknownHookError < RuntimeError
        def initialize(base: nil, point: nil)
          super "Unknown hook #{base ? "base" : "point"} '#{base || point}'"
        end
      end

      HOOK_ALIASES = {
        :env => {
          :post_init => :init,
          :pre_init  => :init
        }
      }

      HOOK_POINTS = {
        :env => [
          :init
        ]
      }

      def self.all
        @_all ||= {}
      end

      # Trigger a hook, giving an optional block where we pass you the,
      # proc we got and then you can do as you please (such as instance eval)
      # but if you do not give us one then we simply pass the args.

      def self.trigger(base, point_, *args, &block)
        raise ArgumentError, "Do not give args with a block" if args.size > 0 && block_given?
        if all.key?(base) && all[base].key?(point_)
          Set.new.merge(point(base, point_, :early)).merge(point(base, point_)).map do |v|
            block_given?? block.call(v) : v.call(*args)
          end
        end
      end

      #

      def self.point(base, point, when_ = :late)
        point = all[base][point] ||= {
          :early => Set.new,
          :late  => Set.new
        }

        point[when_]
      end

      #

      def self.register(base, point, when_ = :late, &block)
        raise UnknownHookError, base: base unless HOOK_POINTS.key?(base)
        point = HOOK_ALIASES[base][point] if HOOK_ALIASES.fetch(base, {}).key?(point)
        raise UnknownHookError, point: point unless HOOK_POINTS[base].include?(point)
        all[base] ||= {}

        point(base, point, when_) \
          .add(block)
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
jekyll-assets-2.1.3 lib/jekyll/assets/hook.rb
jekyll-assets-2.1.2 lib/jekyll/assets/hook.rb
jekyll-assets-2.1.1 lib/jekyll/assets/hook.rb
jekyll-assets-2.1.0 lib/jekyll/assets/hook.rb