Module: Contextify
- Defined in:
- lib/contextify/version.rb,
lib/contextify/contextify.rb,
lib/contextify/class_methods.rb,
lib/contextify/pending_context.rb,
lib/contextify/contextified_class_methods.rb,
lib/contextify/exceptions/unknown_context.rb,
lib/contextify/exceptions/context_not_found.rb
Defined Under Namespace
Modules: ClassMethods, ContextifiedClassMethods Classes: ContextNotFound, PendingContext, UnknownContext
Constant Summary collapse
- VERSION =
contextify version
'0.2.0'
Class Method Summary collapse
-
.contexts ⇒ Hash{Symbol => Class}
All contextified classes.
- .included(base) ⇒ Object
-
.is_context?(name) ⇒ Boolean
Determines whether a context with a specific name has been defined.
-
.is_loading?(path) ⇒ Boolean
Determines whether contexts are being loaded from a specific path.
-
.is_pending? ⇒ Boolean
Determines whether there are pending contexts.
-
.load_block(name, path) {|block| ... } ⇒ Proc
Loads the context block for the context with the specific name from a specific path.
-
.load_blocks(path, &block) ⇒ PendingContext
Loads all context blocks from a specific path.
-
.load_context(name, path, *arguments) ⇒ Object
Loads the context object of a specific name and from the specific path.
-
.load_contexts(path, &block) ⇒ Array
Loads all context objects from a specific path.
-
.loading(path) ⇒ PendingContext
Finds the first pending context being loaded from a specific path.
-
.pending ⇒ PendingContext
The first context waiting to be fully loaded.
-
.waiting ⇒ Array<PendingContext>
The contexts waiting to be fully loaded.
Class Method Details
.contexts ⇒ Hash{Symbol => Class}
All contextified classes.
16 17 18 |
# File 'lib/contextify/contextify.rb', line 16 def Contextify.contexts @@contextify_contexts ||= {} end |
.included(base) ⇒ Object
6 7 8 |
# File 'lib/contextify/contextify.rb', line 6 def self.included(base) base.extend ClassMethods end |
.is_context?(name) ⇒ Boolean
Determines whether a context with a specific name has been defined.
30 31 32 |
# File 'lib/contextify/contextify.rb', line 30 def Contextify.is_context?(name) Contextify.contexts.has_key?(name.to_s) end |
.is_loading?(path) ⇒ Boolean
Determines whether contexts are being loaded from a specific path.
93 94 95 |
# File 'lib/contextify/contextify.rb', line 93 def Contextify.is_loading?(path) !(Contextify.loading(path).nil?) end |
.is_pending? ⇒ Boolean
Determines whether there are pending contexts.
60 61 62 |
# File 'lib/contextify/contextify.rb', line 60 def Contextify.is_pending? !(Contextify.waiting.empty?) end |
.load_block(name, path) {|block| ... } ⇒ Proc
Loads the context block for the context with the specific name from a specific path.
162 163 164 165 166 167 |
# File 'lib/contextify/contextify.rb', line 162 def Contextify.load_block(name,path,&block) context_block = Contextify.load_blocks(path).blocks[name.to_s] block.call(context_block) if block return context_block end |
.load_blocks(path, &block) ⇒ PendingContext
Loads all context blocks from a specific path.
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/contextify/contextify.rb', line 106 def Contextify.load_blocks(path,&block) path = File.(path) unless File.file?(path) raise(ContextNotFound,"context #{path.dump} doest not exist",caller) end # prevent circular loading of contexts unless Contextify.is_pending? # push on the new pending context Contextify.waiting.unshift(PendingContext.new(path)) begin load(path) rescue Exception => e # if any error is encountered, pop off the context Contextify.waiting.shift raise(e) end end # pop off and return the pending context pending_context = Contextify.waiting.shift block.call(pending_context) if block return pending_context end |
.load_context(name, path, *arguments) ⇒ Object
Loads the context object of a specific name and from the specific path.
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/contextify/contextify.rb', line 189 def Contextify.load_context(name,path,*arguments) name = name.to_s unless Contextify.is_context?(name) raise(UnknownContext,"unknown context #{name.dump}",caller) end new_context = Contextify.contexts[name].new(*arguments) Contextify.load_block(name,path) do |context_block| new_context.instance_eval(&context_block) if context_block end return new_context end |
.load_contexts(path, &block) ⇒ Array
Loads all context objects from a specific path.
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/contextify/contextify.rb', line 218 def Contextify.load_contexts(path,&block) new_objs = [] Contextify.load_blocks(path) do |pending| pending.each do |name,context_block| if Contextify.is_context?(name) new_obj = Contextify.contexts[name].new new_obj.instance_eval(&context_block) block.call(new_obj) if block new_objs << new_obj end end end return new_objs end |
.loading(path) ⇒ PendingContext
Finds the first pending context being loaded from a specific path.
73 74 75 76 77 78 79 80 81 |
# File 'lib/contextify/contextify.rb', line 73 def Contextify.loading(path) Contextify.waiting.each do |pending| if pending.path == path return pending end end return nil end |
.pending ⇒ PendingContext
The first context waiting to be fully loaded.
50 51 52 |
# File 'lib/contextify/contextify.rb', line 50 def Contextify.pending Contextify.waiting.first end |
.waiting ⇒ Array<PendingContext>
The contexts waiting to be fully loaded.
40 41 42 |
# File 'lib/contextify/contextify.rb', line 40 def Contextify.waiting @@contextify_waiting_contexts ||= [] end |