method_introspection 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
    
        checksums.yaml
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            ---
         | 
| 2 2 | 
             
            SHA1:
         | 
| 3 | 
            -
              metadata.gz:  | 
| 4 | 
            -
              data.tar.gz:  | 
| 3 | 
            +
              metadata.gz: f8e37158912e43ca147a709371c78620db8933fb
         | 
| 4 | 
            +
              data.tar.gz: f1e14b2af4e5a389b52020a0bd0b64f6da5e021e
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 6 | 
            +
              metadata.gz: 3ba4cb7223cf9442f7ec90a11017c8b0119ee8109b27f047b505beb2e73ec358c4fd66816c249d6b3cb8c7eb6f16a81a196ec684714768ed08bbc35812ab70b4
         | 
| 7 | 
            +
              data.tar.gz: 6e3052cc8ea47e1a41a9596593062119145d35b49450d74e66354c435664d624ff97db9cc8d5145d31c333e8af38b1845cdc7e03d955b7915282f5cc1dde3ba5
         | 
| @@ -22,7 +22,8 @@ module CodeHelpers | |
| 22 22 | 
             
              # @option options [Fixnum] :consume  A number of lines to automatically
         | 
| 23 23 | 
             
              #   consume (add to the expression buffer) without checking for validity.
         | 
| 24 24 | 
             
              # @return [String]  The first complete expression
         | 
| 25 | 
            -
              # | 
| 25 | 
            +
              #
         | 
| 26 | 
            +
              # @raise [SyntaxError] If the first complete expression can't be identified
         | 
| 26 27 | 
             
              # ========================================================================= #
         | 
