class Racket::Utils::Helpers::HelperCache

Cache for helpers, ensuring that helpers get loaded exactly once.

Public Class Methods

new(helper_dir, logger, utils) click to toggle source
# File lib/racket/utils/helpers.rb, line 39
def initialize(helper_dir, logger, utils)
  @helper_dir = helper_dir
  @helpers = {}
  @logger = logger
  @utils = utils
end
service(_options = {}) click to toggle source

Returns a service proc that can be used by the registry.

@param [Hash] _options (unused) @return [Proc]

# File lib/racket/utils/helpers.rb, line 29
def self.service(_options = {})
  lambda do |reg|
    new(
      reg.application_settings.helper_dir,
      reg.application_logger,
      reg.utils
    )
  end
end

Public Instance Methods

load_helpers(helpers) click to toggle source

Loads helper files and return the loadad modules as a hash. Any helper files that cannot be loaded are excluded from the result.

@param [Array] helpers An array of symbols @return [Hash]

# File lib/racket/utils/helpers.rb, line 51
def load_helpers(helpers)
  helper_modules = {}
  helpers.each do |helper|
    helper_module = load_helper(helper)
    helper_modules[helper] = helper_module if helper_module
  end
  helper_modules
end

Private Instance Methods

load_helper(helper) click to toggle source
# File lib/racket/utils/helpers.rb, line 62
def load_helper(helper)
  return @helpers[helper] if @helpers.key?(helper)
  helper_module = load_helper_file(helper)
  @helpers[helper] = helper_module if helper_module
end
load_helper_file(helper) click to toggle source
# File lib/racket/utils/helpers.rb, line 68
def load_helper_file(helper)
  require_helper_file(helper)
  load_helper_module(helper)
end
load_helper_module(helper) click to toggle source

Loads a helper module

@param [Symbol] helper @return [Module]

# File lib/racket/utils/helpers.rb, line 82
def load_helper_module(helper)
  helper_module = nil
  @utils.run_block(NameError) do
    helper_module =
      Racket::Helpers.const_get(helper.to_s.split('_').collect(&:capitalize).join.to_sym)
    @logger.inform_dev("Loaded helper module #{helper.inspect}.")
  end
  helper_module
end
require_helper_file(helper) click to toggle source
# File lib/racket/utils/helpers.rb, line 73
def require_helper_file(helper)
  loaded = @utils.safe_require("racket/helpers/#{helper}")
  @utils.safe_require(@utils.build_path(@helper_dir, helper).to_s) if !loaded && @helper_dir
end