Module: ObjectLoader

Defined in:
lib/object_loader/version.rb,
lib/object_loader/class_methods.rb,
lib/object_loader/object_loader.rb,
lib/object_loader/pending_object.rb,
lib/object_loader/exceptions/object_not_found.rb

Defined Under Namespace

Modules: ClassMethods Classes: ObjectNotFound, PendingObject

Constant Summary collapse

VERSION =

object_loader version

'1.0.1'

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object

Includes ClassMethods into the class.

Parameters:

  • base (Class)

    The class that ObjectLoader is being included into.



12
13
14
# File 'lib/object_loader/object_loader.rb', line 12

def self.included(base)
  base.extend ClassMethods
end

.is_loading?(path) ⇒ Boolean

Determines whether objects are being loaded from a specific path.

Parameters:

  • path (String)

    The path to check if objects are being loaded from.

Returns:

  • (Boolean)

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

Since:

  • 1.0.0



79
80
81
# File 'lib/object_loader/object_loader.rb', line 79

def ObjectLoader.is_loading?(path)
  !(loading(path).nil?)
end

.is_pending?Boolean

Determines whether there are pending objects.

Returns:

  • (Boolean)

    Specifies whether there is a pending object present.

Since:

  • 1.0.0



48
49
50
# File 'lib/object_loader/object_loader.rb', line 48

def ObjectLoader.is_pending?
  !(queue.empty?)
end

.load_blocks(path) {|pending_object| ... } ⇒ PendingObject

Loads all object blocks from a specific path.

Parameters:

  • path (String)

    The path to load all object blocks from.

Yields:

  • (pending_object)

Returns:

  • (PendingObject)

    The pending object which contains the blocks.

Since:

  • 1.0.0



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
# 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) ⇒ Array

Loads all objects from a specific path.

Examples:

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

Parameters:

  • path (String)

    The path to load all objects from.

Returns:

  • (Array)

    The array of loaded objects.

Since:

  • 1.0.0



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# 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) ⇒ PendingObject

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

Parameters:

  • path (String)

    The path which is being loaded.

Returns:

  • (PendingObject)

    The first pending object with the specified path.

Since:

  • 1.0.0



63
64
65
# File 'lib/object_loader/object_loader.rb', line 63

def ObjectLoader.loading(path)
  queue.find { |pending| pending.path == path }
end

.pendingPendingObject

The first pending object waiting to be fully loaded.

Returns:

Since:

  • 1.0.0



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

def ObjectLoader.pending
  queue.first
end

.queueArray<PendingObject>

The pending objects waiting to be fully loaded.

Returns:

  • (Array<PendingObject>)

    Contexts which are waiting to be loaded.

Since:

  • 1.0.0



24
25
26
# File 'lib/object_loader/object_loader.rb', line 24

def ObjectLoader.queue
  @@queue ||= []
end