• Stars
    star
    16
  • Rank 1,267,838 (Top 26 %)
  • Language
    Ruby
  • License
    MIT License
  • Created over 14 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

This is an implementation of Daniel Berger's proposal of structured warnings for Ruby.

StructuredWarnings

This is an implementation of Daniel Berger's proposal of structured warnings for Ruby. They provide dynamic suppression and activation, as well as, an inheritance hierarchy to model their relations. This library preserves the old warn signature, but additionally allows a raise-like use.

For more information on the usage and benefits of this library have a look at the inspiring article at O'Reilly.

www.oreillynet.com/ruby/blog/2008/02/structured_warnings_now.html (link to web archive - O'Reilly took it down)

Installation

Add this line to your application's Gemfile:

gem 'structured_warnings'

And then execute:

$ bundle

Or install it yourself as:

$ gem install structured_warnings

Compatibility

structured_warnings aims to work with all Ruby interpreters. Please file a bug for any incompatibilities.

Versions of structured_warnings before v0.3.0 are incompatible with Ruby 2.4+. Please upgrade accordingly, if you need Ruby 2.4 compatibility. Please note on the otherhand, that many class names changed in an incompatible way with structured_warnings v0.3.0. This was done to avoid future name clashes.

Here's a table which should ease upgrading.

v0.2.0 and before v0.3.0 and after
Warning StructuredWarnings::Base
StandardWarning StructuredWarnings::StandardWarning
DeprecationWarning StructuredWarnings::DeprecationWarning
DeprecatedMethodWarning StructuredWarnings::DeprecatedMethodWarning
DeprecatedSignatureWarning StructuredWarnings::DeprecatedSignatureWarning

Test framework support

structured_warnings supports both test-unit and minitest/test by adding the assert_warn and assert_no_warn assertions.

Pull requests which add support for RSpec or minitest/spec are very welcome.

Known Issues

In Ruby versions before 2.4, the library may not extend Ruby's built-in warnings handled by the C-level function rb_warn. Therefore warnings like "method redefined", "void context", and "parenthesis" may not be manipulated by structured_warnings.

Usage

To get you started - here is a short example

In order to use structured_warnings in library code, use the following code.

# in lib/...
require 'structured_warnings'

class Foo
  def old_method
    warn StructuredWarnings::DeprecatedMethodWarning, 'This method is deprecated. Use new_method instead'
    # Do stuff
  end
end

# in test/...
require 'test/unit'
require 'structured_warnings'

class FooTests < Test::Unit::TestCase
  def setup
    @foo = Foo.new
  end

  def test_old_method_emits_deprecation_warning
    assert_warn(StructuredWarnings::DeprecatedMethodWarning){ @foo.old_method }
  end
end

StructuredWarnings::DeprecatedMethodWarning is only one of multiple predefined warning types. You may add your own types by subclassing StructuredWarnings::Base if you like.

Client code of your library will look as follows:

require "foo"

foo = Foo.new
foo.old_method # => will print
               # ... `old_method' : This method is deprecated. Use new_method instead (StructuredWarnings::DeprecatedMethodWarning)

But the main difference to the standard warning concept shipped with ruby, is that the client is able to selectively disable certain warnings s/he is aware of and not willing to fix.

StructuredWarnings::DeprecatedMethodWarning.disable # Globally disable warnings about deprecated methods!

foo.old_method # => will print nothing

StructuredWarnings::DeprecatedMethodWarning.enable # Reenable warnings again.

And there is an even more powerful option for your clients, the can selectively disable warnings in a dynamic block scope.

# Don't bug me about deprecated method warnings within this block, I know
# what I'm doing.
#
StructuredWarnings::DeprecatedMethodWarning.disable do
  foo.old_method
end

These settings are scoped to the local thread (and all threads spawned in the block scope) and automatically reset after the block.

Detailed Documentation

Have closer look at the RDoc of StructuredWarnings::Warning, StructuredWarnings::Base and StructuredWarnings::Base::ClassMethods.

Part of this library is a set of different warnings:

  • StructuredWarnings::Base
    • StructuredWarnings::BuiltInWarning
    • StructuredWarnings::StandardWarning
    • StructuredWarnings::DeprecationWarning
      • StructuredWarnings::DeprecatedMethodWarning
      • StructuredWarnings::DeprecatedSignatureWarning

You are encouraged to use your own subclasses of StructuredWarnings::Base to give as much feedback to your users as possible.

Resources

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake test to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at github.com/schmidt/structured_warnings.

License

The gem is available as open source under the terms of the MIT License.

More Repositories

1

contextr

A context-oriented programming API for Ruby
Ruby
13
star
2

context_wiki

ContextWiki :: Camping Wiki using ContextR
Ruby
7
star
3

redmine_zxcvbn

Redmine zxcvbn plugin
CSS
5
star
4

literate_maruku

Literate Maruku is a literate programming libary for Ruby based on the Markdown libary Maruku.
Ruby
4
star
5

cordova-ios-iframe-example

Example project created to narrow down a problem with cordova-ios
CSS
4
star
6

chiliproject_custom_home_path

Custom Home Path for ChiliProject
Ruby
3
star
7

multiple_inheritance

Implementing multiple inheritance in plain Ruby
Ruby
3
star
8

flickr_downloader

Download Flickr Album based on a GuestPass or Album URL
Ruby
3
star
9

moviepilot

A Ruby wrapper around moviepilot's REST API (never tested)
Ruby
2
star
10

realtimebattle.rb

Ruby client for RealTimeBattle
2
star
11

overloading

Ruby
2
star
12

vimfiles

My Vim configuration
Vim Script
2
star
13

swt_rawr_hello_world

Hello World example for SWT application with JRuby and Rawr
Ruby
2
star
14

jquery.onscreen

jQuery plugin which adds an :onscreen pseudo selector
JavaScript
1
star
15

chiliproject_safe_password_hashes

Safe Password Hashes for ChiliProject
Ruby
1
star
16

find_by_param_example_app

find_by_param example app for manual integration testing
Ruby
1
star
17

bon_chance

Bonne Chance, Ricardo!
Ruby
1
star
18

chiliproject_bitly_host

ChiliProject Bitly Host Plugin
Ruby
1
star
19

chiliproject_devtools

collection of common helpers for chiliproject development
Ruby
1
star
20

HelloMoon

Hello World in Silverlight/Moonlight build with MonoDevelop
C#
1
star
21

beagle_on_rails

Outdated Web frontend to Beagle the .Net based seach engine
Ruby
1
star
22

chiliproject_wiki_tabs

ChiliProject Plugin which allows you to add custom tabs that link to wiki pages
Ruby
1
star
23

cache_annotations

old experiment to implement active_support's memoize before a_s came up with it
Ruby
1
star
24

mephisto_bloglines

A Mephisto Plugin to include a Bloglines Blogroll into your web site
Ruby
1
star
25

license_plate

I simple site to give you a rough preview how your license plate will look like
JavaScript
1
star