Sha256: 25f1c408303a9fd048c183f400246a2ecac4d6266d521264d9efc5f67e5fb99f

Contents?: true

Size: 1.81 KB

Versions: 4

Compression:

Stored size: 1.81 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)
          return super "Unknown hook  base '#{base}'" if base
          return super "Unknown hook point '#{point}' given."
        end
      end

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

      HookPoints = {
        :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.has_key?(base) && all[base].has_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] ||= {
           :late => Set.new,
          :early => Set.new
        }

        point[_when]
      end

      #

      def self.register(base, point, _when = :late, &block)
        raise UnknownHookError.new(base: base) unless HookPoints.has_key?(base)
        point = HookAliases[base][point] if  HookAliases.fetch(base, {}).has_key?(point)
        raise UnknownHookError.new(point: point) unless HookPoints[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.0.3 lib/jekyll/assets/hook.rb
jekyll-assets-2.0.2 lib/jekyll/assets/hook.rb
jekyll-assets-2.0.1 lib/jekyll/assets/hook.rb
jekyll-assets-2.0.0 lib/jekyll/assets/hook.rb