| 27 28 | 
             
              def expression_at(
         | 
| 28 29 | 
             
                  file,
         | 
| @@ -45,7 +46,6 @@ module CodeHelpers | |
| 45 46 | 
             
                extract_first_expression(relevant_lines, options[:consume])
         | 
| 46 47 | 
             
                rescue SyntaxError => e
         | 
| 47 48 | 
             
                raise if options[:strict]
         | 
| 48 | 
            -
             | 
| 49 49 | 
             
                begin
         | 
| 50 50 | 
             
                  extract_first_expression(relevant_lines) { |code|
         | 
| 51 51 | 
             
                    code.gsub(/\#\{.*?\}/, "temp")
         | 
| @@ -58,10 +58,11 @@ module CodeHelpers | |
| 58 58 | 
             
              # ========================================================================= #
         | 
| 59 59 | 
             
              # === comment_describing
         | 
| 60 60 | 
             
              #
         | 
| 61 | 
            -
              #  | 
| 62 | 
            -
              # the given file.
         | 
| 61 | 
            +
              # This method will retrieve the comment describing the expression on the
         | 
| 62 | 
            +
              # given line of the given file.
         | 
| 63 63 | 
             
              #
         | 
| 64 | 
            -
              # This is useful to get module or method documentation | 
| 64 | 
            +
              # This is useful to get the module or method documentation, in String
         | 
| 65 | 
            +
              # format.
         | 
| 65 66 | 
             
              #
         | 
| 66 67 | 
             
              # @param [Array<String>, File, String] file  The file to parse, either as a File or as
         | 
| 67 68 | 
             
              #                                            a String or an Array of lines.
         | 
| @@ -70,7 +71,11 @@ module CodeHelpers | |
| 70 71 | 
             
              # @return [String]  The comment
         | 
| 71 72 | 
             
              # ========================================================================= #
         | 
| 72 73 | 
             
              def comment_describing(file, line_number)
         | 
| 73 | 
            -
                 | 
| 74 | 
            +
                if file.is_a? Array
         | 
| 75 | 
            +
                  lines = file
         | 
| 76 | 
            +
                else
         | 
| 77 | 
            +
                  lines = file.each_line.to_a
         | 
| 78 | 
            +
                end
         | 
| 74 79 | 
             
                extract_last_comment(lines[0..(line_number - 2)])
         | 
| 75 80 | 
             
              end
         | 
| 76 81 |  | 
| @@ -102,7 +107,6 @@ module CodeHelpers | |
| 102 107 | 
             
              end
         | 
| 103 108 |  | 
| 104 109 | 
             
              private
         | 
| 105 | 
            -
             | 
| 106 110 | 
             
              # ========================================================================= #
         | 
| 107 111 | 
             
              # === extract_first_expression
         | 
| 108 112 | 
             
              #
         | 
| @@ -127,7 +131,7 @@ module CodeHelpers | |
| 127 131 | 
             
              # ========================================================================= #
         | 
| 128 132 | 
             
              # === extract_last_comment
         | 
| 129 133 | 
             
              #
         | 
| 130 | 
            -
              # Get the last comment from the input.
         | 
| 134 | 
            +
              # Get the last comment from the input. We will build up the result.
         | 
| 131 135 | 
             
              #
         | 
| 132 136 | 
             
              # @param [Array<String>]  lines
         | 
| 133 137 | 
             
              # @return [String]
         | 
| @@ -162,10 +166,12 @@ module CodeHelpers | |
| 162 166 | 
             
                  /can not find the string ".*" anywhere before EOF/, # rbx and jruby
         | 
| 163 167 | 
             
                  /missing 'end' for/, /expecting kWHEN/ # rbx
         | 
| 164 168 | 
             
                ]
         | 
| 165 | 
            -
             | 
| 166 | 
            -
                 | 
| 167 | 
            -
             | 
| 168 | 
            -
             | 
| 169 | 
            +
                # ======================================================================= #
         | 
| 170 | 
            +
                # === IncompleteExpression.===
         | 
| 171 | 
            +
                # ======================================================================= #
         | 
| 172 | 
            +
                def self.===(exception)
         | 
| 173 | 
            +
                  return false unless SyntaxError === exception
         | 
| 174 | 
            +
                  case exception.message
         | 
| 169 175 | 
             
                  when *GENERIC_REGEXPS
         | 
| 170 176 | 
             
                    true
         | 
| 171 177 | 
             
                  else
         | 
| @@ -1,4 +1,37 @@ | |
| 1 | 
            -
             | 
| 1 | 
            +
            require 'pp'
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            module MethodIntrospection # The top namespace of this project.
         | 
| 4 | 
            +
             | 
| 5 | 
            +
              @this_file   = nil
         | 
| 6 | 
            +
              @line_number = nil
         | 
| 7 | 
            +
             | 
| 8 | 
            +
              # ========================================================================= #
         | 
| 9 | 
            +
              # === MethodIntrospection.set_this_file
         | 
| 10 | 
            +
              # ========================================================================= #
         | 
| 11 | 
            +
              def self.set_this_file(i) # setter
         | 
| 12 | 
            +
                @this_file = i
         | 
| 13 | 
            +
              end
         | 
| 14 | 
            +
             | 
| 15 | 
            +
              # ========================================================================= #
         | 
| 16 | 
            +
              # === MethodIntrospection.this_file?
         | 
| 17 | 
            +
              # ========================================================================= #
         | 
| 18 | 
            +
              def self.this_file?
         | 
| 19 | 
            +
                @this_file
         | 
| 20 | 
            +
              end; self.instance_eval { alias file? this_file? } # === MethodIntrospection.file?
         | 
| 21 | 
            +
             | 
| 22 | 
            +
              # ========================================================================= #
         | 
| 23 | 
            +
              # === MethodIntrospection.set_line_number
         | 
| 24 | 
            +
              # ========================================================================= #
         | 
| 25 | 
            +
              def self.set_line_number(i) # setter
         | 
| 26 | 
            +
                @line_number = i
         | 
| 27 | 
            +
              end
         | 
| 28 | 
            +
             | 
| 29 | 
            +
              # ========================================================================= #
         | 
| 30 | 
            +
              # === MethodIntrospection.line_number?
         | 
| 31 | 
            +
              # ========================================================================= #
         | 
| 32 | 
            +
              def self.line_number?
         | 
| 33 | 
            +
                @line_number
         | 
| 34 | 
            +
              end
         | 
| 2 35 |  | 
| 3 36 | 
             
              # ========================================================================= #
         | 
| 4 37 | 
             
              # === SourceNotFoundError
         | 
| @@ -28,7 +61,8 @@ module MethodIntrospection | |
| 28 61 | 
             
              #
         | 
| 29 62 | 
             
              # Helper method responsible for extracting the body of a method.
         | 
| 30 63 | 
             
              #
         | 
| 31 | 
            -
              #  | 
| 64 | 
            +
              # This is defined here to avoid polluting the `Method` class.
         | 
| 65 | 
            +
              #
         | 
| 32 66 | 
             
              # @param [Array] source_location The array returned by Method#source_location
         | 
| 33 67 | 
             
              # @param [String]  method_name
         | 
| 34 68 | 
             
              # @return [String] The method body
         | 
| @@ -42,22 +76,6 @@ module MethodIntrospection | |
| 42 76 | 
             
                  "Could not parse source for #{name}: #{error.message}"
         | 
| 43 77 | 
             
              end
         | 
| 44 78 |  | 
| 45 | 
            -
              # ========================================================================= #
         | 
| 46 | 
            -
              # === MethodIntrospection.comment_helper
         | 
| 47 | 
            -
              #
         | 
| 48 | 
            -
              # Helper method responsible for opening source file and buffering up
         | 
| 49 | 
            -
              # the comments for a specified method. Defined here to avoid polluting
         | 
| 50 | 
            -
              # `Method` class.
         | 
| 51 | 
            -
              # @param [Array] source_location The array returned by Method#source_location
         | 
| 52 | 
            -
              # @param [String]  method_name
         | 
| 53 | 
            -
              # @return [String] The comments up to the point of the method.
         | 
| 54 | 
            -
              # ========================================================================= #
         | 
| 55 | 
            -
              def self.comment_helper(source_location, name = nil)
         | 
| 56 | 
            -
                raise_this(name) unless source_location
         | 
| 57 | 
            -
                file, line = *source_location
         | 
| 58 | 
            -
                comment_describing(lines_for(file), line)
         | 
| 59 | 
            -
              end
         | 
| 60 | 
            -
             | 
| 61 79 | 
             
              # ========================================================================= #
         | 
| 62 80 | 
             
              # === MethodIntrospection.lines_for
         | 
| 63 81 | 
             
              #
         | 
| @@ -75,4 +93,34 @@ module MethodIntrospection | |
| 75 93 | 
             
                raise_this(name, e.message)
         | 
| 76 94 | 
             
              end
         | 
| 77 95 |  | 
| 96 | 
            +
              # ========================================================================= #
         | 
| 97 | 
            +
              # === MethodIntrospection.comment_helper
         | 
| 98 | 
            +
              #
         | 
| 99 | 
            +
              # Helper method responsible for opening source file and buffering up
         | 
| 100 | 
            +
              # the comments for a specified method. Defined here to avoid 
         | 
| 101 | 
            +
              # polluting the `Method` class.
         | 
| 102 | 
            +
              #
         | 
| 103 | 
            +
              # The expected input should be an Array, which is returned by the
         | 
| 104 | 
            +
              # method Method#source_location.
         | 
| 105 | 
            +
              #
         | 
| 106 | 
            +
              # This will typically be in the form of:
         | 
| 107 | 
            +
              #
         | 
| 108 | 
            +
              #  ["/Programs/Ruby/2.2.3/lib/ruby/site_ruby/2.2.0/config.rb", 297]
         | 
| 109 | 
            +
              #
         | 
| 110 | 
            +
              # The first one yields to us the name, the second the line number.
         | 
| 111 | 
            +
              #
         | 
| 112 | 
            +
              # @param [String]  method_name
         | 
| 113 | 
            +
              # @return [String] The comments up to the point of the method.
         | 
| 114 | 
            +
              # ========================================================================= #
         | 
| 115 | 
            +
              def self.comment_helper(source_location, name = nil)
         | 
| 116 | 
            +
                raise_this(name) unless source_location
         | 
| 117 | 
            +
                this_file, line_number = *source_location
         | 
| 118 | 
            +
            pp source_location
         | 
| 119 | 
            +
            pp this_file
         | 
| 120 | 
            +
            pp line_number
         | 
| 121 | 
            +
                MethodIntrospection.set_this_file(this_file)
         | 
| 122 | 
            +
                MethodIntrospection.set_line_number(line_number)
         | 
| 123 | 
            +
                comment_describing(lines_for(this_file), line_number)
         | 
| 124 | 
            +
              end
         | 
| 125 | 
            +
             | 
| 78 126 | 
             
            end
         | 
| @@ -13,12 +13,16 @@ module MethodIntrospection | |
| 13 13 | 
             
                end
         | 
| 14 14 |  | 
| 15 15 | 
             
                module ProcExtensions
         | 
| 16 | 
            +
                  # ===================================================================== #
         | 
| 17 | 
            +
                  # === source_location
         | 
| 18 | 
            +
                  #
         | 
| 16 19 | 
             
                  # Return the source location for a Proc (in implementations
         | 
| 17 20 | 
             
                  # without Proc#source_location)
         | 
| 18 21 | 
             
                  #
         | 
| 19 22 | 
             
                  # @return [Array] A two element array. First element is the
         | 
| 20 23 | 
             
                  #   file, second element is the line in the file where the
         | 
| 21 24 | 
             
                  #   proc definition is found.
         | 
| 25 | 
            +
                  # ===================================================================== #
         | 
| 22 26 | 
             
                  def source_location
         | 
| 23 27 | 
             
                    self.to_s =~ /@(.*):(\d+)/
         | 
| 24 28 | 
             
                    [$1, $2.to_i]
         | 
| @@ -26,21 +30,28 @@ module MethodIntrospection | |
| 26 30 | 
             
                end
         | 
| 27 31 |  | 
| 28 32 | 
             
                module UnboundMethodExtensions
         | 
| 33 | 
            +
                  # ===================================================================== #
         | 
| 34 | 
            +
                  # === source_location
         | 
| 35 | 
            +
                  #
         | 
| 29 36 | 
             
                  # Return the source location of an instance method for Ruby 1.8.
         | 
| 30 37 | 
             
                  #
         | 
| 31 38 | 
             
                  # @return [Array] A two element array. First element is the
         | 
| 32 39 | 
             
                  #   file, second element is the line in the file where the
         | 
| 33 40 | 
             
                  #   method definition is found.
         | 
| 41 | 
            +
                  # ===================================================================== #
         | 
| 34 42 | 
             
                  def source_location
         | 
| 35 | 
            -
                    klass = | 
| 36 | 
            -
             | 
| 37 | 
            -
             | 
| 38 | 
            -
             | 
| 39 | 
            -
             | 
| 40 | 
            -
             | 
| 41 | 
            -
             | 
| 43 | 
            +
                    klass =
         | 
| 44 | 
            +
                    case owner
         | 
| 45 | 
            +
                    when Class
         | 
| 46 | 
            +
                      owner
         | 
| 47 | 
            +
                    when Module
         | 
| 48 | 
            +
                      method_owner = owner
         | 
| 49 | 
            +
                      Class.new { include(method_owner) }
         | 
| 50 | 
            +
                    end
         | 
| 42 51 |  | 
| 43 | 
            -
                    #  | 
| 52 | 
            +
                    # =================================================================== #
         | 
| 53 | 
            +
                    # Deal with immediate values.
         | 
| 54 | 
            +
                    # =================================================================== #
         | 
| 44 55 | 
             
                    case
         | 
| 45 56 | 
             
                    when klass == Symbol
         | 
| 46 57 | 
             
                      return :a.method(name).source_location
         |