lib/jekyll/assets/hook.rb in jekyll-assets-2.0.0.pre.beta2 vs lib/jekyll/assets/hook.rb in jekyll-assets-2.0.0.pre.beta3

- old
+ new

@@ -1,45 +1,62 @@ module Jekyll module Assets class Hook class UnknownHookError < RuntimeError - def initialize(base, point) - super "Unknown jekyll-assets hook point (#{point}) or base (#{base}) given." + def initialize(base: nil, point: nil) + return super "Unknown hook base '#{base}'" if base + return super "Unknown hook point '#{point}' given." end end - HOOK_POINTS = { + HookAliases = { + :env => { + :post_init => :init, + :pre_init => :init + } + } + + HookPoints = { :env => [ - :pre_init, :post_init + :init ] } - class << self - def all - @_all ||= {} - end + def self.all + @_all ||= {} + end - def trigger(base, point, *args) - if all[base][point] - then all[base][point].map do |v| - v.call(*args) - 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.fetch(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 point(base, point) - all[base][point] ||= Set.new - end + # --------------------------------------------------------------------- - def register(base, point, &block) - if HOOK_POINTS.has_key?(base) && HOOK_POINTS[base].include?(point) - all[base] ||= {} - point(base, point) << \ - block - else - raise UnknownHookError.new(base, point) - end - end + def self.point(base, point, _when = :late) + point = all.fetch(base).fetch(point, nil) || all.fetch(base).store(point, { + :late => Set.new, :early => Set.new }) + point.fetch(_when) + end + + # --------------------------------------------------------------------- + + def self.register(base, point, _when = :late, &block) + raise UnknownHookError.new(base: base) unless HookPoints.has_key?(base) + point = HookAliases.fetch(base).fetch(point) if HookAliases.fetch(base, {}).has_key?(point) + raise UnknownHookError.new(point: point) unless HookPoints.fetch(base).include?(point) + all.fetch(base, nil) || all.store(base, {}) + point(base, point, _when).add(block) end end end end