Module: JSONAPIonify::Autoload
- Included in:
- JSONAPIonify, Api, JSONAPIonify::Api::Action, JSONAPIonify::Api::Actions, JSONAPIonify::Api::Attribute, JSONAPIonify::Api::Base, JSONAPIonify::Api::Relationship, JSONAPIonify::Api::Resource, JSONAPIonify::Api::Resource::Builders, JSONAPIonify::Api::Resource::Defaults, JSONAPIonify::Api::Resource::Definitions, JSONAPIonify::Api::Server, Structure::Collections, Structure::Helpers, Structure::Maps, Structure::Objects, Types
- Defined in:
- lib/jsonapionify/autoload.rb
Class Method Summary collapse
Instance Method Summary collapse
Class Method Details
.eager_load! ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/jsonapionify/autoload.rb', line 5 def self.eager_load! load_tracker = {} while unloaded.present? unloaded.each do |mod, consts| consts.each do |const| tracker = [mod.name, const].join('::') begin mod.const_get(const, false) rescue NameError => e load_tracker[tracker] = load_tracker[tracker].to_i + 1 if !e..include?('uninitialized constant') || load_tracker[tracker] > 50 raise e end end end end end end |
.unloaded ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/jsonapionify/autoload.rb', line 24 def self.unloaded modules = ObjectSpace.each_object.select do |o| o.is_a?(Module) end modules.each_with_object({}) do |mod, hash| autoloadable_constants = mod.constants.each_with_object([]) do |const, ary| if mod.autoload?(const) && mod.autoload?(const).include?(__dir__) ary << const end end if autoloadable_constants.present? hash[mod] = autoloadable_constants end end end |
Instance Method Details
#autoload_all(dir = nil) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/jsonapionify/autoload.rb', line 40 def autoload_all(dir=nil) file = caller[0].split(/\:\d/)[0] base_dir = File. File.dirname(file) dir ||= name.split('::').last.underscore Dir.glob("#{base_dir}/#{dir}/*.rb").each do |file| basename = File.basename file, File.extname(file) fullpath = File. file const_name = basename.camelize.to_sym autoload const_name, fullpath end end |