0.0
Low commit activity in last 3 years
A long-lived project that still receives updates
ActiveSupport Kernel Reporting Detritus with a few enhancements
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 5.22
>= 0
>= 0
>= 0.16
>= 3.2
>= 0
~> 10.1, >= 10.1.1
~> 2.26, >= 2.26.1
~> 1.33
 Project Readme

SilentStream

SilentStream is an extraction of some parts of ActiveSupport's Kernel Reporting Core Extentions around silencing IO streams.

Since July 2014 silence_stream, silence_stderr, capture, silence, and quietly have been deprecated because they are not thread safe. See that discussion in the PR where it all went down. I rely on them a lot in single threaded code, and so I plan to keep them alive. With the exception of silence, which was just an alias of capture.

This gem was taken out of Rails but it is not Rails dependent. The extraction was total (even the tests!), and this is now a pure Ruby library, which can be used in any Ruby project without encumbrances. This gem has no runtime dependencies.

Project SilentStream
gem name silent_stream
compatibility Ruby 2.3, 2.4, 2.5, 2.6, 2.7, 3.0, 3.1, 3.2, 3.3
license License: MIT
download rank Downloads Today
version Version
continuous integration Build Status
test coverage Test Coverage
maintainability Maintainability
dependencies Depfu
code triage Open Source Helpers
homepage on Github.com, on Railsbling.com
documentation on RDoc.info
Spread ♡ⓛⓞⓥⓔ♡ 🌏, 👼, Tweet Peter

NOTE

One aspect of what this gem provides can be achieved with the Rails' built-in LoggerSilence, which is thread safe. You will have to decide what is right for you!

Doing a Rails <= 4 to Rails >= 5 Upgrade?

The reason for not keeping silence as it was in Rails 4, i.e. an alias of capture, is that the just mentioned LoggerSilence now uses this term, and it is shipping with Rails 5. I don't want to make this gem incompatible with Rails 5, so you will have to convert Rails <= 4 implementations that utilize silence over to capture when using this gem. One further point of difference is this gem does not add the methods to Kernel or Object. You can do that if you like via include. By default this gem does not pollute anything, so you will need to include SilentStream in any class using these methods.

Installation

Add this line to your application's Gemfile:

gem "silent_stream"

And then execute:

$ bundle

Or install it yourself as:

$ gem install silent_stream

Usage

Four standard methods you may be familiar with from ActiveSupport's previous implementation are provided:

silence_stderr
silence_stream
capture
quietly

They are direct replicas, except not mixed into Kernel or Object, so in order to use them you must mix them into your classes or modules.

class Bogosity
  include SilentStream::Extracted # allows use at instance level
  extend SilentStream::Extracted # allows use at class level
  # or
  include SilentStream # access everything, and add #silence_all method, see below
end

In addition there is a silence_all method that is a useful wrapper that can be easily instrumented (turned off and on) with an ENV variable switch.

Including the SilentStream namespace fully gives access to this enhanced method, as well as the extracted methods above, and also makes everything available at the class and instance levels.

class Bogosity
  include SilentStream # allows use of any method at instance or class level

  def silent
    silence_all(true) do
      puts "play that funky music"
      Rails.logger.info("git jiggy with it")
    end
  end
  class << self
    def noise
      silence_all(false) do
        puts "play that funky music"
        Rails.logger.info("git jiggy with it")
      end
    end
  end
end

And run

>> Bogosity.new.silent # has no output
=> nil
>> Bogosity.noise # is noisy
play that funky music
=> nil

Use in Specs / Tests

Make the methods avaialble:

RSpec.configure do |conf|
  conf.include(SilentStream)
end

Then add a test on output:

it "has output" do
  output = capture(:stdout) { subject.request(:get, "/success") }
  logs = [
    "INFO -- request: GET https://api.example.com/success",
    "INFO -- response: Status 200",
  ]
  expect(output).to(include(*logs))
end

See it in practice in the specs for the oauth2 gem and the debug_logging gem

Migrate from ActiveSupport::Testing::Stream, or remove ActiveSupport completely, in your ruby library!

For most scenarios, simple. Change three lines. Here's an example from a gem I just converted from ActiveSupport to SilentStream (see commit)

gemspec diff:

-spec.add_development_dependency 'activesupport', '>= 5'
+spec.add_development_dependency 'silent_stream', '>= 1'

spec_helper.rb diff:

-require 'active_support/testing/stream'
+require 'silent_stream'

RSpec.configure do |config|
-  config.include ActiveSupport::Testing::Stream
+  config.include SilentStream

Run spec suite to verify everything is good. This gem is as close as can be to a drop-in replacement for Rails' ActiveSupport::Testing::Stream.

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec 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 https://github.com/pboling/silent_stream.

Code of Conduct

Everyone interacting in the AnonymousActiveRecord project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.

Versioning

This library aims to adhere to Semantic Versioning 2.0.0. Violations of this scheme should be reported as bugs. Specifically, if a minor or patch version is released that breaks backward compatibility, a new version should be immediately released that restores compatibility. Breaking changes to the public API will only be introduced with new major versions.

As a result of this policy, you can (and should) specify a dependency on this gem using the Pessimistic Version Constraint with two digits of precision.

For example:

spec.add_dependency("silent_stream", "~> 1.0")

License

License: MIT