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
-
.included(base) ⇒ Object
Includes ClassMethods into the class.
-
.is_loading?(path) ⇒ Boolean
Determines whether objects are being loaded from a specific path.
-
.is_pending? ⇒ Boolean
Determines whether there are pending objects.
-
.load_blocks(path) {|pending_object| ... } ⇒ PendingObject
Loads all object blocks from a specific path.
-
.load_objects(path) ⇒ Array
Loads all objects from a specific path.
-
.loading(path) ⇒ PendingObject
Finds the first pending object being loaded from a specific path.
-
.pending ⇒ PendingObject
The first pending object waiting to be fully loaded.
-
.queue ⇒ Array<PendingObject>
The pending objects waiting to be fully loaded.
Class Method Details
.included(base) ⇒ Object
Includes ClassMethods into the class.
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.
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.
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.
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.(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.
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.
63 64 65 |
# File 'lib/object_loader/object_loader.rb', line 63 def ObjectLoader.loading(path) queue.find { |pending| pending.path == path } end |
.pending ⇒ PendingObject
The first pending object waiting to be fully loaded.
36 37 38 |
# File 'lib/object_loader/object_loader.rb', line 36 def ObjectLoader.pending queue.first end |
.queue ⇒ Array<PendingObject>
The pending objects waiting to be fully loaded.
24 25 26 |
# File 'lib/object_loader/object_loader.rb', line 24 def ObjectLoader.queue @@queue ||= [] end |