short_inspect 0.0.1

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/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in short_inspect.gemspec
4
+ gemspec
5
+
6
+ gem 'guard'
data/Guardfile ADDED
@@ -0,0 +1,5 @@
1
+ guard 'rspec', :all_after_pass => false do
2
+ watch(%r{^spec/.+_spec\.rb$})
3
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
4
+ end
5
+
data/README.md ADDED
@@ -0,0 +1,26 @@
1
+ # short_inspect
2
+
3
+ Prevent `#inspect` to include unlimited nested instance variables.
4
+
5
+ ## Usage:
6
+
7
+ ```ruby
8
+ require 'short_inspect'
9
+ ShortInspect.apply_to(ActionDispatch::Routing::RouteSet, ActionController::Base)
10
+ ```
11
+
12
+ For auto inclusion in common rails classes:
13
+
14
+ ```ruby
15
+ require 'short_inspect/rails'
16
+ ```
17
+
18
+ For auto inclusion in all ruby classes:
19
+
20
+ ```ruby
21
+ require 'short_inspect/all'
22
+ ```
23
+
24
+ You probably shouldn't apply it to all classes.
25
+
26
+ It's ~450% slower than the built-in `#inspect` method.
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rspec/core/rake_task'
4
+ RSpec::Core::RakeTask.new(:spec)
5
+ task :default => :spec
6
+
@@ -0,0 +1,24 @@
1
+ require 'benchmark'
2
+ include Benchmark
3
+
4
+ $: << './lib'
5
+
6
+ class TwoVariables
7
+ def initialize color, size
8
+ @color = color
9
+ @size = size
10
+ end
11
+ end
12
+ obj = TwoVariables.new(:red, 60)
13
+
14
+ n = 100000
15
+ Benchmark.bm(10) do |x|
16
+
17
+ x.report("original") { n.times do; obj.inspect; end }
18
+
19
+ require 'short_inspect/all'
20
+
21
+ x.report("new") { n.times do; obj.inspect; end }
22
+
23
+ end
24
+
@@ -0,0 +1,3 @@
1
+ require 'short_inspect'
2
+ ShortInspect.apply_to(Object)
3
+
@@ -0,0 +1,5 @@
1
+ require 'short_inspect'
2
+ ShortInspect.apply_minimal_to(
3
+ ActionDispatch::Routing::RouteSet,
4
+ ActionDispatch::Request
5
+ )
@@ -0,0 +1,3 @@
1
+ module ShortInspect
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,78 @@
1
+ require "short_inspect/version"
2
+
3
+ module ShortInspect
4
+ class << self
5
+ attr_accessor :max_nested_inspected_value_length
6
+ end
7
+ self.max_nested_inspected_value_length = 50
8
+
9
+ def self.included(base)
10
+ base.class_eval do
11
+ alias inspect_without_shortening inspect
12
+
13
+ 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
20
+ end
21
+
22
+ def instance_variables_for_inspection
23
+ s = instance_variables.map do |name|
24
+ "#{name}=#{ShortInspect.limit_inspected_value(instance_variable_get(name).inspect)}"
25
+ end.join(", ")
26
+ s = " #{s}" if s.length != 0
27
+ s
28
+ end
29
+ end
30
+ end
31
+
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
40
+ end
41
+ end
42
+
43
+ def self.apply_to *klasses
44
+ Array(klasses).each do |klass|
45
+ klass.send(:include, ShortInspect)
46
+ end
47
+ end
48
+
49
+ def self.apply_minimal_to *klasses
50
+ Array(klasses).each do |klass|
51
+ klass.send(:include, ShortInspect::Minimal)
52
+ end
53
+ end
54
+
55
+ def self.remove_from *klasses
56
+ Array(klasses).each do |klass|
57
+ klass.class_eval do
58
+ alias inspect inspect_without_shortening
59
+ remove_method :inspect_without_shortening
60
+ end
61
+ end
62
+ end
63
+
64
+ module Minimal
65
+ def self.included(base)
66
+ base.class_eval do
67
+ alias inspect_without_shortening inspect
68
+
69
+ def inspect
70
+ "#<%s:0x%x>" % [ self.class.name, object_id << 1 ]
71
+ end
72
+ end
73
+ end
74
+ end
75
+
76
+ end
77
+
78
+
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "short_inspect/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "short_inspect"
7
+ s.version = ShortInspect::VERSION
8
+ s.authors = ["Levente Bagi"]
9
+ s.email = ["levente@picklive.com"]
10
+ s.homepage = "https://github.com/Picklive/short_inspect"
11
+ s.summary = %q{Make the inspect method result more concise}
12
+ s.description = %q{Limit the length of the result of the inspect method}
13
+
14
+ s.rubyforge_project = "short_inspect"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ s.add_development_dependency "rspec"
22
+ end
@@ -0,0 +1,70 @@
1
+ require 'short_inspect'
2
+
3
+ describe ShortInspect do
4
+ describe "inspect" do
5
+
6
+ it "doesn't apply automatically" do
7
+ object = TwoVariables.new(:red, TwoVariables.new(:blue, "extra " * 200))
8
+ object.inspect.length.should > 100
9
+ end
10
+
11
+ context "when applied" do
12
+ before(:each){ ShortInspect.apply_to(Object) }
13
+ after(:each){ ShortInspect.remove_from(Object) }
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
28
+ end
29
+ end
30
+
31
+ end
32
+
33
+ describe ShortInspect::Minimal do
34
+ describe "inspect" do
35
+
36
+ context "when applied" do
37
+ before(:each){ ShortInspect.apply_minimal_to(Object) }
38
+ after(:each){ ShortInspect.remove_from(Object) }
39
+
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')
52
+ object.inspect.should_not include 'red'
53
+ object.inspect.should_not include 'large'
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
59
+
60
+
61
+ class NoVariables
62
+ end
63
+
64
+ class TwoVariables
65
+ def initialize color, size
66
+ @color = color
67
+ @size = size
68
+ end
69
+ end
70
+
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: short_inspect
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Levente Bagi
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-11-23 00:00:00 +00:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rspec
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :development
34
+ version_requirements: *id001
35
+ description: Limit the length of the result of the inspect method
36
+ email:
37
+ - levente@picklive.com
38
+ executables: []
39
+
40
+ extensions: []
41
+
42
+ extra_rdoc_files: []
43
+
44
+ files:
45
+ - .gitignore
46
+ - Gemfile
47
+ - Guardfile
48
+ - README.md
49
+ - Rakefile
50
+ - benchmark/benchmark.rb
51
+ - lib/short_inspect.rb
52
+ - lib/short_inspect/all.rb
53
+ - lib/short_inspect/rails.rb
54
+ - lib/short_inspect/version.rb
55
+ - short_inspect.gemspec
56
+ - spec/short_inspect_spec.rb
57
+ has_rdoc: true
58
+ homepage: https://github.com/Picklive/short_inspect
59
+ licenses: []
60
+
61
+ post_install_message:
62
+ rdoc_options: []
63
+
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ none: false
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ hash: 3
72
+ segments:
73
+ - 0
74
+ version: "0"
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ none: false
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ hash: 3
81
+ segments:
82
+ - 0
83
+ version: "0"
84
+ requirements: []
85
+
86
+ rubyforge_project: short_inspect
87
+ rubygems_version: 1.6.2
88
+ signing_key:
89
+ specification_version: 3
90
+ summary: Make the inspect method result more concise
91
+ test_files:
92
+ - spec/short_inspect_spec.rb