state_inspector 1.0.2 → 1.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA256:
3
- metadata.gz: 3d15f153b234a84d1a55dfcdabf0da072a54525b3f4e002264903886dffcbe0f
4
- data.tar.gz: 5dfc62c1219a7eed867d36b08bf00e03c954ca4f505dd26dc1b64d4bee6998d9
2
+ SHA1:
3
+ metadata.gz: fe629c67e180cfa586f4543e4d0bb416fa1694e1
4
+ data.tar.gz: 14cf88964e95a255333b1890aac132ca45df850f
5
5
  SHA512:
6
- metadata.gz: 1270b6929c3ff8ba30e83ddb07aebf41f1ba3d60f0b33feac2b4275fe3f3b8b48c2f25bb9e6ba9ebb6ec6ef22acf31599e560d0bb2a2a2c93e02ccd719b74d25
7
- data.tar.gz: 46e4b286c61f4232dc01acd8ae7bc1876d2c77923af2099070f85d38bc22f071cc00c754acdb17c947fcd3ff089f01f2589afd300da907671d148bc7877f5f00
6
+ metadata.gz: 6499a6c6fd0b6d43e0b14e9b9677fd57ed5a7e185e845ff910f7a563221f9b08974cc38a8add1199af3c70dc4cb6b61970f15158db0aca072c69c7fbcb280032
7
+ data.tar.gz: c7af11123487b76d83de3c3653869275e2cbda55c0dacfc3ced4fd5e4a9e175ba53b6a7d08034c7a566fcbbef81890fa5512009c82009af9d5b1aac0d339d543
data/README.md CHANGED
@@ -1,10 +1,10 @@
1
+ <img src="https://s23.postimg.org/4iiqow7xn/observer_hooks3.png" alt="Observer Hooks" align="right" width="50" height="44" />
1
2
  # StateInspector
