rack-var-dump 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -12,11 +12,11 @@ rack-var-dumpはphpのvar_dump()のようなデバッグ用のメソッドを提
12
12
 
13
13
  Objectにvar_dumpメソッドを提供し、Rackがレスポンスを返すまでにModel,Controller,View,
14
14
 
15
- その他コンテキストで実行されたすべてのレシーバーをviewに自動的に出力します
15
+ その他コンテキストで実行されたvar_dump()のすべてのレシーバーをviewに自動的に出力します
16
16
 
17
17
  出力には michaeldv / awesome_print を使用しています。
18
18
 
19
- <img src="http://cdn-ak.f.st-hatena.com/images/fotolife/b/bunnyhop/20120208/20120208182805.png"/>
19
+ <img src="http://cdn-ak.f.st-hatena.com/images/fotolife/b/bunnyhop/20120208/20120208183913.png"/>
20
20
 
21
21
  ## Supported versions
22
22
 
@@ -73,6 +73,12 @@ var_dumpの返り値は常にselfのため、チェーンが途切れずに使
73
73
 
74
74
  @first_name = User.find(3).var_dump('user.id(3)').first_name
75
75
 
76
+ 引数にハッシュを与えることで、awesome_printのオプションを使用することができます。
77
+
78
+ @users.var_dump("user.all") #=> 通常出力
79
+ @users.var_dump("user.all", :plain => true) #=> plain text(obj.inspect)の出力
80
+ @users.var_dump(:plain => true) #=> subjectは省略してもok
81
+
76
82
  ## Copyright
77
83
 
78
84
  Copyright (c) 2012 Daichi Hirata. See LICENSE for details.
data/Rakefile CHANGED
@@ -3,4 +3,16 @@ require 'rspec/core/rake_task'
3
3
 
4
4
  RSpec::Core::RakeTask.new(:spec)
5
5
 
6
- task :default => :spec
6
+ task :default => :spec
7
+
8
+ def app_task(name)
9
+ task name => [:load_app, "app:db:#{name}"]
10
+ end
11
+
12
+ desc "Debugging tasks for local development"
13
+ task :debug do
14
+ rmdir "pkg"
15
+ sh "rake build"
16
+ sh "gem uninstall rack-var-dump"
17
+ sh "gem install pkg/rack-var-dump"
18
+ end
data/lib/rack/var_dump.rb CHANGED
@@ -2,28 +2,24 @@ require "rack/utils"
2
2
  require 'rack/var_dump/kernel'
3
3
  require 'rack/var_dump/version'
4
4
  require 'rack/var_dump/awesome_print'
5
+ require 'rack/var_dump/response'
6
+ require 'rack/var_dump/variable'
5
7
 
6
8
  module Rack
7
9
  class VarDump
8
10
  @@var_aggregates = []
9
11
 
10
- def self.reset!
11
- @@var_aggregates = []
12
+ def self.var_aggregates
13
+ @@var_aggregates
12
14
  end
13
15
 
14
- def self.var_dump(var, subject)
15
- @@var_aggregates <<
16
- { :var => ai(var, :html => true), :subject => subject }
16
+ def self.reset!
17
+ @@var_aggregates = []
17
18
  end
18
19
 
19
- def self.ai(var, options = {})
20
- ap = AwesomePrint::Inspector.new(options)
21
- ap.instance_eval do
22
- formatter = AwesomePrint::VarDump.new(self)
23
- instance_variable_set(:@formatter, formatter)
24
- end
25
-
26
- ap.awesome(var)
20
+ def self.var_dump(var, subject, options = {})
21
+ default = { :html => true, :plain => false }
22
+ @@var_aggregates << Variable.new(var, subject, default.merge(options))
27
23
  end
28
24
 
29
25
  def initialize(app)
@@ -31,32 +27,8 @@ module Rack
31
27
  end
32
28
 
33
29
  def call(env)
34
- request = Rack::Request.new(env)
35
30
  status, headers, response = @app.call(env)
36
-
37
- if headers["Content-Type"] =~ /^text\/html/ && !@@var_aggregates.empty?
38
- body = ""
39
- response.each {|org_body| body << org_body}
40
- response = [apply(body)] if body =~ /<body.*>/
41
- headers["Content-Length"] = response.join.bytesize.to_s
42
- end
43
-
44
- VarDump.reset!
45
- [status, headers, response]
31
+ Response.call(status, headers, response)
46
32
  end
47
-
48
- private
49
- def apply(body)
50
- html = '<div id="var_dump" style="display:block">'
51
- html << '<pre style="background-color:#eee;padding:10px;font-size:11px;white-space:pre-wrap;color:black!important;">'
52
- @@var_aggregates.each_with_index do |info, n|
53
- html << "var_dump:#{n} #{info[:subject]}\n"
54
- html << info[:var]
55
- html << "\n\n"
56
- end
57
- html << "</pre></div>"
58
-
59
- body.sub(/<body.*>/, '\&' + html)
60
- end
61
33
  end
62
34
  end
@@ -1,6 +1,11 @@
1
1
  module Kernel
2
- def var_dump(subject = nil)
3
- Rack::VarDump.var_dump(self, subject)
2
+ def var_dump(subject = nil, options = {})
3
+ if subject.kind_of?(Hash)
4
+ options = subject
5
+ subject = nil
6
+ end
7
+
8
+ Rack::VarDump.var_dump(self, subject, options)
4
9
  self
