squeal 1.0.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.
- data/README.textile +69 -0
- data/lib/extensions/class.rb +15 -0
- data/lib/squeal.rb +2 -0
- data/lib/squeal/squeal.rb +78 -0
- data/spec/lib/squeal/squeal_spec.rb +89 -0
- metadata +68 -0
data/README.textile
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
h1. Squeal
|
2
|
+
|
3
|
+
A simple object instantiation counter.
|
4
|
+
|
5
|
+
Inspired by oink (http://github.com/noahd1/oink).
|
6
|
+
I needed a non-rails one for my search engine project Picky (http://github.com/floere/picky), but oink did not fit well.
|
7
|
+
|
8
|
+
Note: It reports cumulatively until @Squeal.reset@ is called.
|
9
|
+
|
10
|
+
Only works on certain Ruby classes yet (not String, for example).
|
11
|
+
|
12
|
+
h1. Examples
|
13
|
+
|
14
|
+
h2. Installation
|
15
|
+
|
16
|
+
gem install squeal
|
17
|
+
|
18
|
+
h2. On a single class.
|
19
|
+
|
20
|
+
require 'squeal'
|
21
|
+
|
22
|
+
TestClass.squeal do
|
23
|
+
TestClass.new
|
24
|
+
TestClass.new
|
25
|
+
Object.new
|
26
|
+
String.new
|
27
|
+
end
|
28
|
+
TestClass.new
|
29
|
+
|
30
|
+
Squeal.report # => 'TestClass: 2'
|
31
|
+
|
32
|
+
h2. On multiple classes
|
33
|
+
|
34
|
+
require 'squeal'
|
35
|
+
|
36
|
+
# Aliased as Squeal.record
|
37
|
+
#
|
38
|
+
Squeal.squeal(Object, TestClass) do
|
39
|
+
TestClass.new
|
40
|
+
String.new
|
41
|
+
Object.new
|
42
|
+
TestClass.new
|
43
|
+
String.new
|
44
|
+
end
|
45
|
+
Object.new
|
46
|
+
TestClass.new
|
47
|
+
|
48
|
+
Squeal.report # => 'Object: 1, TestClass: 2'
|
49
|
+
|
50
|
+
h2. Without a block
|
51
|
+
|
52
|
+
require 'squeal'
|
53
|
+
|
54
|
+
# Aliased as Squeal.squeal
|
55
|
+
#
|
56
|
+
Squeal.record Object, TestClass
|
57
|
+
|
58
|
+
TestClass.new
|
59
|
+
String.new
|
60
|
+
Object.new
|
61
|
+
TestClass.new
|
62
|
+
String.new
|
63
|
+
|
64
|
+
Squeal.stop
|
65
|
+
|
66
|
+
Object.new
|
67
|
+
TestClass.new
|
68
|
+
|
69
|
+
Squeal.report # => 'Object: 1, TestClass: 2'
|
data/lib/squeal.rb
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
class Squeal
|
2
|
+
|
3
|
+
module InitializeWrapper
|
4
|
+
|
5
|
+
alias_method :initialize_before_squeal, :initialize
|
6
|
+
|
7
|
+
def initialize *args, &block
|
8
|
+
instance = initialize_before_squeal *args, &block
|
9
|
+
self.class.squeal_increment
|
10
|
+
instance
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
14
|
+
|
15
|
+
class << self
|
16
|
+
|
17
|
+
# Increment the counter for the given class.
|
18
|
+
#
|
19
|
+
def increment klass
|
20
|
+
return unless @recording && @counters[klass]
|
21
|
+
@counters[klass] += 1
|
22
|
+
end
|
23
|
+
|
24
|
+
# Install and reset for the given class.
|
25
|
+
#
|
26
|
+
def reset_for klass
|
27
|
+
install_on klass
|
28
|
+
@counters ||= {}
|
29
|
+
@counters[klass] ||= 0
|
30
|
+
self
|
31
|
+
end
|
32
|
+
|
33
|
+
# Installs squeal on the given class if it isn't yet installed.
|
34
|
+
#
|
35
|
+
def install_on klass
|
36
|
+
klass.send :include, InitializeWrapper unless klass.include?(InitializeWrapper)
|
37
|
+
end
|
38
|
+
|
39
|
+
# Record the given classes for the duration of the block.
|
40
|
+
#
|
41
|
+
def record *klasses
|
42
|
+
@recording = true
|
43
|
+
klasses.each { |klass| reset_for klass }
|
44
|
+
if block_given?
|
45
|
+
yield
|
46
|
+
stop
|
47
|
+
end
|
48
|
+
self
|
49
|
+
end
|
50
|
+
alias squeal record
|
51
|
+
|
52
|
+
# Stop recording.
|
53
|
+
#
|
54
|
+
def stop
|
55
|
+
@recording = false
|
56
|
+
end
|
57
|
+
|
58
|
+
# Returns a string with the current instantiation count.
|
59
|
+
#
|
60
|
+
# Example:
|
61
|
+
# * SomeClass: 2, SomeOtherClass: 10
|
62
|
+
#
|
63
|
+
def report
|
64
|
+
result = []
|
65
|
+
@counters.each_pair do |klass, number|
|
66
|
+
result << "#{klass}: #{number}"
|
67
|
+
end
|
68
|
+
result.sort.join(', ')
|
69
|
+
end
|
70
|
+
|
71
|
+
# Resets the counters.
|
72
|
+
#
|
73
|
+
def reset
|
74
|
+
@counters = {}
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
end
|
@@ -0,0 +1,89 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Squeal do
|
4
|
+
before(:each) do
|
5
|
+
Object.send :remove_const, :TestClass if Object.const_defined?(:TestClass)
|
6
|
+
TestClass = Class.new
|
7
|
+
end
|
8
|
+
after(:each) do
|
9
|
+
Squeal.reset
|
10
|
+
end
|
11
|
+
it 'allows a proxy object' do
|
12
|
+
lambda { TestClass.squeal }.should_not raise_error
|
13
|
+
end
|
14
|
+
it 'records with a block' do
|
15
|
+
TestClass.squeal do
|
16
|
+
TestClass.new
|
17
|
+
TestClass.new
|
18
|
+
Object.new
|
19
|
+
String.new
|
20
|
+
end
|
21
|
+
TestClass.new
|
22
|
+
|
23
|
+
Squeal.report.should == 'TestClass: 2'
|
24
|
+
end
|
25
|
+
it 'records all mentioned classes with a block' do
|
26
|
+
Squeal.squeal(Object, TestClass) do
|
27
|
+
TestClass.new
|
28
|
+
String.new
|
29
|
+
Object.new
|
30
|
+
TestClass.new
|
31
|
+
String.new
|
32
|
+
end
|
33
|
+
Object.new
|
34
|
+
TestClass.new
|
35
|
+
|
36
|
+
Squeal.report.should == 'Object: 1, TestClass: 2'
|
37
|
+
end
|
38
|
+
it 'records the class it is used on with a block' do
|
39
|
+
TestClass.squeal do
|
40
|
+
TestClass.new
|
41
|
+
Hash.new
|
42
|
+
TestClass.new
|
43
|
+
Object.new
|
44
|
+
Hash.new
|
45
|
+
end
|
46
|
+
TestClass.new
|
47
|
+
|
48
|
+
Squeal.report.should == 'TestClass: 2'
|
49
|
+
end
|
50
|
+
it 'does not record if squeal.record has not been called' do
|
51
|
+
Hash.new
|
52
|
+
TestClass.new
|
53
|
+
|
54
|
+
Squeal.report.should == ''
|
55
|
+
end
|
56
|
+
it 'reports cumulatively until reset' do
|
57
|
+
Hash.new
|
58
|
+
TestClass.new
|
59
|
+
|
60
|
+
Squeal.report.should == ''
|
61
|
+
|
62
|
+
TestClass.squeal do
|
63
|
+
TestClass.new
|
64
|
+
Hash.new
|
65
|
+
TestClass.new
|
66
|
+
Object.new
|
67
|
+
Hash.new
|
68
|
+
end
|
69
|
+
|
70
|
+
Squeal.report.should == 'TestClass: 2'
|
71
|
+
|
72
|
+
Squeal.record(Object, TestClass) do
|
73
|
+
TestClass.new
|
74
|
+
String.new
|
75
|
+
Object.new
|
76
|
+
TestClass.new
|
77
|
+
String.new
|
78
|
+
end
|
79
|
+
Object.new
|
80
|
+
TestClass.new
|
81
|
+
|
82
|
+
Squeal.report.should == 'Object: 1, TestClass: 4'
|
83
|
+
|
84
|
+
Squeal.reset
|
85
|
+
|
86
|
+
Squeal.report.should == ''
|
87
|
+
end
|
88
|
+
|
89
|
+
end
|
metadata
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: squeal
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 1
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
version: 1.0.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Florian Hanke
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-11-14 00:00:00 +01:00
|
18
|
+
default_executable:
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description: Simple object instantiation recorder.
|
22
|
+
email: florian.hanke+squeal@gmail.com
|
23
|
+
executables: []
|
24
|
+
|
25
|
+
extensions: []
|
26
|
+
|
27
|
+
extra_rdoc_files:
|
28
|
+
- README.textile
|
29
|
+
files:
|
30
|
+
- lib/extensions/class.rb
|
31
|
+
- lib/squeal/squeal.rb
|
32
|
+
- lib/squeal.rb
|
33
|
+
- README.textile
|
34
|
+
- spec/lib/squeal/squeal_spec.rb
|
35
|
+
has_rdoc: true
|
36
|
+
homepage: http://github.com/floere/squeal
|
37
|
+
licenses: []
|
38
|
+
|
39
|
+
post_install_message:
|
40
|
+
rdoc_options:
|
41
|
+
- --charset=UTF-8
|
42
|
+
require_paths:
|
43
|
+
- lib
|
44
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
45
|
+
none: false
|
46
|
+
requirements:
|
47
|
+
- - ">="
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
segments:
|
50
|
+
- 0
|
51
|
+
version: "0"
|
52
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
53
|
+
none: false
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
segments:
|
58
|
+
- 0
|
59
|
+
version: "0"
|
60
|
+
requirements: []
|
61
|
+
|
62
|
+
rubyforge_project:
|
63
|
+
rubygems_version: 1.3.7
|
64
|
+
signing_key:
|
65
|
+
specification_version: 3
|
66
|
+
summary: Records object instantiations.
|
67
|
+
test_files:
|
68
|
+
- spec/lib/squeal/squeal_spec.rb
|