2
3
  [![Gem Version](https://badge.fury.io/rb/state_inspector.svg)](http://badge.fury.io/rb/state_inspector)
3
4
  [![Build Status](https://travis-ci.org/danielpclark/state_inspector.svg?branch=master)](https://travis-ci.org/danielpclark/state_inspector)
4
5
  [![SayThanks.io](https://img.shields.io/badge/SayThanks.io-%E2%98%BC-1EAEDB.svg)](https://saythanks.io/to/danielpclark)
5
6
 
6
- The original purpose of this project is to log state change on target objects. This now can fully
7
- inform on method calls with parameters as well as instance variables.
7
+ 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.
8
8
 
9
9
  This project uses a variation of the observer pattern. There is a hash of Reporters where you can
10
10
  mark the key as a class instance or the class itself and point it to an Observer object. Three
@@ -29,7 +29,7 @@ Or install it yourself as:
29
29
 
30
30
  $ gem install state_inspector
31
31
 
32
- ## Usage
32
+ ## Complex Usage
33
33
 
34
34
  The preferred usage is to pick what classes you want to track state change in and have them logged to
35
35
  a session logger. To do this you would need to do the following.
@@ -65,19 +65,31 @@ on the class itself then simply execute that method on the instances you want to
65
65
 
66
66
  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).
67
67
 
68
+ ## Simple Usage
69
+
68
70
  If you want to only toggle an informant for a small area you may use a helper method to toggle the
69
71
  observer on and off for you.
70
72
 
73
+ When you include `StateInspector::Helper` it handles requiring the existing observers and bringing them
74
+ into scope. You are free to pass in an observer as a second paramater to the `toggle_snoop` helper method
75
+ which will only be assigned for the scope of the block of code.
76
+
71
77
  ```ruby
72
78
  require 'state_inspector/helper'
73
- include Helper
79
+ include StateInspector::Helper
74
80
 
75
81
  # instead of doing MyClass.toggle_informant as above, do this.
76
82
 
77
83
  m = MyClass.new
78
- toggle_snoop(m) do
84
+ observer = InternalObserver.new
85
+
86
+ # observer parameter optional. Assign beforehand if not provided here.
87
+ toggle_snoop(m, observer) do
79
88
  # put your own code here
80
89
  end
90
+
91
+ # look at the results
92
+ observer.values
81
93
  ```
82
94
 
83
95
  When writing tests for code and using StateInspector it's very important to ensure the informant is
@@ -226,6 +238,25 @@ LEGEND FOR METHOD| _ | _
226
238
  `self` | `:method_name` | `:arguments`
227
239
 
228
240
 
241
+ ## Session Logger
242
+
243
+ 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).
244
+
245
+ Here's an example that catches all setter method calls from constants within the `Gem` class. _Placed at start of file to observe._
246
+
247
+ ```ruby
248
+ require 'state_inspector'
249
+ require 'state_inspector/observers/session_logger_observer'
250
+ include StateInspector::Observers
251
+
252
+ SessionLoggerObserver.file = "/home/myhomedir/dev/rubygems/log/output.log"
253
+ StateInspector::Reporter.default SessionLoggerObserver
254
+ Gem.constants.each {|c| a = eval("Gem::#{c}"); if a.is_a? Class; a.toggle_informant end}
255
+ ```
256
+ 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.
257
+
258
+ 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).
259
+
229
260
  ## Development
230
261
 
231
262
  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.
@@ -241,3 +272,4 @@ Bug reports and pull requests are welcome on GitHub at https://github.com/daniel
241
272
 
242
273
  The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
243
274
 
275
+
@@ -1,20 +1,38 @@
1
+ require 'state_inspector'
2
+ require 'state_inspector/observers'
1
3
  module StateInspector
2
4
  module Helper
3
- def toggle_snoop(obj)
5
+ def self.included(base)
6
+ base.include Observers
7
+ end
8
+
9
+ def toggle_snoop(obj, observer=nil)
10
+ if observer
11
+ old_observer = Reporter.has_observer?(obj) ? Reporter[obj] : nil
12
+ Reporter[obj] = observer
13
+ end
4
14
  obj.toggle_informant
5
- yield
15
+ value = yield Reporter[obj]
6
16
  ensure
7
17
  obj.toggle_informant
18
+ (old_observer.nil? ? Reporter.drop(obj) : Reporter[obj] = old_observer) if observer
19
+ value
8
20
  end
9
21
 
10
- def toggle_snoop_clean(obj)
22
+ def toggle_snoop_clean(obj, observer=nil)
11
23
  obj.state_inspector.skip_setter_snoops
24
+ if observer
25
+ old_observer = Reporter.has_observer?(obj) ? Reporter[obj] : nil
26
+ Reporter[obj] = observer
27
+ end
12
28
  obj.toggle_informant
13
- yield
29
+ value = yield Reporter[obj]
14
30
  ensure
15
31
  obj.toggle_informant
16
32
  (obj.respond_to?(:class_eval) ? obj : obj.class).
17
33
  remove_instance_variable(:@state_inspector)
34
+ (old_observer.nil? ? Reporter.drop(obj) : Reporter[obj] = old_observer) if observer
35
+ value
18
36
  end
19
37
  end
20
38
  end
@@ -13,6 +13,19 @@ module StateInspector
13
13
  reporters[key] = value
14
14
  end
15
15
 
16
+ def has_key? key
17
+ reporters.has_key?(key)
18
+ end
19
+
20
+ def has_observer? key
21
+ class_key = key.respond_to?(:class_eval) ? key : key.class
22
+ reporters.has_key?(key) || reporters.has_key?(class_key)
23
+ end
24
+
25
+ def drop key
26
+ reporters.delete key
27
+ end
28
+
16
29
  def default observer=nil
17
30
  @default = observer if observer
18
31
  reporters.default = @default
@@ -15,6 +15,7 @@ module StateInspector
15
15
  def tell_si *args, &block
16
16
  if informant?
17
17
  key = self.respond_to?(:class_eval) ? self : self.class
18
+ key = Reporter.has_key?(key) ? key : self
18
19
  Reporter[key].update(self, *args, &block)
19
20
  end
20
21
  end
@@ -1,3 +1,3 @@
1
1
  module StateInspector
2
- VERSION = "1.0.2"
2
+ VERSION = "1.0.3"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: state_inspector
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel P. Clark
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-01-27 00:00:00.000000000 Z
11
+ date: 2017-02-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler