lib/ronin/context.rb in ronin-0.1.1 vs lib/ronin/context.rb in ronin-0.1.2

- old
+ new

@@ -27,44 +27,48 @@ require 'ronin/extensions/meta' module Ronin module Context def self.included(base) - base.metaclass_def(:contextify) do |name| - Context.contexts[name.to_sym] = self + base.module_eval do + def self.contextify(name) + name = name.to_sym - meta_def(:context_name) { name } + Context.contexts[name] = self - class_def(:context_name) { name } + meta_def(:context_name) { name } - meta_def(:load_context) do |path,*args| - Context.load_context(self.context_name,path,*args) - end + class_def(:context_name) { name } - # define the top-level context wrappers - Kernel.module_eval %{ - def ronin_#{name}(*args,&block) - if (args.empty? && Ronin::Context.is_pending?) - Ronin::Context.pending.blocks[:#{name}] = block - return nil - else - new_context = #{self}.new(*args) - new_context.instance_eval(&block) if block - return new_context - end + meta_def(:load_context) do |path,*args| + Context.load_context(self.context_name,path,*args) end - } - # define the Ronin-level context loader - Ronin.module_eval %{ - def ronin_load_#{name}(path,*args,&block) - new_context = #{self}.load_context(path,*args) + # define the top-level context wrappers + Kernel.module_eval %{ + def ronin_#{name}(*args,&block) + if (args.empty? && Ronin::Context.is_pending?) + Ronin::Context.pending.blocks[:#{name}] = block + return nil + else + new_context = #{self}.new(*args) + new_context.instance_eval(&block) if block + return new_context + end + end + } - block.call(new_context) if block - return new_context - end - } + # define the Ronin-level context loader + Ronin.module_eval %{ + def ronin_load_#{name}(path,*args,&block) + new_context = #{self}.load_context(path,*args) + + block.call(new_context) if block + return new_context + end + } + end end end # # Returns a Hash of all defined contexts. @@ -88,11 +92,10 @@ # Context.namify(Ronin::Resources) # => "ronin_resources" # # Context.namify(Analysis::Audio) # => "ronin_analysis_audio" # def Context.namify(base) - # similar to the way Og tableizes Class names base.to_s.downcase.gsub(/::/,'_').gsub(/^ronin_/,'').to_sym end # # Returns the Array of contexts which are waiting to be loaded. @@ -219,15 +222,15 @@ pending.each_block do |name,context_block| if Context.is_context?(name) new_obj = Context.contexts[name].new new_obj.instance_eval(&context_block) + block.call(new_obj) if block new_objs << new_obj end end end - new_objs.each(&block) if block return new_objs end end end