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

Class Method Details

.contextsHash{Symbol => Class}

All contextified classes.

Returns:

  • (Hash{Symbol => Class})

    All defined contexts and their 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.

Parameters:

  • name (Symbol, String)

    The name of the context to search for.

Returns:

  • (Boolean)

    Specifies whether there is a context defined with the specified name.



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.

Parameters:

  • path (String)

    The path to check if contexts are being loaded from.

Returns:

  • (Boolean)

    Specifies whether pending contexts are being loaded from the specified 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.

Returns:

  • (Boolean)

    Specifies whether there is a pending context present.



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.

Examples:

Contextify.load_block(:exploit,'/path/to/my_exploit.rb')
# => Proc
Contextify.load_block(:shellcode,'/path/to/execve.rb') do |block|
  # ...
end

Parameters:

  • name (Symbol, String)

    The name of the context to load the block for.

  • path (String)

    The path to load the context block from.

Yields:

  • (block)

    The block which will receive the context block.

Yield Parameters:

  • block (Proc)

    The context block loaded from the specified path.

Returns:

  • (Proc)

    The block for the context with the specified name.



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.

Parameters:

  • path (String)

    The path to load all context blocks from.

Returns:



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.expand_path(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.

Examples:

Contextify.load_context(:note,'/path/to/my_notes.rb')
# => Note

Parameters:

  • name (Symbol, String)

    The name of the context to load.

  • path (String)

    The path to load the context object from.

Returns:

  • (Object)

    The loaded context object.

Raises:



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.

Examples:

Contextify.load_contexts('/path/to/misc_contexts.rb')
# => [...]

Parameters:

  • path (String)

    The path to load all context objects from.

Returns:

  • (Array)

    The array of loaded context objects.



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.

Parameters:

  • path (String)

    The path which is being loaded.

Returns:

  • (PendingContext)

    The first pending context with the specified 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

.pendingPendingContext

The first context waiting to be fully loaded.

Returns:



50
51
52
# File 'lib/contextify/contextify.rb', line 50

def Contextify.pending
  Contextify.waiting.first
end

.waitingArray<PendingContext>

The contexts waiting to be fully loaded.

Returns:

  • (Array<PendingContext>)

    Contexts which are waiting to be loaded.



40
41
42
# File 'lib/contextify/contextify.rb', line 40

def Contextify.waiting
  @@contextify_waiting_contexts ||= []
end