Class: Hammer::Loader

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

Overview

Discovers and evaluates Hammerfile fragments (‘*_hammer.rb`) against a target `Hammer` subclass. One Loader per target class - the target memoises it via `target.loader`. Holds the per-target dedup cache so re-entrant `load` is safe.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target) ⇒ Loader

Returns a new instance of Loader.



11
12
13
14
# File 'lib/hammer/loader.rb', line 11

def initialize(target)
  @target = target
  @loaded = {}
end

Instance Attribute Details

#loadedObject (readonly)

Returns the value of attribute loaded.



9
10
11
# File 'lib/hammer/loader.rb', line 9

def loaded
  @loaded
end

Class Method Details

.caller_anchor(loc) ⇒ Object

Resolve ‘caller_locations(…).first` to a directory.



17
18
19
20
# File 'lib/hammer/loader.rb', line 17

def self.caller_anchor(loc)
  path = loc&.absolute_path || loc&.path
  path ? File.dirname(path) : Dir.pwd
end

Instance Method Details

#load(anchor, paths, kwargs) ⇒ Object

Raises:

  • (Hammer::Error)


22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/hammer/loader.rb', line 22

def load(anchor, paths, kwargs)
  bad = kwargs.keys - %i[auto]
  raise Hammer::Error, "load: unknown option(s): #{bad.join(', ')}" unless bad.empty?
  return if kwargs.key?(:auto) && !kwargs[:auto]

  files =
    if paths.empty?
      discover(anchor)
    else
      paths.flat_map { |p| resolve_pattern(anchor, p) }.uniq.sort
    end

  files.each { |f| load_one(f) }
end