Module: Im::Loader::Config

Extended by:
Internal
Included in:
Im::Loader
Defined in:
lib/im/loader/config.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Internal

internal

Instance Attribute Details

#inflectorObject

Returns the value of attribute inflector.



10
11
12
# File 'lib/im/loader/config.rb', line 10

def inflector
  @inflector
end

#loggerObject

Returns the value of attribute logger.



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

def logger
  @logger
end

#root_dirsObject (readonly)

Absolute paths of the root directories, as a set:

#<Set: {
  "/Users/fxn/blog/app/channels",
  "/Users/fxn/blog/app/adapters",
  ...
}>

This is a private collection maintained by the loader. The public interface for it is ‘push_dir` and `dirs`.



27
28
29
# File 'lib/im/loader/config.rb', line 27

def root_dirs
  @root_dirs
end

Instance Method Details

#collapse(*glob_patterns) ⇒ Object

Configure directories or glob patterns to be collapsed.



194
195
196
197
198
199
200
# File 'lib/im/loader/config.rb', line 194

def collapse(*glob_patterns)
  glob_patterns = expand_paths(glob_patterns)
  mutex.synchronize do
    collapse_glob_patterns.merge(glob_patterns)
    collapse_dirs.merge(expand_glob_patterns(glob_patterns))
  end
end

#dirs(ignored: false) ⇒ Object

Rturns an array with the absolute paths of the root directories as strings. If ‘ignored` is falsey (default), ignored root directories are filtered out.

These are read-only collections, please add to them with ‘push_dir`.



142
143
144
145
146
147
148
# File 'lib/im/loader/config.rb', line 142

def dirs(ignored: false)
  if ignored || ignored_paths.empty?
    root_dirs
  else
    root_dirs.reject { |root_dir| ignored_path?(root_dir) }
  end.freeze
end

#do_not_eager_load(*paths) ⇒ Object

Let eager load ignore the given files or directories. The constants defined in those files are still autoloadable.



176
177
178
# File 'lib/im/loader/config.rb', line 176

def do_not_eager_load(*paths)
  mutex.synchronize { eager_load_exclusions.merge(expand_paths(paths)) }
end

#enable_reloadingObject

You need to call this method before setup in order to be able to reload. There is no way to undo this, either you want to reload or you don’t.

Raises:



155
156
157
158
159
160
161
162
163
164
165
# File 'lib/im/loader/config.rb', line 155

def enable_reloading
  mutex.synchronize do
    break if @reloading_enabled

    if @setup
      raise Im::Error, "cannot enable reloading after setup"
    else
      @reloading_enabled = true
    end
  end
end

#ignore(*glob_patterns) ⇒ Object

Configure files, directories, or glob patterns to be totally ignored.



183
184
185
186
187
188
189
# File 'lib/im/loader/config.rb', line 183

def ignore(*glob_patterns)
  glob_patterns = expand_paths(glob_patterns)
  mutex.synchronize do
    ignored_glob_patterns.merge(glob_patterns)
    ignored_paths.merge(expand_glob_patterns(glob_patterns))
  end
end

#initializeObject



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/im/loader/config.rb', line 84

def initialize
  @inflector              = Im::Inflector.new
  @logger                 = self.class.default_logger
  @tag                    = SecureRandom.hex(3)
  @initialized_at         = Time.now
  @root_dirs              = Set.new
  @ignored_glob_patterns  = Set.new
  @ignored_paths          = Set.new
  @collapse_glob_patterns = Set.new
  @collapse_dirs          = Set.new
  @eager_load_exclusions  = Set.new
  @reloading_enabled      = false
  @on_setup_callbacks     = []
  @on_load_callbacks      = {}
  @on_unload_callbacks    = {}
end

#log!Object

Logs to ‘$stdout`, handy shortcut for debugging.



270
271
272
# File 'lib/im/loader/config.rb', line 270

def log!
  @logger = ->(msg) { puts msg }
end

#on_load(cpath = :ANY, &block) ⇒ Object

Configure a block to be invoked once a certain constant path is loaded. Supports multiple callbacks, and if there are many, they are executed in the order in which they were defined.

loader.on_load("SomeApiClient") do |klass, _abspath|
  klass.endpoint = "https://api.dev"
end

Can also be configured for any constant loaded:

loader.on_load do |cpath, value, abspath|
  # ...
end

Raises:

  • (TypeError)


230
231
232
233
234
# File 'lib/im/loader/config.rb', line 230

def on_load(cpath = :ANY, &block)
  raise TypeError, "on_load only accepts strings" unless cpath.is_a?(String) || cpath == :ANY

  mutex.synchronize { _on_load(cpath, &block) }
end

#on_setup(&block) ⇒ Object

Configure a block to be called after setup and on each reload. If setup was already done, the block runs immediately.



206
207
208
209
210
211
# File 'lib/im/loader/config.rb', line 206

def on_setup(&block)
  mutex.synchronize do
    on_setup_callbacks << block
    block.call if @setup
  end
end

#on_unload(cpath = :ANY, &block) ⇒ Object

Configure a block to be invoked right before a certain constant is removed. Supports multiple callbacks, and if there are many, they are executed in the order in which they were defined.

loader.on_unload("Country") do |klass, _abspath|
  klass.clear_cache
end

Can also be configured for any removed constant:

loader.on_unload do |cpath, value, abspath|
  # ...
end

Raises:

  • (TypeError)


259
260
261
262
263
264
265
# File 'lib/im/loader/config.rb', line 259

def on_unload(cpath = :ANY, &block)
  raise TypeError, "on_unload only accepts strings" unless cpath.is_a?(String) || cpath == :ANY

  mutex.synchronize do
    (on_unload_callbacks[cpath] ||= []) << block
  end
end

#push_dir(path) ⇒ Object

Pushes ‘path` to the list of root directories.

Raises ‘Im::Error` if `path` does not exist, or if another loader in the same process already manages that directory or one of its ascendants or descendants.

Raises:



109
110
111
112
113
114
115
116
117
# File 'lib/im/loader/config.rb', line 109

def push_dir(path)
  abspath = File.expand_path(path)
  if dir?(abspath)
    raise_if_conflicting_directory(abspath)
    root_dirs << abspath
  else
    raise Im::Error, "the root directory #{abspath} does not exist"
  end
end

#reloading_enabled?Boolean

Returns:

  • (Boolean)


168
169
170
# File 'lib/im/loader/config.rb', line 168

def reloading_enabled?
  @reloading_enabled
end

#tagObject

Returns the loader’s tag.

Implemented as a method instead of via attr_reader for symmetry with the writer below.



125
126
127
# File 'lib/im/loader/config.rb', line 125

def tag
  @tag
end

#tag=(tag) ⇒ Object

Sets a tag for the loader, useful for logging.



132
133
134
# File 'lib/im/loader/config.rb', line 132

def tag=(tag)
  @tag = tag.to_s
end