README.md in state_inspector-1.0.2 vs README.md in state_inspector-1.0.3
- old
+ new
@@ -1,12 +1,12 @@
+<img src="https://s23.postimg.org/4iiqow7xn/observer_hooks3.png" alt="Observer Hooks" align="right" width="50" height="44" />
# StateInspector
[![Gem Version](https://badge.fury.io/rb/state_inspector.svg)](http://badge.fury.io/rb/state_inspector)
[![Build Status](https://travis-ci.org/danielpclark/state_inspector.svg?branch=master)](https://travis-ci.org/danielpclark/state_inspector)
[![SayThanks.io](https://img.shields.io/badge/SayThanks.io-%E2%98%BC-1EAEDB.svg)](https://saythanks.io/to/danielpclark)
-The original purpose of this project is to log state change on target objects. This now can fully
-inform on method calls with parameters as well as instance variables.
+This can fully inform on object method calls and parameters as well as instance variables before and after (when called via a setter method). In short this utilizes the decorator patttern and the observer pattern to hook and report when a method is called. In simple terms it will wrap any methods you choose with a hook that sends off all the details of the method call when it is executed to a reporter/observer of your choosing.
This project uses a variation of the observer pattern. There is a hash of Reporters where you can
mark the key as a class instance or the class itself and point it to an Observer object. Three
observers are included for you to use under StateInspector::Observers which are NullObserver (default),
InternalObserver, and SessionLoggerObserver. When you toggle on an "informant" on a class instance or
@@ -27,11 +27,11 @@
Or install it yourself as:
$ gem install state_inspector
-## Usage
+## Complex Usage
The preferred usage is to pick what classes you want to track state change in and have them logged to
a session logger. To do this you would need to do the following.
```ruby
@@ -63,23 +63,35 @@
If you don't want to inform on all instances of a class then instead of running `toggle_informant`
on the class itself then simply execute that method on the instances you want to observe.
If you want to see the expected results of the current observer/reporters then see [test/reporter_test.rb](https://github.com/danielpclark/state_inspector/blob/master/test/reporter_test.rb).
+## Simple Usage
+
If you want to only toggle an informant for a small area you may use a helper method to toggle the
observer on and off for you.
+When you include `StateInspector::Helper` it handles requiring the existing observers and bringing them
+into scope. You are free to pass in an observer as a second paramater to the `toggle_snoop` helper method
+which will only be assigned for the scope of the block of code.
+
```ruby
require 'state_inspector/helper'
-include Helper
+include StateInspector::Helper
# instead of doing MyClass.toggle_informant as above, do this.
m = MyClass.new
-toggle_snoop(m) do
+observer = InternalObserver.new
+
+# observer parameter optional. Assign beforehand if not provided here.
+toggle_snoop(m, observer) do
# put your own code here
end
+
+# look at the results
+observer.values
```
When writing tests for code and using StateInspector it's very important to ensure the informant is
untoggled. Otherwise you will have sporatic behavior in your tests. You may use the helpers provided
here for your tests to ensure you won't have glitchy tests as a result of using informants.
@@ -224,10 +236,29 @@
LEGEND FOR METHOD| _ | _
-----------------|---|---
`self` | `:method_name` | `:arguments`
+## Session Logger
+
+The session logger is an observer that saves all output to a log file. A default log file is saved to `log/state_inspector/<timestamp>.log`. You can manually set a log file with the `file=` method on the SessionLoggerObserver which may be helpful for situations where the code runs in a temporary directory and vanishes upon completion (like the rubygems test suite).
+
+Here's an example that catches all setter method calls from constants within the `Gem` class. _Placed at start of file to observe._
+
+```ruby
+require 'state_inspector'
+require 'state_inspector/observers/session_logger_observer'
+include StateInspector::Observers
+
+SessionLoggerObserver.file = "/home/myhomedir/dev/rubygems/log/output.log"
+StateInspector::Reporter.default SessionLoggerObserver
+Gem.constants.each {|c| a = eval("Gem::#{c}"); if a.is_a? Class; a.toggle_informant end}
+```
+This example is a very expensive one as we set the default observer/reporter to `SessionLoggerObserver` which means it catches **all reports not previously assigned**. The last line simply finds all class objects within the Gem namespace and toggles-on the informants (which by default hooks in listeners to each setter method). The `file=` method used here overwrites the default and guarantees where the data will be written.
+
+I've tried the above code in the rubygems test suite on one file `test/rubygems/test_gem_dependency_installer.rb`. When running this file it records 7756 lines worth of information. This is clearly too much information to parse manually which is why I highly recommend using the scoped helper methods to toggle on and off the behavior around the code you are specifically interested in. You can still toggle informers on many classes like what was done above, but the more objects you do the more I recommend you narrow down the scope of what you're capturing (like to one specific test in a test suite).
+
## 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](https://rubygems.org).
@@ -238,6 +269,7 @@
## License
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
+