Module: WindooZeitwerkConfig

Defined in:
lib/windoo/zeitwerk_config.rb

Overview

Windoo’s Zeitwerk Config and processes

Constant Summary collapse

VERBOSE_LOADING_FILE =

touch this file to make zeitwerk and mixins send text to stderr as things load or get mixed in

Pathname.new('/tmp/windoo-verbose-loading')
VERBOSE_LOADING_ENV =

Or, set this ENV var to also make zeitverk and mixins send text to stderr

'WINDOO_VERBOSE_LOADING'
EAGER_LOAD_FILE =

touch this file to make zeitwek eager-load everything when the gem is required.

Pathname.new('/tmp/windoo-zeitwerk-eager-load')

Class Method Summary collapse

Class Method Details

.eager_load_for_testingObject

For testing the Zeitwrk Loader. Normally we want autoloading on demand, eager loading loads everything so we can see it

To make this happen touch the file defined in ZEITWERK_EAGER_LOAD_FILE in jamf.rb



149
150
151
152
153
154
155
156
# File 'lib/windoo/zeitwerk_config.rb', line 149

def self.eager_load_for_testing
  return unless EAGER_LOAD_FILE.file?

  loader.eager_load(force: true)
  warn :loaded
  # rescue Zeitwerk::NameError => e
  #   warn e.message
end

.load_msg(msg) ⇒ Object

rubocop: disable Style/StderrPuts



36
37
38
# File 'lib/windoo/zeitwerk_config.rb', line 36

def self.load_msg(msg)
  $stderr.puts msg if verbose_loading?
end

.loaderObject

The loader object for Windoo



42
43
44
# File 'lib/windoo/zeitwerk_config.rb', line 42

def self.loader
  @loader
end

.setup_zeitwerk_loader(zloader) ⇒ Object

Configure the Zeitwerk loader, See github.com/fxn/zeitwerk This all has to happen before the first ‘module Windoo’ declaration



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/windoo/zeitwerk_config.rb', line 48

def self.setup_zeitwerk_loader(zloader)
  @loader = zloader

  ##### Ingored Paths
  ################################
  #
  # These should be ignored, some will be required directly
  ###############################################

  # Ignore this file
  ####
  loader.ignore __FILE__

  # ignore files at the top level
  ####
  Pathname.new(__dir__).parent
  # loader.ignore "#{top}/some_top_level_file.rb"

  # ignore things that are manually loaded by our code
  ####
  loader.ignore "#{__dir__}/exceptions.rb"

  ##### Collaped Paths
  ################################
  #
  # these paths all define classes & modules directly below 'Jamf'
  # If we didn't collapse them, then e.g.
  #   /jamf/api/base_classes/classic/group.rb
  # would be expected to define
  #   Jamf::Api::BaseClasses::Classic::Group
  # rather than what we want:
  #  Jamf::Group
  ###################################################

  loader.collapse("#{__dir__}/objects")

  ##### Inflected Paths
  ################################
  #
  # filenames => Constants, which don't adhere to zeitwerk's parsing standards.
  #
  # Mostly because the a filename like 'oapi_object' would be
  # loaded by zeitwerk expecting it to define 'OapiObject', but it really
  # defines 'OAPIObject'
  ###############################################

  loader.inflector.inflect 'json_object' => 'JSONObject'
  loader.inflector.inflect 'api_collection' => 'APICollection'

  ##### Callbacks
  ################################

  # callback for when a specific file/constant loads
  # duplicate and uncomment this if desired to react to
  # specific things loading
  #####################################
  # loader.on_load('Windoo::SomeClass') do |klass, abspath|
  #   load_msg "I just loaded #{klass} from #{abspath}"
  # end

  # callback for when anything loads
  #  - const_path is like "Windoo::SomeClass" or "Windoo::SomeClass::SOME_CONST_ARRY"
  #  - value is the value that constant contains after loading,
  #    e.g. the class Windoo::SomeClass for 'Windoo::SomeClass' or
  #    an Array for the constant  "Windoo::SomeClass::SOME_CONST_ARRY"
  #  - abspath is the full path to the file where the constant was loaded from.
  #####################################
  loader.on_load do |const_path, value, abspath|
    load_msg "Zeitwerk just loaded #{value.class} '#{const_path}' from:\n  #{abspath}"

    # Parse JSON_ATTRIBUTES into getters and setters for subclasses of
    # JSONObject
    #
    # The class we just loaded must have this method and constant
    # and the method must not have run already for the class or any superclass.
    # This prevents running parse_oapi_properties again in subclasses that
    # don't need to do that
    if defined?(value::JSON_ATTRIBUTES)
      parsed = value.parse_json_attributes
      load_msg "Parsed JSON_ATTRIBUTES for #{value}" if parsed
    end

    # Generate the identifier list methods (.all_*) for subclasses of APIObject
    # in the Classic API
    # if value.is_a?(Class) && value.superclass == Jamf::APIObject

    #   done = value.define_identifier_list_methods
    #   load_msg "Defined identifier list methods for #{value}" if done
    # end
  end

  # actually do the setup that was defined above
  loader.setup
end

.verbose_loading?Boolean

Only look at the filesystem once.

Returns:

  • (Boolean)


27
28
29
30
31
32
33
# File 'lib/windoo/zeitwerk_config.rb', line 27

def self.verbose_loading?
  return @verbose_loading unless @verbose_loading.nil?

  @verbose_loading = VERBOSE_LOADING_FILE.file?
  @verbose_loading ||= ENV.include? VERBOSE_LOADING_ENV
  @verbose_loading
end