Class: Jekyll::Assets::Hook

Inherits:
Object
  • Object
show all
Defined in:
lib/jekyll/assets/hook.rb

Defined Under Namespace

Classes: Point, UnknownHookError

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.pointsObject (readonly)

Returns the value of attribute points


31
32
33
# File 'lib/jekyll/assets/hook.rb', line 31

def points
  @points
end

Class Method Details

.add_point(*point) ⇒ Hash<Hash<Array>>

Note:

plugins can create their own points if wished.

– Create a hook point to attach hooks to. –

Parameters:

  • point (Array<String,Symbol>)

    the parent and child.

Returns:

  • (Hash<Hash<Array>>)

Raises:

  • (ArgumentError)

66
67
68
69
70
71
72
# File 'lib/jekyll/assets/hook.rb', line 66

def self.add_point(*point)
  raise ArgumentError, "only give 2 points" if point.count > 2

  @points[point[0]] ||= {}
  @points[point[0]][point[1]] ||= {}
  @points
end

.check_point(*point) ⇒ nil

– Checks that a point exists or raises an error. –

Parameters:

  • point

    the points to check.

Returns:

  • (nil)

Raises:

  • (ArgumentError)

124
125
126
127
128
129
# File 'lib/jekyll/assets/hook.rb', line 124

def self.check_point(*point)
  raise ArgumentError, "only give 2 points" if point.count > 2
  if !@points.key?(point[0]) || !@points[point[0]].key?(point[1])
    raise ArgumentError, "Unknown hook #{point}"
  end
end

.get_point(*point) ⇒ Array<Proc>

Note:

this is really internal.

– Get a hook point. –

Parameters:

  • point (Array<String,Symbol>)

    the parent and child.

Returns:

  • (Array<Proc>)

80
81
82
83
84
# File 'lib/jekyll/assets/hook.rb', line 80

def self.get_point(*point)
  check_point(*point)
  @points[point[0]][point[1]]
    .sort_by(&:priority)
end

.register(*point, priority: 48, &block) ⇒ nil

Note:

this is what plugins should use.

– Register a hook on a hook point. –

Parameters:

  • point (Array<String,Symbol>)

    the parent and child.

  • priority (Integer) (defaults to: 48)

    your priority.

Returns:

  • (nil)

111
112
113
114
115
116
117
# File 'lib/jekyll/assets/hook.rb', line 111

def self.register(*point, priority: 48, &block)
  check_point(*point)
  point_ = Point.new(priority, &block)
  out = @points[point[0]]
  out = out[point[1]]
  out << point_
end

.trigger(*point, &block) ⇒ nil

Note:

plugins can trigger their own hooks.

– Trigger a hook point. –

Parameters:

  • point (Array<String,Symbol>)

    the parent and child.

  • block (Proc{})

    the code to run.

Returns:

  • (nil)

See Also:

  • selfself.add_point

94
95
96
97
98
99
100
101
102
# File 'lib/jekyll/assets/hook.rb', line 94

def self.trigger(*point, &block)
  hooks = get_point(*point)
  Logger.debug "messaging hooks on #{point.last} " \
    "through #{point.first}"

  hooks.map do |v|
    block.call(v.block)
  end
end