Module: Coradoc::Hooks

Defined in:
lib/coradoc/hooks.rb

Constant Summary collapse

HOOK_POINTS =
{
  before_parse: 'Called before parsing text content. Receives (content, format:, **options). Should return modified content.',
  after_parse: 'Called after parsing. Receives (model, format:, **options). Should return modified model.',
  before_transform: 'Called before transforming model. Receives (model, direction:, **options). Should return modified model.',
  after_transform: 'Called after transforming model. Receives (model, direction:, **options). Should return modified model.',
  before_serialize: 'Called before serializing model. Receives (model, format:, **options). Should return modified model.',
  after_serialize: 'Called after serializing. Receives (output, format:, **options). Should return modified output.',
  on_error: 'Called when an error occurs. Receives (error, context). Can re-raise or return fallback value.'
}.freeze

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.strict_modeObject

Returns the value of attribute strict_mode.



18
19
20
# File 'lib/coradoc/hooks.rb', line 18

def strict_mode
  @strict_mode
end

Class Method Details

.clear(hook_point) ⇒ Object



45
46
47
48
49
50
# File 'lib/coradoc/hooks.rb', line 45

def clear(hook_point)
  validate_hook_point!(hook_point)
  removed = registry[hook_point]&.size || 0
  registry.delete(hook_point)
  removed
end

.clear_allObject



52
53
54
55
56
57
# File 'lib/coradoc/hooks.rb', line 52

def clear_all
  total = registry.values.sum(&:size)
  @registry = {}
  @sequence_counter = nil
  total
end

.documentation(hook_point = nil) ⇒ Object



100
101
102
103
104
105
106
107
# File 'lib/coradoc/hooks.rb', line 100

def documentation(hook_point = nil)
  if hook_point
    validate_hook_point!(hook_point)
    HOOK_POINTS[hook_point]
  else
    HOOK_POINTS.dup
  end
end

.invoke(hook_point, *args, **kwargs) ⇒ Object



75
76
77
78
79
80
81
82
83
84
# File 'lib/coradoc/hooks.rb', line 75

def invoke(hook_point, *args, **kwargs)
  validate_hook_point!(hook_point)
  return args.first if args.one? && !registry[hook_point]&.any?

  result = args.first
  registry[hook_point].each do |hook|
    result = invoke_hook(hook[:callback], result, args, kwargs)
  end
  result
end

.list(hook_point = nil) ⇒ Object



64
65
66
67
68
69
70
71
72
73
# File 'lib/coradoc/hooks.rb', line 64

def list(hook_point = nil)
  if hook_point
    validate_hook_point!(hook_point)
    registry[hook_point]&.map { |h| h.merge(point: hook_point) } || []
  else
    registry.flat_map do |point, hooks|
      hooks.map { |h| h.merge(point: point) }
    end
  end
end

.register(hook_point, priority: 100, name: nil, &block) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/coradoc/hooks.rb', line 20

def register(hook_point, priority: 100, name: nil, &block)
  validate_hook_point!(hook_point)
  validate_block!(block, hook_point)

  hook_id = name || generate_hook_id(hook_point)
  registry[hook_point] ||= []
  registry[hook_point] << {
    id: hook_id,
    priority: priority,
    sequence: next_sequence,
    callback: block
  }
  registry[hook_point].sort_by! { |h| [h[:priority], h[:sequence]] }
  hook_id
end

.registered?(hook_point) ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
62
# File 'lib/coradoc/hooks.rb', line 59

def registered?(hook_point)
  validate_hook_point!(hook_point)
  registry[hook_point]&.any? || false
end

.remove(hook_point, hook_id) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/coradoc/hooks.rb', line 36

def remove(hook_point, hook_id)
  validate_hook_point!(hook_point)
  return false unless registry[hook_point]

  original_size = registry[hook_point].size
  registry[hook_point].reject! { |h| h[:id] == hook_id }
  registry[hook_point].size < original_size
end

.with_hooks(hook_point, *args, **kwargs) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/coradoc/hooks.rb', line 86

def with_hooks(hook_point, *args, **kwargs)
  validate_hook_point!(hook_point)

  modified_args = invoke(hook_point, *args, **kwargs)
  result = yield(*modified_args)

  after_point = "after_#{hook_point.to_s.sub('before_', '')}".to_sym
  result = invoke(after_point, result, **kwargs) if HOOK_POINTS.key?(after_point)

  result
rescue StandardError => e
  invoke_error_hooks(e, hook_point, args, kwargs)
end