short_inspect 0.0.1 → 0.0.2
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.md +10 -9
- data/lib/short_inspect.rb +11 -21
- data/lib/short_inspect/rails.rb +4 -1
- data/lib/short_inspect/version.rb +1 -1
- data/spec/short_inspect_spec.rb +13 -36
- metadata +3 -4
- data/lib/short_inspect/all.rb +0 -3
data/README.md
CHANGED
@@ -6,21 +6,22 @@ Prevent `#inspect` to include unlimited nested instance variables.
|
|
6
6
|
|
7
7
|
```ruby
|
8
8
|
require 'short_inspect'
|
9
|
-
ShortInspect.apply_to(ActionDispatch::Routing::RouteSet
|
9
|
+
ShortInspect.apply_to(ActionDispatch::Routing::RouteSet)
|
10
|
+
|
11
|
+
# or:
|
12
|
+
ShortInspect.apply_minimal_to(ActionDispatch::Routing::RouteSet)
|
10
13
|
```
|
11
14
|
|
12
|
-
|
15
|
+
The normal version will include instance variables on the top level with
|
16
|
+
a simple value.
|
13
17
|
|
14
|
-
|
15
|
-
require 'short_inspect/rails'
|
16
|
-
```
|
18
|
+
The minimal version will only include the class name and object id.
|
17
19
|
|
18
|
-
|
20
|
+
|
21
|
+
For auto inclusion in common rails classes:
|
19
22
|
|
20
23
|
```ruby
|
21
|
-
require 'short_inspect/
|
24
|
+
require 'short_inspect/rails'
|
22
25
|
```
|
23
26
|
|
24
|
-
You probably shouldn't apply it to all classes.
|
25
|
-
|
26
27
|
It's ~450% slower than the built-in `#inspect` method.
|
data/lib/short_inspect.rb
CHANGED
@@ -1,42 +1,31 @@
|
|
1
1
|
require "short_inspect/version"
|
2
2
|
|
3
|
+
|
3
4
|
module ShortInspect
|
4
|
-
class << self
|
5
|
-
attr_accessor :max_nested_inspected_value_length
|
6
|
-
end
|
7
|
-
self.max_nested_inspected_value_length = 50
|
8
5
|
|
9
6
|
def self.included(base)
|
10
7
|
base.class_eval do
|
11
8
|
alias inspect_without_shortening inspect
|
12
9
|
|
13
10
|
def inspect
|
14
|
-
s
|
15
|
-
if s[0..1] == '#<' && s[-1..-1] == '>'
|
16
|
-
"#{s[0..-2]}#{instance_variables_for_inspection}#{s[-1..-1]}"
|
17
|
-
else
|
18
|
-
s
|
19
|
-
end
|
11
|
+
"#<%s:0x%x%s>" % [ self.class.name, object_id << 1, instance_variables_for_short_inspect ]
|
20
12
|
end
|
21
13
|
|
22
|
-
def
|
14
|
+
def instance_variables_for_short_inspect
|
23
15
|
s = instance_variables.map do |name|
|
24
|
-
"#{name}=#{ShortInspect.
|
16
|
+
"#{name}=#{ShortInspect.basic_inspect(instance_variable_get(name))}"
|
25
17
|
end.join(", ")
|
26
18
|
s = " #{s}" if s.length != 0
|
27
19
|
s
|
28
20
|
end
|
21
|
+
|
29
22
|
end
|
30
23
|
end
|
31
24
|
|
32
|
-
def self.
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
c1 = max_nested_inspected_value_length - c2 - c3
|
37
|
-
"#{long[0..c1-1]}...#{long[-c3..-1]}"
|
38
|
-
else
|
39
|
-
long
|
25
|
+
def self.basic_inspect obj
|
26
|
+
case obj
|
27
|
+
when String, Fixnum, Float, Symbol, Bignum then obj.inspect
|
28
|
+
else "#<%s:0x%x>" % [ obj.class.name, obj.object_id << 1 ]
|
40
29
|
end
|
41
30
|
end
|
42
31
|
|
@@ -61,13 +50,14 @@ module ShortInspect
|
|
61
50
|
end
|
62
51
|
end
|
63
52
|
|
53
|
+
|
64
54
|
module Minimal
|
65
55
|
def self.included(base)
|
66
56
|
base.class_eval do
|
67
57
|
alias inspect_without_shortening inspect
|
68
58
|
|
69
59
|
def inspect
|
70
|
-
|
60
|
+
ShortInspect.basic_inspect(self)
|
71
61
|
end
|
72
62
|
end
|
73
63
|
end
|
data/lib/short_inspect/rails.rb
CHANGED
data/spec/short_inspect_spec.rb
CHANGED
@@ -4,53 +4,33 @@ describe ShortInspect do
|
|
4
4
|
describe "inspect" do
|
5
5
|
|
6
6
|
it "doesn't apply automatically" do
|
7
|
-
object =
|
8
|
-
object.inspect.
|
7
|
+
object = TestObject.new(:red, TestObject.new(:blue))
|
8
|
+
object.inspect.should include 'blue'
|
9
9
|
end
|
10
10
|
|
11
11
|
context "when applied" do
|
12
12
|
before(:each){ ShortInspect.apply_to(Object) }
|
13
13
|
after(:each){ ShortInspect.remove_from(Object) }
|
14
14
|
|
15
|
-
it "it
|
16
|
-
object =
|
17
|
-
object.inspect.
|
18
|
-
object
|
19
|
-
object.inspect.length.should < 100
|
20
|
-
end
|
21
|
-
|
22
|
-
it "is the same as the original when it's short" do
|
23
|
-
object = NoVariables.new
|
24
|
-
object.inspect.should == object.inspect_without_shortening
|
25
|
-
|
26
|
-
object = TwoVariables.new(:red, :medium)
|
27
|
-
object.inspect.should == object.inspect_without_shortening
|
15
|
+
it "it only includes instance variables on the top level" do
|
16
|
+
object = TestObject.new(:red, TestObject.new(:blue))
|
17
|
+
object.inspect.should include 'red'
|
18
|
+
object.inspect.should_not include 'blue'
|
28
19
|
end
|
29
20
|
end
|
30
21
|
|
31
22
|
end
|
32
23
|
|
33
|
-
describe ShortInspect::Minimal do
|
34
|
-
describe "inspect" do
|
35
24
|
|
25
|
+
describe "ShortInspect::Minimal" do
|
26
|
+
describe "inspect" do
|
36
27
|
context "when applied" do
|
37
28
|
before(:each){ ShortInspect.apply_minimal_to(Object) }
|
38
29
|
after(:each){ ShortInspect.remove_from(Object) }
|
39
30
|
|
40
|
-
it "
|
41
|
-
object =
|
42
|
-
object.inspect.should == object.inspect_without_shortening
|
43
|
-
end
|
44
|
-
|
45
|
-
it "includes class name" do
|
46
|
-
object = TwoVariables.new(:red, 'large')
|
47
|
-
object.inspect.should include 'TwoVariables'
|
48
|
-
end
|
49
|
-
|
50
|
-
it "does not include instance variables at all" do
|
51
|
-
object = TwoVariables.new(:red, 'large')
|
31
|
+
it "it doesn't include instance variables at all" do
|
32
|
+
object = TestObject.new(:red)
|
52
33
|
object.inspect.should_not include 'red'
|
53
|
-
object.inspect.should_not include 'large'
|
54
34
|
end
|
55
35
|
end
|
56
36
|
end
|
@@ -58,13 +38,10 @@ describe ShortInspect do
|
|
58
38
|
end
|
59
39
|
|
60
40
|
|
61
|
-
class
|
62
|
-
|
63
|
-
|
64
|
-
class TwoVariables
|
65
|
-
def initialize color, size
|
41
|
+
class TestObject
|
42
|
+
def initialize color, child = nil
|
66
43
|
@color = color
|
67
|
-
@
|
44
|
+
@child = child
|
68
45
|
end
|
69
46
|
end
|
70
47
|
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: short_inspect
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 27
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 2
|
10
|
+
version: 0.0.2
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Levente Bagi
|
@@ -49,7 +49,6 @@ files:
|
|
49
49
|
- Rakefile
|
50
50
|
- benchmark/benchmark.rb
|
51
51
|
- lib/short_inspect.rb
|
52
|
-
- lib/short_inspect/all.rb
|
53
52
|
- lib/short_inspect/rails.rb
|
54
53
|
- lib/short_inspect/version.rb
|
55
54
|
- short_inspect.gemspec
|
data/lib/short_inspect/all.rb
DELETED