5
10
  end
6
11
  end
@@ -0,0 +1,61 @@
1
+ module Rack
2
+ class VarDump
3
+ class Response
4
+ def self.call(*args)
5
+ self.new(*args)._call
6
+ end
7
+
8
+ def initialize(status, headers, response)
9
+ @status = status
10
+ @headers = headers
11
+ @response = response
12
+ @content_body = ""
13
+ @var_aggregates = VarDump.var_aggregates
14
+ end
15
+
16
+ def _call
17
+ if @headers["Content-Type"] =~ /^text\/html/ && !@var_aggregates.empty?
18
+ @response.each do |orign_body|
19
+ @content_body << orign_body
20
+ end
21
+
22
+ @response = [apply_content_body] if @content_body =~ /<body.*>/
23
+ @headers["Content-Length"] = @response.join.bytesize.to_s
24
+ end
25
+
26
+ VarDump.reset!
27
+ [@status, @headers, @response]
28
+ end
29
+
30
+ private
31
+ def apply_content_body
32
+ html = <<-HTML
33
+ <style type="text/css">
34
+ <!--
35
+ #raci-var-dump pre {
36
+ background-color: #eee;
37
+ padding: 10px;
38
+ font-size: 11px;
39
+ white-space: pre-wrap;
40
+ color: black!important;
41
+ }
42
+ -->
43
+ </style>
44
+ <div id="rack-var-dump" style="display:block">
45
+ <pre>
46
+ HTML
47
+
48
+ @var_aggregates.each_with_index do |variable, index|
49
+ html << <<-EOH
50
+ var_dump:#{index} #{variable.subject}
51
+ #{variable.dump}
52
+
53
+ EOH
54
+ end
55
+
56
+ html << "</pre></div>"
57
+ @content_body.sub(/<body.*>/, '\&' + html)
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,33 @@
1
+ module Rack
2
+ class VarDump
3
+ class Variable
4
+ attr_reader :subject
5
+
6
+ def initialize(var, subject, options = {})
7
+ @variable = var
8
+ @subject = subject
9
+ @options = options
10
+ end
11
+
12
+ def dump
13
+ @options[:plain] ? inspect_dump : ai_dump
14
+ end
15
+
16
+ def inspect_dump
17
+ Rack::Utils.escape_html(@variable.inspect)
18
+ end
19
+ private :inspect
20
+
21
+ def ai_dump
22
+ ap = AwesomePrint::Inspector.new(@options)
23
+ ap.instance_eval do
24
+ formatter = AwesomePrint::VarDump.new(self)
25
+ instance_variable_set(:@formatter, formatter)
26
+ end
27
+
28
+ ap.awesome(@variable)
29
+ end
30
+ private :ai_dump
31
+ end
32
+ end
33
+ end
@@ -1,5 +1,5 @@
1
1
  module Rack
2
2
  class VarDump
3
- VERSION = "1.0.0"
3
+ VERSION = "1.0.1"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-var-dump
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-02-08 00:00:00.000000000Z
12
+ date: 2012-02-09 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: awesome_print
16
- requirement: &2158675240 !ruby/object:Gem::Requirement
16
+ requirement: &2154970300 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - =
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 1.0.2
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *2158675240
24
+ version_requirements: *2154970300
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rake
27
- requirement: &2158674720 !ruby/object:Gem::Requirement
27
+ requirement: &2154969520 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: 0.9.2.2
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *2158674720
35
+ version_requirements: *2154969520
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rack
38
- requirement: &2158674180 !ruby/object:Gem::Requirement
38
+ requirement: &2154968680 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: 1.3.5
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *2158674180
46
+ version_requirements: *2154968680
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: rack-test
49
- requirement: &2158673640 !ruby/object:Gem::Requirement
49
+ requirement: &2154968140 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: 0.5.3
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *2158673640
57
+ version_requirements: *2154968140
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: rspec
60
- requirement: &2158673180 !ruby/object:Gem::Requirement
60
+ requirement: &2154967680 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,7 +65,7 @@ dependencies:
65
65
  version: 2.7.0
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *2158673180
68
+ version_requirements: *2154967680
69
69
  description: provides a method for debugging like var_dump() of php.
70
70
  email:
71
71
  - bunny.hop.md@gmail.com
@@ -85,8 +85,9 @@ files:
85
85
  - lib/rack/var_dump/awesome_print/ext/var_dump.rb
86
86
  - lib/rack/var_dump/awesome_print/ext/var_dump.rb.old
87
87
  - lib/rack/var_dump/kernel.rb
88
- - lib/rack/var_dump/object.rb
89
88
  - lib/rack/var_dump/railtie.rb
89
+ - lib/rack/var_dump/response.rb
90
+ - lib/rack/var_dump/variable.rb
90
91
  - lib/rack/var_dump/version.rb
91
92
  - lib/rails-var-dump.rb
92
93
  - lib/rails_var_dump.rb
@@ -1,7 +0,0 @@
1
- module Object
2
- def var_dump(subject = nil)
3
- Rack::VarDump.var_dump(self, subject)
4
-
5
- self
6
- end
7
- end