Sha256: 2f10bd59083016a08321de3375b258d5631a38246910c5d92ecb7ca89a65da56

Contents?: true

Size: 1.76 KB

Versions: 1

Compression:

Stored size: 1.76 KB

Contents

module Sourcerer

  module MethodDSL


    # creatue a raw eval-able process source, so you can set
    # the right bindings using the .to_proc call from String methods
    def source

      # defaults
      begin
        return_string= Sourcerer::ProcSource.new
        block= 0
      end

      unless Sourcerer::ProcSource.source_cache[self.inspect].nil?
        return Sourcerer::ProcSource.source_cache[self.inspect]
      else

        if self.source_location.nil?
          return nil
        end

        File.open(File.expand_path(self.source_location[0])
        ).each_line_from self.source_location[1] do |line|
          block += line.source_formater_for_line_sub
          return_string.concat(line)
          break if block == 0
        end

        begin

          if return_string.split("\n")[0].include? '('
            return_string.sub!('(',' ')
            return_string.sub!(')',' ')
          end

          args_to_replace = return_string.split("\n")[0].match(/\s*def\s*\S*\s*(.*)/).captures[0]

          if args_to_replace != String.new
            return_string.sub!(args_to_replace,"|#{args_to_replace}|")
          end

        rescue TypeError,NoMethodError
          return nil
        end

        return_string.sub!(/\s*\bdef\s*[\w\S]*/,'Proc.new{')
        return_string.sub!(/}[^}]*$/,"}")

        if !return_string.include?('Proc.new')
          return_string.sub!(/^[^{]*(?!={)/,'Proc.new')
        end

        if !self.source_location.nil?
          Sourcerer::ProcSource.source_cache[self.inspect]= return_string
        end

        return return_string
      end
    end

    alias :source_string :source
    alias :proc_source   :source

  end

end


class Method
  include Sourcerer::MethodDSL
end

class UnboundMethod
  include Sourcerer::MethodDSL
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sourcerer-0.5.0.pre.beta lib/sourcerer/method.rb