Class: Textus::Hooks::Loader

Inherits:
Object
  • Object
show all
Defined in:
lib/textus/hooks/loader.rb

Defined Under Namespace

Classes: Dsl

Instance Method Summary collapse

Constructor Details

#initialize(events:, rpc:) ⇒ Loader

Returns a new instance of Loader.



28
29
30
31
32
# File 'lib/textus/hooks/loader.rb', line 28

def initialize(events:, rpc:)
  @events = events
  @rpc    = rpc
  @dsl    = Dsl.new(events: @events, rpc: @rpc)
end

Instance Method Details

#load_dir(dir) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/textus/hooks/loader.rb', line 34

def load_dir(dir)
  return unless File.directory?(dir)

  # Discard any leftover blocks from a prior partial load.
  Textus.drain_hook_blocks

  Dir.glob(File.join(dir, "**/*.rb")).sort.each do |f| # rubocop:disable Lint/RedundantDirGlobSort
    load(f)
  rescue StandardError, ScriptError => e
    raise UsageError.new("failed loading hook #{File.basename(f)}: #{e.class}: #{e.message}")
  end

  Textus.drain_hook_blocks.each do |blk|
    blk.call(@dsl)
  rescue StandardError, ScriptError => e
    raise UsageError.new("failed registering hook: #{e.class}: #{e.message}")
  end
end