Permalink
Browse files

Initial commit.

  • Loading branch information...
0 parents commit 039a41eb0e354ab233b495d16c09427bac7c14d5 @postmodern committed Jun 23, 2011
Showing with 566 additions and 0 deletions.
  1. +3 −0 .document
  2. +2 −0 .gitignore
  3. +1 −0 .rspec
  4. +1 −0 .yardopts
  5. +11 −0 ChangeLog.md
  6. +20 −0 LICENSE.txt
  7. +59 −0 README.md
  8. +40 −0 Rakefile
  9. +16 −0 gemspec.yml
  10. +17 −0 lib/warnings.rb
  11. +22 −0 lib/warnings/mixin.rb
  12. +4 −0 lib/warnings/version.rb
  13. +109 −0 lib/warnings/warning.rb
  14. +60 −0 lib/warnings/warnings.rb
  15. +12 −0 spec/mixin_spec.rb
  16. +5 −0 spec/spec_helper.rb
  17. +41 −0 spec/warning_spec.rb
  18. +16 −0 spec/warnings_spec.rb
  19. +127 −0 warnings.gemspec
@@ -0,0 +1,3 @@
+-
+ChangeLog.*
+LICENSE.txt
@@ -0,0 +1,2 @@
+doc/
+pkg/
1 .rspec
@@ -0,0 +1 @@
+--colour --format documentation
@@ -0,0 +1 @@
+--markup markdown --title "Warnings Documentation" --protected
@@ -0,0 +1,11 @@
+### 0.1.0 / 2011-06-23
+
+* Initial release:
+ * Respects `$VERBOSE` (`ruby -w`) and `$DEBUG` (`ruby -d`)
+ * Search Warning Messages by:
+ * Source File
+ * Source Method
+ * Message
+ * Prints Messages upon exit.
+ * ANSI Coloring.
+
@@ -0,0 +1,20 @@
+Copyright (c) 2011 Hal Brodigan
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,59 @@
+# warnings
+
+* [Source](http://github.com/postmodern/warnings)
+* [Issues](http://github.com/postmodern/warnings/issues)
+* [Documentation](http://rubydoc.info/gems/warnings/frames)
+* [Email](mailto:postmodern.mod3 at gmail.com)
+
+## Description
+
+Warnings provides a searchable and more readable Warning Message
+facility to Ruby. With Warnings, one can queue all warning messages
+generated by your code, search through them and/or print them upon exit.
+
+## Features
+
+* Respects `$VERBOSE` (`ruby -w`) and `$DEBUG` (`ruby -d`)
+* Search Warning Messages by:
+ * Source File
+ * Source Method
+ * Message
+* Prints Messages upon exit.
+* ANSI Coloring.
+
+## Examples
+
+ require 'warnings'
+
+ def danger!
+ warn "Fire in the disco!"
+ end
+
+ danger!
+
+ Warnings.grep(/fire/)
+ # => [...]
+
+ Warnings.from_method('danger!')
+ # => [...]
+
+ Warnings.from_file('foo/bar.rb')
+ # => [...]
+
+ exit
+ #
+ # Warnings:
+ #
+ # fire in the disco! lib/foo/bar.rb:42
+
+## Requirements
+
+## Install
+
+ $ gem install warnings
+
+## Copyright
+
+Copyright (c) 2011 Hal Brodigan
+
+See {file:LICENSE.txt} for details.
@@ -0,0 +1,40 @@
+# encoding: utf-8
+
+require 'rubygems'
+require 'rake'
+
+begin
+ gem 'ore-tasks', '~> 0.4'
+ require 'ore/tasks'
+
+ Ore::Tasks.new
+rescue LoadError => e
+ STDERR.puts e.message
+ STDERR.puts "Run `gem install ore-tasks` to install 'ore/tasks'."
+end
+
+begin
+ gem 'rspec', '~> 2.4'
+ require 'rspec/core/rake_task'
+
+ RSpec::Core::RakeTask.new
+rescue LoadError => e
+ task :spec do
+ abort "Please run `gem install rspec` to install RSpec."
+ end
+end
+
+task :test => :spec
+task :default => :spec
+
+begin
+ gem 'yard', '~> 0.7.0'
+ require 'yard'
+
+ YARD::Rake::YardocTask.new
+rescue LoadError => e
+ task :yard do
+ abort "Please run `gem install yard` to install YARD."
+ end
+end
+task :doc => :yard
@@ -0,0 +1,16 @@
+name: warnings
+summary: Provides a better Warning Message facility to Ruby.
+description:
+ Warnings provides a searchable and more readable Warning Message
+ facility to Ruby. With Warnings, one can queue all warning messages
+ generated by your code, search through them and/or print them upon exit.
+
+license: MIT
+authors: Postmodern
+email: postmodern.mod3@gmail.com
+homepage: http://github.com/postmodern/warnings
+
+development_dependencies:
+ ore-tasks: ~> 0.4
+ rspec: ~> 2.4
+ yard: ~> 0.7.0
@@ -0,0 +1,17 @@
+require 'warnings/version'
+require 'warnings/warnings'
+require 'warnings/mixin'
+
+include Warnings::Mixin
+
+at_exit do
+ if (!$WARNINGS.empty? && ($VERBOSE || $DEBUG))
+ STDERR.puts
+ STDERR.puts "Warnings:"
+ STDERR.puts
+
+ $WARNINGS.each { |warning| STDERR.puts " #{warning.format}" }
+
+ STDERR.puts
+ end
+end
@@ -0,0 +1,22 @@
+require 'warnings/warning'
+require 'warnings/warnings'
+
+module Warnings
+ module Mixin
+ #
+ # Registers a warning.
+ #
+ # @param [String] message
+ # The warning message.
+ #
+ # @example
+ # warn "Foo#bar method will be removed in 1.0.0"
+ #
+ # @return [nil]
+ #
+ def warn(message)
+ $WARNINGS << Warning.new(message,caller)
+ return nil
+ end
+ end
+end
@@ -0,0 +1,4 @@
+module Warnings
+ # warnings version
+ VERSION = "0.1.0"
+end
@@ -0,0 +1,109 @@
+module Warnings
+ class Warning
+
+ # The warning message
+ attr_reader :message
+
+ # The backtrace to the warning
+ #
+ # @return [Array<String>]
+ # The raw backtrace.
+ #
+ attr_reader :backtrace
+
+ # The source location of the warning.
+ #
+ # @return [Array<path, line>]
+ # The path and line number where the warning originated.
+ #
+ attr_reader :source_location
+
+ # The source method of the warning.
+ #
+ # @return [Symbol]
+ # The method name.
+ #
+ attr_reader :source_method
+
+ #
+ # Creates a new warning.
+ #
+ # @param [String] message
+ # The warning message.
+ #
+ # @param [Array<String>] backtrace
+ # The backtrace of the warning.
+ #
+ def initialize(message,backtrace)
+ @message = message
+ @backtrace = backtrace
+
+ file, line, context = @backtrace.first.split(':',3)
+
+ @source_location = [file, line.to_i]
+ @source_method = context[4..-2] if context
+ end
+
+ #
+ # The source file of the warning.
+ #
+ # @return [String]
+ # The path of the file.
+ #
+ def source_file
+ @source_location[0]
+ end
+
+ #
+ # The source line of the warning.
+ #
+ # @return [Integer]
+ # The line-number of the warning.
+ #
+ def source_line
+ @source_location[1]
+ end
+
+ #
+ # Converts the warning to a String.
+ #
+ # @return [String]
+ # The warning message.
+ #
+ def to_s
+ @message.to_s
+ end
+
+ #
+ # Formats a warning.
+ #
+ # @return [String]
+ # The printable warning.
+ #
+ # @note
+ # Will include ANSI color codes, only if `STDOUT` is a TTY Terminal.
+ #
+ def format
+ if STDOUT.tty?
+ "\e[33m#{@message}\e[0m\t\t\e[2m#{source_file}:#{source_line}\e[0m"
+ else
+ "#{@message}\t\t#{source_file}:#{source_line}"
+ end
+ end
+
+ #
+ # Inspects the warning.
+ #
+ # @return [String]
+ # The inspected {#source_file}, {#source_line} and {#message}.
+ #
+ def inspect
+ if @source_method
+ "#{source_file}:#{source_line} (#{@source_method}): #{@message}"
+ else
+ "#{source_file}:#{source_line}: #{@message}"
+ end
+ end
+
+ end
+end
@@ -0,0 +1,60 @@
+module Warnings
+ # The global list of warnings
+ $WARNINGS ||= []
+
+ #
+ # Selects all warnings with a similar message.
+ #
+ # @param [String, Regexp] pattern
+ # The message pattern to search for.
+ #
+ # @return [Warning]
+ # The warnings from the sub-path.
+ #
+ def self.grep(pattern)
+ $WARNINGS.select { |warning| warning.message.match(pattern) }
+ end
+
+ #
+ # Selects all warnings originating from a file.
+ #
+ # @param [String] path
+ # The sub-path to search for.
+ #
+ # @return [Warning]
+ # The warnings from the sub-path.
+ #
+ def self.from_file(path)
+ if File.extname(path).empty?
+ # default the file extension to `.rb`
+ path = "#{path}.rb"
+ end
+
+ $WARNINGS.select do |warning|
+ (warning.source_file == path) ||
+ warning.source_file.end_with?("/#{path}")
+ end
+ end
+
+ #
+ # Selects all warnings originating from a method.
+ #
+ # @param [Regexp, String] name
+ # The method name or pattern to search for.
+ #
+ # @return [Warning]
+ # The warnings from the specified method.
+ #
+ def self.from_method(name)
+ selector = case name
+ when Regexp
+ lambda { |warning| warning.source_method =~ name }
+ else
+ name = name.to_s
+
+ lambda { |warning| warning.source_method == name }
+ end
+
+ $WARNINGS.select(&selector)
+ end
+end
@@ -0,0 +1,12 @@
+require 'spec_helper'
+require 'warnings/mixin'
+
+describe Warnings::Mixin do
+ subject { Object.new.extend(Warnings::Mixin) }
+
+ it "should allow adding warnings to $WARNINGS" do
+ subject.warn "test1"
+
+ $WARNINGS.last.message.should == "test1"
+ end
+end
@@ -0,0 +1,5 @@
+gem 'rspec', '~> 2.4'
+require 'rspec'
+require 'warnings/version'
+
+include Warnings
Oops, something went wrong.

0 comments on commit 039a41e

Please sign in to comment.