Sha256: f3081bcd790a0b5c549c7c11d3093a5ec45196058c9b6f6d914d5d594f3fcbb6

Contents?: true

Size: 1.14 KB

Versions: 2

Compression:

Stored size: 1.14 KB

Contents

module Arrest

  ##
  # Classloader that specifies the deferred class loading strategy for the user. It is registered as default class loader
  # in the Arrest::Source.
  # The default implementation tries to load the class specified by the given symbol parameter in the following order:
  # 1) from the given module utilising Arrest (in our case SGDB)
  # 2) from Arrest itself (e.g. :Ref)
  # 3) from the Kernel (for all basic types - String, Integer etc)
  class DefaultClassLoader

    def load(sym)
      # Using const_get is effectively a hack - it uses the fact that class names are also constants to allow you to get hold of them.
      # Better use eval if possible

      clazz =
        begin
          eval("#{Source.mod.to_s}::#{sym}") unless Source.mod == Kernel
        rescue NameError
        end


      clazz ||=
        begin
          eval("Arrest::#{sym}")
        rescue NameError
        end

      clazz ||=
        begin
          Kernel.const_get(sym)
        rescue NameError
        end

      raise "Class #{sym} could not be loaded! Tried module if given, with fallback Arrest and Kernel" unless clazz
      clazz
    end
  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
arrest-0.0.91 lib/arrest/default_class_loader.rb
arrest-0.0.90 lib/arrest/default_class_loader.rb