Class: Snails::Loader

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(loader: nil, load_paths: nil, preload_paths: nil, postload_paths: nil) ⇒ Loader

Returns a new instance of Loader.



15
16
17
18
19
20
21
22
# File 'lib/snails/loader.rb', line 15

def initialize(loader: nil, load_paths: nil, preload_paths: nil, postload_paths: nil)
  @loader = loader || Zeitwerk::Loader.new
  @load_paths = load_paths || DEFAULT_LOAD_PATHS
  @preload_paths = preload_paths || DEFAULT_PRELOAD_PATHS
  @postload_paths = postload_paths || []
  @ignore_paths = []
  @no_eager_load_paths = []
end

Instance Attribute Details

#ignore_pathsObject

Returns the value of attribute ignore_paths.



13
14
15
# File 'lib/snails/loader.rb', line 13

def ignore_paths
  @ignore_paths
end

#load_pathsObject

Returns the value of attribute load_paths.



13
14
15
# File 'lib/snails/loader.rb', line 13

def load_paths
  @load_paths
end

#loaderObject (readonly)

Returns the value of attribute loader.



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

def loader
  @loader
end

#no_eager_load_pathsObject

Returns the value of attribute no_eager_load_paths.



13
14
15
# File 'lib/snails/loader.rb', line 13

def no_eager_load_paths
  @no_eager_load_paths
end

#postload_pathsObject

Returns the value of attribute postload_paths.



13
14
15
# File 'lib/snails/loader.rb', line 13

def postload_paths
  @postload_paths
end

#preload_pathsObject

Returns the value of attribute preload_paths.



13
14
15
# File 'lib/snails/loader.rb', line 13

def preload_paths
  @preload_paths
end

Instance Method Details

#load!(opts = {}) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/snails/loader.rb', line 28

def load!(opts = {})
  # $LOAD_PATH.unshift(File.join(Snails.root, 'lib'))

  preload_paths.each do |path|
    Dir.glob(Snails.root.join(path, '*.rb')).each { |f| require f }
  end

  load_paths.each do |path|
    loader.push_dir(Snails.root.join(path))
  end

  ignore_paths.each do |path|
    loader.ignore(Snails.root.join(path))
  end

  no_eager_load_paths.each do |path|
    loader.do_not_eager_load(Snails.root.join(path))
  end

  loader.enable_reloading if opts[:enable_reload]
  loader.setup

  postload_paths.each do |path|
    Dir.glob(Snails.root.join(path, '*.rb')).each { |f| require f }
  end

  loader.eager_load if opts[:eager_load]

  @loaded = true

  # enable reloading for already defined apps
  Snails.apps.each do |app|
    app.set :lock, true
  end if Snails.env.dev?
end

#loaded?Boolean

Returns:



24
25
26
# File 'lib/snails/loader.rb', line 24

def loaded?
  @loaded
end

#reload!Object



64
65
66
# File 'lib/snails/loader.rb', line 64

def reload!
  loader.reload
end