state_inspector 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +12 -0
- data/.travis.yml +5 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +139 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/state_inspector.rb +8 -0
- data/lib/state_inspector/observers.rb +3 -0
- data/lib/state_inspector/observers/internal_observer.rb +14 -0
- data/lib/state_inspector/observers/null_observer.rb +12 -0
- data/lib/state_inspector/observers/observer.rb +21 -0
- data/lib/state_inspector/observers/session_logger_observer.rb +82 -0
- data/lib/state_inspector/reporter.rb +30 -0
- data/lib/state_inspector/snoop.rb +47 -0
- data/lib/state_inspector/state_inspector.rb +8 -0
- data/lib/state_inspector/version.rb +3 -0
- data/state_inspector.gemspec +27 -0
- metadata +106 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 91381367b1700878be9583f54504f203f15175ca
|
4
|
+
data.tar.gz: 22dc94bacd8aaaec80b95fdd6277730dcbbcec47
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c5ef7d1e269c4874c8a7c9c937c8ce649a1d23e27797def648fb73d20c91ab3e81862199392d60b40181db1ac202931d216f3c7a984d63334ed02a21de4100bd
|
7
|
+
data.tar.gz: c066d620a79f942ef71fe8ffe84962bddf143d75c841f170351d970e1029f929c1c409346f54501b18acddc23facd5a9721e3a59a3afdf3b63a5cf0ce18004e7
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 Daniel P. Clark
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,139 @@
|
|
1
|
+
# StateInspector
|
2
|
+
[](http://badge.fury.io/rb/state_inspector)
|
3
|
+
[](https://travis-ci.org/danielpclark/state_inspector)
|
4
|
+
|
5
|
+
The original purpose of this project is to log state change on target objects. This will expand
|
6
|
+
further into additional introspection as new versions are made available.
|
7
|
+
|
8
|
+
**Currently this project depends on attr hooks.**
|
9
|
+
|
10
|
+
This project uses a variation of the observer pattern. There is a hash of Reporters where you can
|
11
|
+
mark the key as a class instance or the class itself and point it to an Observer object. Three
|
12
|
+
observers are included for you to use under StateInspector::Observers which are NullObserver (default),
|
13
|
+
InternalObserver, and SessionLoggerObserver. When you toggle on an "informant" on a class instance or
|
14
|
+
class then each time a setter method is called it will pass that information on to the relevant observer
|
15
|
+
which handles the behavior you want to occur with that information.
|
16
|
+
|
17
|
+
## Installation
|
18
|
+
|
19
|
+
Add this line to your application's Gemfile:
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
gem 'state_inspector'
|
23
|
+
```
|
24
|
+
|
25
|
+
And then execute:
|
26
|
+
|
27
|
+
$ bundle
|
28
|
+
|
29
|
+
Or install it yourself as:
|
30
|
+
|
31
|
+
$ gem install state_inspector
|
32
|
+
|
33
|
+
## Usage
|
34
|
+
|
35
|
+
The preferred usage is to pick what classes you want to track state change in and have them logged to
|
36
|
+
a session logger. To do this you would need to do the following.
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
require 'state_inspector'
|
40
|
+
require 'state_inspector/observers/session_logger_observer'
|
41
|
+
include StateInspector::Observers
|
42
|
+
|
43
|
+
class MyClass
|
44
|
+
attr_writer :thing
|
45
|
+
end
|
46
|
+
|
47
|
+
StateInspector::Reporter[MyClass] = SessionLoggerObserver
|
48
|
+
|
49
|
+
MyClass.toggle_informant
|
50
|
+
```
|
51
|
+
|
52
|
+
Now everytime the setter method is used for `thing` a new line will be entered into a log file
|
53
|
+
of the object, method, old value, and new value. So you will see what is changed from where in
|
54
|
+
the order that the changed occurred. This session logger will grab as many objects state changes
|
55
|
+
as you want and give you a nice ordered history of what has occurred.
|
56
|
+
|
57
|
+
If you don't want to inform on all instances of one class then instead of running `toggle_informant`
|
58
|
+
on the class then execute that method on just the instances of that class you want instead.
|
59
|
+
|
60
|
+
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).
|
61
|
+
|
62
|
+
## Observers
|
63
|
+
|
64
|
+
To include all Observers into scope you can do:
|
65
|
+
|
66
|
+
```ruby
|
67
|
+
require 'state_inspector/observers'
|
68
|
+
include StateInspector::Observers
|
69
|
+
```
|
70
|
+
|
71
|
+
You may look at the available observers in [state_inspector/observers](https://github.com/danielpclark/state_inspector/tree/master/lib/state_inspector/observers).
|
72
|
+
|
73
|
+
Observers will have a few methods they each have in common.
|
74
|
+
|
75
|
+
```ruby
|
76
|
+
module Observer
|
77
|
+
def update *vals
|
78
|
+
values() << vals
|
79
|
+
end
|
80
|
+
|
81
|
+
def display
|
82
|
+
values.join " "
|
83
|
+
end
|
84
|
+
|
85
|
+
def values
|
86
|
+
@values ||= []
|
87
|
+
end
|
88
|
+
|
89
|
+
def purge
|
90
|
+
@values = []
|
91
|
+
end
|
92
|
+
end
|
93
|
+
```
|
94
|
+
|
95
|
+
When you're writing your own observer you'll include this Observer onto a module's instance and
|
96
|
+
overwrite whatever methods you want there.
|
97
|
+
|
98
|
+
```ruby
|
99
|
+
module ExampleObserver
|
100
|
+
class << self
|
101
|
+
include Observer
|
102
|
+
def display
|
103
|
+
"Custom display code here"
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
```
|
108
|
+
|
109
|
+
And to register this observer to a target class you simply write:
|
110
|
+
|
111
|
+
```ruby
|
112
|
+
StateInspector::Reporter[MyTargettedClass] = ExampleObserver
|
113
|
+
```
|
114
|
+
|
115
|
+
|
116
|
+
## Road Map
|
117
|
+
|
118
|
+
* 0.8.0 State inspection of all flagged objects on setter methods.
|
119
|
+
Includes logger observer, internal observer, and null observer.
|
120
|
+
|
121
|
+
* 0.9.0 Sweep for missed setter methods and prepend inspection behavior.
|
122
|
+
|
123
|
+
* 1.0.0 Optional reporting on all/target method calls
|
124
|
+
|
125
|
+
## Development
|
126
|
+
|
127
|
+
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.
|
128
|
+
|
129
|
+
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).
|
130
|
+
|
131
|
+
## Contributing
|
132
|
+
|
133
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/danielpclark/state_inspector.
|
134
|
+
|
135
|
+
|
136
|
+
## License
|
137
|
+
|
138
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
139
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "state_inspector"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
module StateInspector
|
2
|
+
module Observers
|
3
|
+
module Observer
|
4
|
+
def update *vals
|
5
|
+
values() << vals
|
6
|
+
end
|
7
|
+
|
8
|
+
def display
|
9
|
+
values.join " "
|
10
|
+
end
|
11
|
+
|
12
|
+
def values
|
13
|
+
@values ||= []
|
14
|
+
end
|
15
|
+
|
16
|
+
def purge
|
17
|
+
@values = []
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,82 @@
|
|
1
|
+
require_relative 'observer'
|
2
|
+
require 'fileutils'
|
3
|
+
require 'logger'
|
4
|
+
|
5
|
+
module StateInspector
|
6
|
+
module Observers
|
7
|
+
module SessionLoggerObserver
|
8
|
+
class << self
|
9
|
+
include Observer
|
10
|
+
def update *values
|
11
|
+
folder = File.join ['log', 'state_inspector']
|
12
|
+
@file ||= File.join(
|
13
|
+
folder,
|
14
|
+
['session', Time.now.to_i, 'log'].join('.')
|
15
|
+
)
|
16
|
+
FileUtils.mkdir_p folder
|
17
|
+
File.open(@file, File::WRONLY | File::APPEND | File::CREAT) do |file|
|
18
|
+
logger = Logger.new(file)
|
19
|
+
logger << values.
|
20
|
+
map(&value_mapper).
|
21
|
+
join(splitter)
|
22
|
+
logger << "\n"
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def file= f
|
27
|
+
warn("Warning! Log file #{@file} was already set!") if @file
|
28
|
+
@file = f
|
29
|
+
end
|
30
|
+
|
31
|
+
def display
|
32
|
+
if @file
|
33
|
+
File.open(@file, File::RDONLY) {|f| f.read }
|
34
|
+
else
|
35
|
+
""
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def values
|
40
|
+
if @file
|
41
|
+
File.open(@file, File::RDONLY) {|f| f.readlines}.map(&:chomp).map do |line|
|
42
|
+
if line.empty?
|
43
|
+
nil
|
44
|
+
else
|
45
|
+
line.split(splitter).map(&value_mapper)
|
46
|
+
end
|
47
|
+
end.compact
|
48
|
+
else
|
49
|
+
[]
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def purge
|
54
|
+
File.delete(@file) if File.exist? @file
|
55
|
+
@file = nil
|
56
|
+
end
|
57
|
+
|
58
|
+
private
|
59
|
+
def splitter
|
60
|
+
"\t\t"
|
61
|
+
end
|
62
|
+
|
63
|
+
def value_mapper
|
64
|
+
->v{
|
65
|
+
case v
|
66
|
+
when nil
|
67
|
+
"nil"
|
68
|
+
when "nil"
|
69
|
+
nil
|
70
|
+
when Symbol
|
71
|
+
v.inspect
|
72
|
+
when ->val{ val.is_a?(String) && val =~ /\A:/ }
|
73
|
+
v[1..-1].to_sym
|
74
|
+
else
|
75
|
+
v
|
76
|
+
end
|
77
|
+
}
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require_relative 'observers/null_observer'
|
2
|
+
|
3
|
+
module StateInspector
|
4
|
+
# OBSERVABLE PATTERN
|
5
|
+
# HASH-LIKE KEY OF OJECT TO OBSERVER/LOGGER INSTANCE
|
6
|
+
module Reporter
|
7
|
+
class << self
|
8
|
+
def [](key)
|
9
|
+
reporters[key]
|
10
|
+
end
|
11
|
+
|
12
|
+
def []=(key, value)
|
13
|
+
reporters[key] = value
|
14
|
+
end
|
15
|
+
|
16
|
+
def default observer=nil
|
17
|
+
@default = observer if observer
|
18
|
+
reporters.default = @default
|
19
|
+
@default
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
def reporters
|
24
|
+
@reporters ||= Hash.new.tap {|h|
|
25
|
+
h.default = @default || Observers::NullObserver
|
26
|
+
}
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require_relative 'state_inspector'
|
2
|
+
require_relative 'reporter'
|
3
|
+
|
4
|
+
module StateInspector
|
5
|
+
module Snoop
|
6
|
+
def Snoop.extended(base)
|
7
|
+
base.include ClassMethods
|
8
|
+
end
|
9
|
+
|
10
|
+
def attr_writer attr_name
|
11
|
+
define_method("#{attr_name}=") do |value|
|
12
|
+
tell_si __method__.to_s.chop,
|
13
|
+
instance_variable_get("@#{attr_name.to_s}"),
|
14
|
+
value
|
15
|
+
|
16
|
+
instance_variable_set("@#{attr_name.to_s}", value)
|
17
|
+
end
|
18
|
+
nil
|
19
|
+
end
|
20
|
+
|
21
|
+
def attr_accessor attr_name
|
22
|
+
define_method("#{attr_name}") do
|
23
|
+
instance_variable_get("@#{attr_name.to_s}")
|
24
|
+
end
|
25
|
+
|
26
|
+
self.attr_writer(attr_name)
|
27
|
+
end
|
28
|
+
|
29
|
+
module ClassMethods
|
30
|
+
def state_inspector
|
31
|
+
StateInspector
|
32
|
+
end
|
33
|
+
|
34
|
+
def tell_si what, old, new
|
35
|
+
state_inspector.report( self, "@#{what.to_s}", old, new ) if informant?
|
36
|
+
end
|
37
|
+
|
38
|
+
def toggle_informant
|
39
|
+
@informant = !@informant
|
40
|
+
end
|
41
|
+
|
42
|
+
def informant?
|
43
|
+
@informant || self.class.instance_variable_get(:@informant)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'state_inspector/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "state_inspector"
|
8
|
+
spec.version = StateInspector::VERSION
|
9
|
+
spec.authors = ["Daniel P. Clark"]
|
10
|
+
spec.email = ["6ftdan@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Introspection tool for reporting state change.}
|
13
|
+
spec.description = %q{Introspection tool for reporting state change. Instance varialbes and method call debugging.}
|
14
|
+
spec.homepage = "https://github.com/danielpclark/state_inspector"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
18
|
+
f.match(%r{^(test|spec|features)/})
|
19
|
+
end
|
20
|
+
spec.bindir = "exe"
|
21
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
22
|
+
spec.require_paths = ["lib"]
|
23
|
+
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.13"
|
25
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
26
|
+
spec.add_development_dependency "minitest", "~> 5.0"
|
27
|
+
end
|
metadata
ADDED
@@ -0,0 +1,106 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: state_inspector
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.8.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Daniel P. Clark
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-01-19 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.13'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.13'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '5.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '5.0'
|
55
|
+
description: Introspection tool for reporting state change. Instance varialbes and
|
56
|
+
method call debugging.
|
57
|
+
email:
|
58
|
+
- 6ftdan@gmail.com
|
59
|
+
executables: []
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- ".gitignore"
|
64
|
+
- ".travis.yml"
|
65
|
+
- Gemfile
|
66
|
+
- LICENSE.txt
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- bin/console
|
70
|
+
- bin/setup
|
71
|
+
- lib/state_inspector.rb
|
72
|
+
- lib/state_inspector/observers.rb
|
73
|
+
- lib/state_inspector/observers/internal_observer.rb
|
74
|
+
- lib/state_inspector/observers/null_observer.rb
|
75
|
+
- lib/state_inspector/observers/observer.rb
|
76
|
+
- lib/state_inspector/observers/session_logger_observer.rb
|
77
|
+
- lib/state_inspector/reporter.rb
|
78
|
+
- lib/state_inspector/snoop.rb
|
79
|
+
- lib/state_inspector/state_inspector.rb
|
80
|
+
- lib/state_inspector/version.rb
|
81
|
+
- state_inspector.gemspec
|
82
|
+
homepage: https://github.com/danielpclark/state_inspector
|
83
|
+
licenses:
|
84
|
+
- MIT
|
85
|
+
metadata: {}
|
86
|
+
post_install_message:
|
87
|
+
rdoc_options: []
|
88
|
+
require_paths:
|
89
|
+
- lib
|
90
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
91
|
+
requirements:
|
92
|
+
- - ">="
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '0'
|
95
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
96
|
+
requirements:
|
97
|
+
- - ">="
|
98
|
+
- !ruby/object:Gem::Version
|
99
|
+
version: '0'
|
100
|
+
requirements: []
|
101
|
+
rubyforge_project:
|
102
|
+
rubygems_version: 2.6.8
|
103
|
+
signing_key:
|
104
|
+
specification_version: 4
|
105
|
+
summary: Introspection tool for reporting state change.
|
106
|
+
test_files: []
|