module ObjectLoader

Constants

VERSION

object_loader version

Public Class Methods

included(base) click to toggle source

Includes {ClassMethods} into the class.

@param [Class] base

The class that {ObjectLoader} is being included into.
# File lib/object_loader/object_loader.rb, line 12
def self.included(base)
  base.extend ClassMethods
end
is_loading?(path) click to toggle source

Determines whether objects are being loaded from a specific path.

@param [String] path

The path to check if objects are being loaded from.

@return [Boolean]

Specifies whether pending objects are being loaded from the
specified path.

@since 1.0.0

# File lib/object_loader/object_loader.rb, line 79
def ObjectLoader.is_loading?(path)
  !(loading(path).nil?)
end
is_pending?() click to toggle source

Determines whether there are pending objects.

@return [Boolean]

Specifies whether there is a pending object present.

@since 1.0.0

# File lib/object_loader/object_loader.rb, line 48
def ObjectLoader.is_pending?
  !(queue.empty?)
end
load_blocks(path) { |pending_object| ... } click to toggle source

Loads all object blocks from a specific path.

@param [String] path

The path to load all object blocks from.

@return [PendingObject]

The pending object which contains the blocks.

@since 1.0.0

# File lib/object_loader/object_loader.rb, line 94
def ObjectLoader.load_blocks(path)
  path = File.expand_path(path)

  unless File.file?(path)
    raise(ObjectNotFound,"#{path.dump} doest not exist",caller)
  end

  # prevent circular loading of objects
  unless is_pending?
    # push on the new pending object
    queue.unshift(PendingObject.new(path))

    begin
      load(path)
    rescue Exception => e
      # if any error is encountered, pop off the object
      queue.shift
      raise(e)
    end
  end

  # pop off and return the pending object
  pending_object = queue.shift

  yield pending_object if block_given?
  return pending_object
end
load_objects(path) { |new_object| ... } click to toggle source

Loads all objects from a specific path.

@param [String] path

The path to load all objects from.

@return [Array]

The array of loaded objects.

@example

ObjectLoader.load_objects('/path/to/misc_object.rb')
# => [...]

@since 1.0.0

# File lib/object_loader/object_loader.rb, line 137
def ObjectLoader.load_objects(path)
  new_objects = []

  load_blocks(path) do |pending|
    pending.each do |klass,block|
      new_object = klass.new
      new_object.instance_eval(&block)

      yield new_object if block_given?
      new_objects << new_object
    end
  end

  return new_objects
end
loading(path) click to toggle source

Finds the first pending object being loaded from a specific path.

@param [String] path

The path which is being loaded.

@return [PendingObject]

The first pending object with the specified path.

@since 1.0.0

# File lib/object_loader/object_loader.rb, line 63
def ObjectLoader.loading(path)
  queue.find { |pending| pending.path == path }
end
pending() click to toggle source

The first pending object waiting to be fully loaded.

@return [PendingObject]

The pending object being loaded.

@since 1.0.0

# File lib/object_loader/object_loader.rb, line 36
def ObjectLoader.pending
  queue.first
end
queue() click to toggle source

The pending objects waiting to be fully loaded.

@return [Array<PendingObject>]

Contexts which are waiting to be loaded.

@since 1.0.0

# File lib/object_loader/object_loader.rb, line 24
def ObjectLoader.queue
  @@queue ||= []
end