lib/jekyll/assets/hook.rb in jekyll-assets-3.0.7 vs lib/jekyll/assets/hook.rb in jekyll-assets-3.0.8
- old
+ new
@@ -10,82 +10,52 @@
super "Unknown hook point `#{point}'"
end
end
# --
+ class Point
+ attr_accessor :block, :priority
+
+ # --
+ # A hook point only holds data for later, it
+ # really serves no other purpose for now, other
+ # than to make live easier for handling hooks
+ # and their sorting, later in stream.
+ # --
+ def initialize(priority, &block)
+ @priority, @block = priority, block
+ end
+ end
+
+ # --
class << self
attr_reader :points
end
# --
@points = {
env: {
- before_init: {
- 1 => [],
- 2 => [],
- 3 => [],
- },
-
- after_init: {
- 1 => [],
- 2 => [],
- 3 => [],
- },
-
- after_write: {
- 1 => [],
- 2 => [],
- 3 => [],
- },
+ before_init: [],
+ after_init: [],
+ after_write: [],
},
config: {
- before_merge: {
- 1 => [],
- 2 => [],
- 3 => [],
- },
+ before_merge: [],
},
asset: {
- before_compile: {
- 1 => [],
- 2 => [],
- 3 => [],
- },
-
- before_read: {
- 1 => [],
- 2 => [],
- 3 => [],
- },
-
- after_read: {
- 1 => [],
- 2 => [],
- 3 => [],
- },
-
- before_write: {
- 1 => [],
- 2 => [],
- 3 => [],
- },
-
- after_write: {
- 1 => [],
- 2 => [],
- 3 => [],
- },
+ before_compile: [],
+ before_read: [],
+ after_read: [],
+ after_compression: [],
+ before_write: [],
+ after_write: [],
},
liquid: {
- before_render: {
- 1 => [],
- 2 => [],
- 3 => [],
- },
+ before_render: [],
},
}
# --
# Create a hook point to attach hooks to.
@@ -95,20 +65,11 @@
# --
def self.add_point(*point)
raise ArgumentError, "only give 2 points" if point.count > 2
@points[point[0]] ||= {}
- @points[point[0]][point[1]] ||= {
- #
- }
-
- 1.upto(3).each do |i|
- @points[point[0]][point[1]][i] ||= [
- #
- ]
- end
-
+ @points[point[0]][point[1]] ||= {}
@points
end
# --
# @return [Array<Proc>]
@@ -116,15 +77,12 @@
# @note this is really internal.
# Get a hook point.
# --
def self.get_point(*point)
check_point(*point)
-
@points[point[0]][point[1]]
- .each_with_object([]) do |(_, v), a|
- a.concat(v)
- end
+ .sort_by(&:priority)
end
# --
# Trigger a hook point.
# @note plugins can trigger their own hooks.
@@ -137,31 +95,26 @@
hooks = get_point(*point)
Logger.debug "messaging hooks on #{point.last} " \
"through #{point.first}"
hooks.map do |v|
- block.call(v)
+ block.call(v.block)
end
end
# --
# Register a hook on a hook point.
# @param [Array<String,Symbol>] point the parent and child.
# @param [Integer] priority your priority.
# @note this is what plugins should use.
# @return [nil]
# --
- def self.register(*point, priority: 2, &block)
- if priority > 3
- raise ArgumentError,
- "priority must be between 1 and 3"
- end
-
+ def self.register(*point, priority: 48, &block)
check_point(*point)
+ point_ = Point.new(priority, &block)
out = @points[point[0]]
out = out[point[1]]
- out = out[priority]
- out << block
+ out << point_
end
# --
# @param point the points to check.
# Checks that a point exists or raises an error.