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 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, ActionController::Base)
9
+ ShortInspect.apply_to(ActionDispatch::Routing::RouteSet)
10
+
11
+ # or:
12
+ ShortInspect.apply_minimal_to(ActionDispatch::Routing::RouteSet)
10
13
  ```
11
14
 
12
- For auto inclusion in common rails classes:
15
+ The normal version will include instance variables on the top level with
16
+ a simple value.
13
17
 
14
- ```ruby
15
- require 'short_inspect/rails'
16
- ```
18
+ The minimal version will only include the class name and object id.
17
19
 
18
- For auto inclusion in all ruby classes:
20
+
21
+ For auto inclusion in common rails classes:
19
22
 
20
23
  ```ruby
21
- require 'short_inspect/all'
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 = to_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 instance_variables_for_inspection
14
+ def instance_variables_for_short_inspect
23
15
  s = instance_variables.map do |name|
24
- "#{name}=#{ShortInspect.limit_inspected_value(instance_variable_get(name).inspect)}"
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.limit_inspected_value(long)
33
- if long.length > max_nested_inspected_value_length
34
- c3 = 5 # chars to keep at end
35
- c2 = 3 # "..."
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
- "#<%s:0x%x>" % [ self.class.name, object_id << 1 ]
60
+ ShortInspect.basic_inspect(self)
71
61
  end
72
62
  end
73
63
  end
@@ -1,5 +1,8 @@
1
1
  require 'short_inspect'
2
2
  ShortInspect.apply_minimal_to(
3
3
  ActionDispatch::Routing::RouteSet,
4
- ActionDispatch::Request
4
+ ActionDispatch::Request,
5
+ ActionView::OutputFlow,
6
+ ActionDispatch::Request,
7
+ ActionView::LookupContext
5
8
  )
@@ -1,3 +1,3 @@
1
1
  module ShortInspect
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -4,53 +4,33 @@ describe ShortInspect do
4
4
  describe "inspect" do
5
5
 
6
6
  it "doesn't apply automatically" do
7
- object = TwoVariables.new(:red, TwoVariables.new(:blue, "extra " * 200))
8
- object.inspect.length.should > 100
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 limits the size of long instance variables" do
16
- object = TwoVariables.new(:red, TwoVariables.new(:blue, "extra " * 200))
17
- object.inspect.length.should < 100
18
- object = TwoVariables.new(:red, "extra " * 20)
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 "is the same as the original when it's short" do
41
- object = NoVariables.new
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 NoVariables
62
- end
63
-
64
- class TwoVariables
65
- def initialize color, size
41
+ class TestObject
42
+ def initialize color, child = nil
66
43
  @color = color
67
- @size = size
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: 29
4
+ hash: 27
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 1
10
- version: 0.0.1
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
@@ -1,3 +0,0 @@
1
- require 'short_inspect'
2
- ShortInspect.apply_to(Object)
3
-