ryansch-awesome_print 1.0.2.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 +22 -0
- data/CHANGELOG +77 -0
- data/Gemfile +3 -0
- data/Gemfile.lock +26 -0
- data/LICENSE +22 -0
- data/README.md +339 -0
- data/Rakefile +2 -0
- data/lib/ap.rb +10 -0
- data/lib/awesome_print/core_ext/array.rb +71 -0
- data/lib/awesome_print/core_ext/class.rb +22 -0
- data/lib/awesome_print/core_ext/kernel.rb +21 -0
- data/lib/awesome_print/core_ext/logger.rb +20 -0
- data/lib/awesome_print/core_ext/method.rb +21 -0
- data/lib/awesome_print/core_ext/object.rb +22 -0
- data/lib/awesome_print/core_ext/string.rb +31 -0
- data/lib/awesome_print/ext/action_view.rb +18 -0
- data/lib/awesome_print/ext/active_record.rb +64 -0
- data/lib/awesome_print/ext/active_support.rb +47 -0
- data/lib/awesome_print/ext/mongo_mapper.rb +38 -0
- data/lib/awesome_print/ext/mongoid.rb +65 -0
- data/lib/awesome_print/ext/nokogiri.rb +45 -0
- data/lib/awesome_print/formatter.rb +384 -0
- data/lib/awesome_print/inspector.rb +140 -0
- data/lib/awesome_print/version.rb +10 -0
- data/lib/awesome_print.rb +30 -0
- data/spec/colors_spec.rb +106 -0
- data/spec/formats_spec.rb +730 -0
- data/spec/methods_spec.rb +458 -0
- data/spec/objects_spec.rb +79 -0
- data/spec/spec_helper.rb +51 -0
- metadata +133 -0
data/.gitignore
ADDED
data/CHANGELOG
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
1.0.2
|
2
|
+
- Added formatting of Mongoid documents (Adam Doppelt)
|
3
|
+
- ActiveRecord objects display attributes only. Use :raw => true to display the entire object
|
4
|
+
- ActiveSupport::Date objects get formatted as regular Date
|
5
|
+
- Rails.logger.ap colorizes output based on ActiveSupport::LogSubscriber.colorize_logging (default is true)
|
6
|
+
- Improved formatting of methods array
|
7
|
+
|
8
|
+
1.0.1
|
9
|
+
- Updated repo tags for Rubygems.org
|
10
|
+
|
11
|
+
1.0.0 Thanksgiving edition
|
12
|
+
- Added ability to format *arbitrary* Ruby object
|
13
|
+
- Added :limit option to limit large output for arrays and hashes (Andrew Horsman)
|
14
|
+
- Improved HTML formatting when :html => true (Daniel Johnson)
|
15
|
+
- Added Mongoid extension (Adam Doppelt)
|
16
|
+
- Added Nokogiri extension (Adam Doppelt)
|
17
|
+
- Removed Jeweler gem dependency
|
18
|
+
|
19
|
+
0.4.0
|
20
|
+
- 'ap object' now returns the object (Stephan Hagemann)
|
21
|
+
- Added :html => true option to enable HTML colors rather that ANSI (ex. Sinatra templates)
|
22
|
+
- Added AwesomePrint.force_colors! to allow color output on demand (Andrew O'Brien)
|
23
|
+
- Added MongoMapper formatter mixin (Elpizo Choi)
|
24
|
+
- Fixed formatting of methods array when object#method is overridden
|
25
|
+
- Fixed potential stack errors by checking whether AwesomePrint is already loaded
|
26
|
+
- Improved Ruby 1.8.6 and 1.8.7 compatibility
|
27
|
+
- Improved Windows compatibility (Viktar Basharymau)
|
28
|
+
|
29
|
+
0.3.2
|
30
|
+
- Make sure Rails mixins get loaded in Rails console when required from .irbrc
|
31
|
+
- Fixed an issue with classes that define their own #send method (ex: Socket)
|
32
|
+
- Fixed compatibility issue with Liquid gem that defines Module#liquid_methods
|
33
|
+
- Fixed hash spec for Ruby < 1.9 where order of hash keys is not guaranteed
|
34
|
+
- Added :sorted_hash_keys option to sort hash keys (Ed Ruder)
|
35
|
+
|
36
|
+
0.3.1 RubyConf X edition
|
37
|
+
- Fixed Ruby 1.8.6 compatibility issues (thanks, Tim!)
|
38
|
+
- Fixed stack overflow issue with Rails 2.3.x console
|
39
|
+
|
40
|
+
0.3.0
|
41
|
+
- Display object.methods and family in human readable format
|
42
|
+
- Objects inherited from Array, Hash, File, Dir, and Struct are shown as their base class
|
43
|
+
- Added option to suppress array index in output (Sean Gallagher)
|
44
|
+
- Updated README on how to set up ~/.irbrc for MacRuby (Eloy Duran)
|
45
|
+
- Specs pass 100% with Ruby 1.8.7/RSpec 1.3 and Ruby 1.9.2/RSpec 2.0
|
46
|
+
|
47
|
+
0.2.1
|
48
|
+
- ap can now be used within Rails templates (ex. <%= ap object %>)
|
49
|
+
- Added support for printing Struct
|
50
|
+
|
51
|
+
0.2.0
|
52
|
+
- Added support for logger.ap (including Rails logger)
|
53
|
+
- Added support for HashWithIndifferentAccess from ActiveSupport
|
54
|
+
- ap now works with scripts that use ActiveRecord/ActiveSupport outside Rails
|
55
|
+
- ap now correctly shows file and directory names with fancy characters (shell escape)
|
56
|
+
|
57
|
+
0.1.4
|
58
|
+
- Format BigDecimal and Rational objects as Float scalars
|
59
|
+
- Explicit options parameter can override custom defaults
|
60
|
+
- Custom defaults are not interfering when running specs
|
61
|
+
- Custom defaults now work correctly with Ruby 1.9.x
|
62
|
+
|
63
|
+
0.1.3
|
64
|
+
- Added support for setting custom defaults in ~/.aprc
|
65
|
+
|
66
|
+
0.1.2
|
67
|
+
- Correctly handle empty arrays and hashes
|
68
|
+
- Use alias_method instead of alias (fixes non-tty method aliasing)
|
69
|
+
- Added awesome_inspect method
|
70
|
+
|
71
|
+
0.1.1
|
72
|
+
- Added support for tableless ActiveRecord models
|
73
|
+
- Left align hash keys if @options[:indent] is negative
|
74
|
+
|
75
|
+
0.1.0
|
76
|
+
- Initial Release.
|
77
|
+
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
awesome_print (1.0.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: http://rubygems.org/
|
8
|
+
specs:
|
9
|
+
diff-lcs (1.1.2)
|
10
|
+
fakefs (0.3.2)
|
11
|
+
rspec (2.6.0)
|
12
|
+
rspec-core (~> 2.6.0)
|
13
|
+
rspec-expectations (~> 2.6.0)
|
14
|
+
rspec-mocks (~> 2.6.0)
|
15
|
+
rspec-core (2.6.4)
|
16
|
+
rspec-expectations (2.6.0)
|
17
|
+
diff-lcs (~> 1.1.2)
|
18
|
+
rspec-mocks (2.6.0)
|
19
|
+
|
20
|
+
PLATFORMS
|
21
|
+
ruby
|
22
|
+
|
23
|
+
DEPENDENCIES
|
24
|
+
awesome_print!
|
25
|
+
fakefs (>= 0.2.1)
|
26
|
+
rspec (>= 2.6.0)
|
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2010-2011 Michael Dvorkin
|
2
|
+
twitter.com/mid
|
3
|
+
%w(mike dvorkin.net) * "@" || %w(mike fatfreecrm.com) * "@"
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,339 @@
|
|
1
|
+
## Awesome Print ##
|
2
|
+
Awesome Print is a Ruby library that pretty prints Ruby objects in full color
|
3
|
+
exposing their internal structure with proper indentation. Rails ActiveRecord
|
4
|
+
objects and usage within Rails templates are supported via included mixins.
|
5
|
+
|
6
|
+
### Installation ###
|
7
|
+
# Installing as Ruby gem
|
8
|
+
$ gem install awesome_print
|
9
|
+
|
10
|
+
# Cloning the repository
|
11
|
+
$ git clone git://github.com/michaeldv/awesome_print.git
|
12
|
+
|
13
|
+
### Usage ###
|
14
|
+
|
15
|
+
require "awesome_print"
|
16
|
+
ap object, options = {}
|
17
|
+
|
18
|
+
Default options:
|
19
|
+
|
20
|
+
:indent => 4, # Indent using 4 spaces.
|
21
|
+
:index => true, # Display array indices.
|
22
|
+
:html => false, # Use ANSI color codes rather than HTML.
|
23
|
+
:multiline => true, # Display in multiple lines.
|
24
|
+
:plain => false, # Use colors.
|
25
|
+
:sort_keys => false, # Do not sort hash keys.
|
26
|
+
:limit => false, # Limit large output for arrays and hashes. Set to a boolean or integer.
|
27
|
+
:color => {
|
28
|
+
:args => :pale,
|
29
|
+
:array => :white,
|
30
|
+
:bigdecimal => :blue,
|
31
|
+
:class => :yellow,
|
32
|
+
:date => :greenish,
|
33
|
+
:falseclass => :red,
|
34
|
+
:fixnum => :blue,
|
35
|
+
:float => :blue,
|
36
|
+
:hash => :pale,
|
37
|
+
:keyword => :cyan,
|
38
|
+
:method => :purpleish,
|
39
|
+
:nilclass => :red,
|
40
|
+
:string => :yellowish,
|
41
|
+
:struct => :pale,
|
42
|
+
:symbol => :cyanish,
|
43
|
+
:time => :greenish,
|
44
|
+
:trueclass => :green,
|
45
|
+
:variable => :cyanish
|
46
|
+
}
|
47
|
+
|
48
|
+
Supported color names:
|
49
|
+
|
50
|
+
:gray, :red, :green, :yellow, :blue, :purple, :cyan, :white
|
51
|
+
:black, :redish, :greenish, :yellowish, :blueish, :purpleish, :cyanish, :pale
|
52
|
+
|
53
|
+
### Examples ###
|
54
|
+
$ cat > 1.rb
|
55
|
+
require "awesome_print"
|
56
|
+
data = [ false, 42, %w(forty two), { :now => Time.now, :class => Time.now.class, :distance => 42e42 } ]
|
57
|
+
ap data
|
58
|
+
^D
|
59
|
+
$ ruby 1.rb
|
60
|
+
[
|
61
|
+
[0] false,
|
62
|
+
[1] 42,
|
63
|
+
[2] [
|
64
|
+
[0] "forty",
|
65
|
+
[1] "two"
|
66
|
+
],
|
67
|
+
[3] {
|
68
|
+
:class => Time < Object,
|
69
|
+
:now => Fri Apr 02 19:55:53 -0700 2010,
|
70
|
+
:distance => 4.2e+43
|
71
|
+
}
|
72
|
+
]
|
73
|
+
|
74
|
+
$ cat > 2.rb
|
75
|
+
require "awesome_print"
|
76
|
+
data = { :now => Time.now, :class => Time.now.class, :distance => 42e42 }
|
77
|
+
ap data, :indent => -2 # <-- Left align hash keys.
|
78
|
+
^D
|
79
|
+
$ ruby 2.rb
|
80
|
+
{
|
81
|
+
:class => Time < Object,
|
82
|
+
:now => Fri Apr 02 19:55:53 -0700 2010,
|
83
|
+
:distance => 4.2e+43
|
84
|
+
}
|
85
|
+
|
86
|
+
$ cat > 3.rb
|
87
|
+
require "awesome_print"
|
88
|
+
data = [ false, 42, %w(forty two) ]
|
89
|
+
data << data # <-- Nested array.
|
90
|
+
ap data, :multiline => false
|
91
|
+
^D
|
92
|
+
$ ruby 3.rb
|
93
|
+
[ false, 42, [ "forty", "two" ], [...] ]
|
94
|
+
|
95
|
+
$ cat > 4.rb
|
96
|
+
require "awesome_print"
|
97
|
+
class Hello
|
98
|
+
def self.world(x, y, z = nil, &blk)
|
99
|
+
end
|
100
|
+
end
|
101
|
+
ap Hello.methods - Class.methods
|
102
|
+
^D
|
103
|
+
$ ruby 4.rb
|
104
|
+
[
|
105
|
+
[0] world(x, y, *z, &blk) Hello
|
106
|
+
]
|
107
|
+
|
108
|
+
$ cat > 5.rb
|
109
|
+
require "awesome_print"
|
110
|
+
ap (''.methods - Object.methods).grep(/!/)
|
111
|
+
^D
|
112
|
+
$ ruby 5.rb
|
113
|
+
[
|
114
|
+
[ 0] capitalize!() String
|
115
|
+
[ 1] chomp!(*arg1) String
|
116
|
+
[ 2] chop!() String
|
117
|
+
[ 3] delete!(*arg1) String
|
118
|
+
[ 4] downcase!() String
|
119
|
+
[ 5] encode!(*arg1) String
|
120
|
+
[ 6] gsub!(*arg1) String
|
121
|
+
[ 7] lstrip!() String
|
122
|
+
[ 8] next!() String
|
123
|
+
[ 9] reverse!() String
|
124
|
+
[10] rstrip!() String
|
125
|
+
[11] slice!(*arg1) String
|
126
|
+
[12] squeeze!(*arg1) String
|
127
|
+
[13] strip!() String
|
128
|
+
[14] sub!(*arg1) String
|
129
|
+
[15] succ!() String
|
130
|
+
[16] swapcase!() String
|
131
|
+
[17] tr!(arg1, arg2) String
|
132
|
+
[18] tr_s!(arg1, arg2) String
|
133
|
+
[19] upcase!() String
|
134
|
+
]
|
135
|
+
|
136
|
+
$ cat > 6.rb
|
137
|
+
require "awesome_print"
|
138
|
+
ap 42 == ap(42)
|
139
|
+
^D
|
140
|
+
$ ruby 6.rb
|
141
|
+
42
|
142
|
+
true
|
143
|
+
$ cat 7.rb
|
144
|
+
require "awesome_print"
|
145
|
+
some_array = (1..1000).to_a
|
146
|
+
ap some_array, :limit => true
|
147
|
+
^D
|
148
|
+
$ ruby 7.rb
|
149
|
+
[
|
150
|
+
[ 0] 1,
|
151
|
+
[ 1] 2,
|
152
|
+
[ 2] 3,
|
153
|
+
[ 3] .. [996],
|
154
|
+
[997] 998,
|
155
|
+
[998] 999,
|
156
|
+
[999] 1000
|
157
|
+
]
|
158
|
+
|
159
|
+
$ cat 8.rb
|
160
|
+
require "awesome_print"
|
161
|
+
some_array = (1..1000).to_a
|
162
|
+
ap some_array, :limit => 5
|
163
|
+
^D
|
164
|
+
$ ruby 8.rb
|
165
|
+
[
|
166
|
+
[ 0] 1,
|
167
|
+
[ 1] 2,
|
168
|
+
[ 2] .. [997],
|
169
|
+
[998] 999,
|
170
|
+
[999] 1000
|
171
|
+
]
|
172
|
+
|
173
|
+
### Example (Rails console) ###
|
174
|
+
$ rails console
|
175
|
+
rails> require "awesome_print"
|
176
|
+
rails> ap Account.all(:limit => 2)
|
177
|
+
[
|
178
|
+
[0] #<Account:0x1033220b8> {
|
179
|
+
:id => 1,
|
180
|
+
:user_id => 5,
|
181
|
+
:assigned_to => 7,
|
182
|
+
:name => "Hayes-DuBuque",
|
183
|
+
:access => "Public",
|
184
|
+
:website => "http://www.hayesdubuque.com",
|
185
|
+
:toll_free_phone => "1-800-932-6571",
|
186
|
+
:phone => "(111)549-5002",
|
187
|
+
:fax => "(349)415-2266",
|
188
|
+
:deleted_at => nil,
|
189
|
+
:created_at => Sat, 06 Mar 2010 09:46:10 UTC +00:00,
|
190
|
+
:updated_at => Sat, 06 Mar 2010 16:33:10 UTC +00:00,
|
191
|
+
:email => "info@hayesdubuque.com",
|
192
|
+
:background_info => nil
|
193
|
+
},
|
194
|
+
[1] #<Account:0x103321ff0> {
|
195
|
+
:id => 2,
|
196
|
+
:user_id => 4,
|
197
|
+
:assigned_to => 4,
|
198
|
+
:name => "Ziemann-Streich",
|
199
|
+
:access => "Public",
|
200
|
+
:website => "http://www.ziemannstreich.com",
|
201
|
+
:toll_free_phone => "1-800-871-0619",
|
202
|
+
:phone => "(042)056-1534",
|
203
|
+
:fax => "(106)017-8792",
|
204
|
+
:deleted_at => nil,
|
205
|
+
:created_at => Tue, 09 Feb 2010 13:32:10 UTC +00:00,
|
206
|
+
:updated_at => Tue, 09 Feb 2010 20:05:01 UTC +00:00,
|
207
|
+
:email => "info@ziemannstreich.com",
|
208
|
+
:background_info => nil
|
209
|
+
}
|
210
|
+
]
|
211
|
+
rails> ap Account
|
212
|
+
class Account < ActiveRecord::Base {
|
213
|
+
:id => :integer,
|
214
|
+
:user_id => :integer,
|
215
|
+
:assigned_to => :integer,
|
216
|
+
:name => :string,
|
217
|
+
:access => :string,
|
218
|
+
:website => :string,
|
219
|
+
:toll_free_phone => :string,
|
220
|
+
:phone => :string,
|
221
|
+
:fax => :string,
|
222
|
+
:deleted_at => :datetime,
|
223
|
+
:created_at => :datetime,
|
224
|
+
:updated_at => :datetime,
|
225
|
+
:email => :string,
|
226
|
+
:background_info => :string
|
227
|
+
}
|
228
|
+
rails>
|
229
|
+
|
230
|
+
### IRB integration ###
|
231
|
+
To use awesome_print as default formatter in irb and Rails console add the following
|
232
|
+
code to your ~/.irbrc file:
|
233
|
+
|
234
|
+
require "rubygems"
|
235
|
+
require "awesome_print"
|
236
|
+
|
237
|
+
unless IRB.version.include?('DietRB')
|
238
|
+
IRB::Irb.class_eval do
|
239
|
+
def output_value
|
240
|
+
ap @context.last_value
|
241
|
+
end
|
242
|
+
end
|
243
|
+
else # MacRuby
|
244
|
+
IRB.formatter = Class.new(IRB::Formatter) do
|
245
|
+
def inspect_object(object)
|
246
|
+
object.ai
|
247
|
+
end
|
248
|
+
end.new
|
249
|
+
end
|
250
|
+
|
251
|
+
### PRY integration ###
|
252
|
+
If you miss awesome_print's way of formatting output, here's how you can use it in place
|
253
|
+
of the formatting which comes with pry. Add the following code to your ~/.pryrc:
|
254
|
+
|
255
|
+
require "rubygems"
|
256
|
+
require "awesome_print"
|
257
|
+
|
258
|
+
Pry.print = proc { |output, value| output.puts value.ai }
|
259
|
+
|
260
|
+
### Logger Convenience Method ###
|
261
|
+
awesome_print adds the 'ap' method to the Logger and ActiveSupport::BufferedLogger classes
|
262
|
+
letting you call:
|
263
|
+
|
264
|
+
logger.ap object
|
265
|
+
|
266
|
+
By default, this logs at the :debug level. You can override that globally with:
|
267
|
+
|
268
|
+
:log_level => :info
|
269
|
+
|
270
|
+
in the custom defaults (see below). You can also override on a per call basis with:
|
271
|
+
|
272
|
+
logger.ap object, :warn
|
273
|
+
|
274
|
+
### ActionView Convenience Method ###
|
275
|
+
awesome_print adds the 'ap' method to the ActionView::Base class making it available
|
276
|
+
within Rails templates. For example:
|
277
|
+
|
278
|
+
<%= ap @accounts.first %> # ERB
|
279
|
+
!= ap @accounts.first # HAML
|
280
|
+
|
281
|
+
With other web frameworks (ex: in Sinatra templates) you can explicitly request HTML
|
282
|
+
formatting:
|
283
|
+
|
284
|
+
<%= ap @accounts.first, :html => true %>
|
285
|
+
|
286
|
+
### Setting Custom Defaults ###
|
287
|
+
You can set your own default options by creating ``.aprc`` file in your home
|
288
|
+
directory. Within that file assign your defaults to ``AwesomePrint.defaults``.
|
289
|
+
For example:
|
290
|
+
|
291
|
+
# ~/.aprc file.
|
292
|
+
AwesomePrint.defaults = {
|
293
|
+
:indent => -2,
|
294
|
+
:color => {
|
295
|
+
:hash => :pale,
|
296
|
+
:class => :white
|
297
|
+
}
|
298
|
+
}
|
299
|
+
|
300
|
+
### Running Specs ###
|
301
|
+
|
302
|
+
$ gem install rspec # RSpec 2.x is the requirement.
|
303
|
+
$ rake spec # Run the entire spec suite.
|
304
|
+
$ rspec spec/logger_spec.rb # Run individual spec file.
|
305
|
+
|
306
|
+
### Note on Patches/Pull Requests ###
|
307
|
+
* Fork the project on Github.
|
308
|
+
* Make your feature addition or bug fix.
|
309
|
+
* Add specs for it, making sure $ rake spec is all green.
|
310
|
+
* Commit, do not mess with rakefile, version, or history.
|
311
|
+
* Send me a pull request.
|
312
|
+
|
313
|
+
### Contributors ###
|
314
|
+
|
315
|
+
* Adam Doppelt -- https://github.com/gurgeous
|
316
|
+
* Andrew O'Brien -- https://github.com/AndrewO
|
317
|
+
* Andrew Horsman -- https://github.com/basicxman
|
318
|
+
* Benoit Daloze -- http://github.com/eregon
|
319
|
+
* Brandon Zylstra -- https://github.com/brandondrew
|
320
|
+
* Daniel Johnson -- https://github.com/adhd360
|
321
|
+
* Daniel Bretoi -- http://github.com/danielb2
|
322
|
+
* Eloy Duran -- http://github.com/alloy
|
323
|
+
* Elpizo Choi -- https://github.com/fuJiin
|
324
|
+
* Greg Weber -- https://github.com/gregwebs
|
325
|
+
* Jeff Felchner -- https://github.com/jfelchner
|
326
|
+
* Sean Gallagher -- http://github.com/torandu
|
327
|
+
* Stephan Hagemann -- https://github.com/shageman
|
328
|
+
* Tim Harper -- http://github.com/timcharper
|
329
|
+
* Tobias Crawley -- http://github.com/tobias
|
330
|
+
* Viktar Basharymau -- https://github.com/DNNX
|
331
|
+
|
332
|
+
### License ###
|
333
|
+
Copyright (c) 2010-2011 Michael Dvorkin
|
334
|
+
|
335
|
+
twitter.com/mid
|
336
|
+
|
337
|
+
%w(mike dvorkin.net) * "@" || %w(mike fatfreecrm.com) * "@"
|
338
|
+
|
339
|
+
Released under the MIT license. See LICENSE file for details.
|
data/Rakefile
ADDED
data/lib/ap.rb
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
# Copyright (c) 2010-2011 Michael Dvorkin
|
2
|
+
#
|
3
|
+
# Awesome Print is freely distributable under the terms of MIT license.
|
4
|
+
# See LICENSE file or http://www.opensource.org/licenses/mit-license.php
|
5
|
+
#------------------------------------------------------------------------------
|
6
|
+
#
|
7
|
+
# Keeping this for backwards compatibility to allow
|
8
|
+
# require "ap"
|
9
|
+
#
|
10
|
+
require File.dirname(__FILE__) + "/awesome_print"
|
@@ -0,0 +1,71 @@
|
|
1
|
+
# Copyright (c) 2010-2011 Michael Dvorkin
|
2
|
+
#
|
3
|
+
# Awesome Print is freely distributable under the terms of MIT license.
|
4
|
+
# See LICENSE file or http://www.opensource.org/licenses/mit-license.php
|
5
|
+
#------------------------------------------------------------------------------
|
6
|
+
#
|
7
|
+
# The following makes it possible to invoke awesome_print while performing
|
8
|
+
# operations on method arrays, ex:
|
9
|
+
#
|
10
|
+
# ap [].methods - Object.methods
|
11
|
+
# ap ''.methods.grep(/!|\?/)
|
12
|
+
#
|
13
|
+
# If you could think of a better way please let me know :-)
|
14
|
+
#
|
15
|
+
class Array #:nodoc:
|
16
|
+
[ :-, :& ].each do |operator|
|
17
|
+
original_operator = instance_method(operator)
|
18
|
+
|
19
|
+
define_method operator do |*args|
|
20
|
+
arr = original_operator.bind(self).call(*args)
|
21
|
+
if self.instance_variable_defined?('@__awesome_methods__')
|
22
|
+
arr.instance_variable_set('@__awesome_methods__', self.instance_variable_get('@__awesome_methods__'))
|
23
|
+
arr.sort! { |a, b| a.to_s <=> b.to_s } # Need the block since Ruby 1.8.x can't sort arrays of symbols.
|
24
|
+
end
|
25
|
+
arr
|
26
|
+
end
|
27
|
+
end
|
28
|
+
#
|
29
|
+
# Intercepting Array#grep needs a special treatment since grep accepts
|
30
|
+
# an optional block.
|
31
|
+
#
|
32
|
+
alias :original_grep :grep
|
33
|
+
def grep(pattern, &blk)
|
34
|
+
#
|
35
|
+
# The following looks rather insane and I've sent numerous hours trying
|
36
|
+
# to figure it out. The problem is that if grep gets called with the
|
37
|
+
# block, for example:
|
38
|
+
#
|
39
|
+
# [].methods.grep(/(.+?)_by/) { $1.to_sym }
|
40
|
+
#
|
41
|
+
# ...then simple:
|
42
|
+
#
|
43
|
+
# original_grep(pattern, &blk)
|
44
|
+
#
|
45
|
+
# doesn't set $1 within the grep block which causes nil.to_sym failure.
|
46
|
+
# The workaround below has been tested with Ruby 1.8.7/Rails 2.3.8 and
|
47
|
+
# Ruby 1.9.2/Rails 3.0.0. For more info see the following thread dating
|
48
|
+
# back to 2003 when Ruby 1.8.0 was as fresh off the grill as Ruby 1.9.2
|
49
|
+
# is in 2010 :-)
|
50
|
+
#
|
51
|
+
# http://www.justskins.com/forums/bug-when-rerouting-string-52852.html
|
52
|
+
#
|
53
|
+
# BTW, if you figure out a better way of intercepting Array#grep please
|
54
|
+
# let me know: twitter.com/mid -- or just say hi so I know you've read
|
55
|
+
# the comment :-)
|
56
|
+
#
|
57
|
+
arr = unless blk
|
58
|
+
original_grep(pattern)
|
59
|
+
else
|
60
|
+
original_grep(pattern) do |match|
|
61
|
+
eval("%Q/#{match.to_s.gsub('/', '\/')}/ =~ #{pattern.inspect}", blk.binding)
|
62
|
+
yield match
|
63
|
+
end
|
64
|
+
end
|
65
|
+
if self.instance_variable_defined?('@__awesome_methods__')
|
66
|
+
arr.instance_variable_set('@__awesome_methods__', self.instance_variable_get('@__awesome_methods__'))
|
67
|
+
arr.reject! { |item| !(item.is_a?(Symbol) || item.is_a?(String)) } # grep block might return crap.
|
68
|
+
end
|
69
|
+
arr
|
70
|
+
end
|
71
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# Copyright (c) 2010-2011 Michael Dvorkin
|
2
|
+
#
|
3
|
+
# Awesome Print is freely distributable under the terms of MIT license.
|
4
|
+
# See LICENSE file or http://www.opensource.org/licenses/mit-license.php
|
5
|
+
#------------------------------------------------------------------------------
|
6
|
+
class Class #:nodoc:
|
7
|
+
#
|
8
|
+
# Intercept methods below to inject @__awesome_print__ instance variable
|
9
|
+
# so we know it is the *methods* array when formatting an array.
|
10
|
+
#
|
11
|
+
# Remaining public/private etc. '_methods' are handled in core_ext/object.rb.
|
12
|
+
#
|
13
|
+
%w(instance_methods private_instance_methods protected_instance_methods public_instance_methods).each do |name|
|
14
|
+
original_method = instance_method(name)
|
15
|
+
|
16
|
+
define_method name do |*args|
|
17
|
+
methods = original_method.bind(self).call(*args)
|
18
|
+
methods.instance_variable_set('@__awesome_methods__', self)
|
19
|
+
methods
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# Copyright (c) 2010-2011 Michael Dvorkin
|
2
|
+
#
|
3
|
+
# Awesome Print is freely distributable under the terms of MIT license.
|
4
|
+
# See LICENSE file or http://www.opensource.org/licenses/mit-license.php
|
5
|
+
#------------------------------------------------------------------------------
|
6
|
+
module Kernel
|
7
|
+
|
8
|
+
def ai(options = {})
|
9
|
+
ap = AwesomePrint::Inspector.new(options)
|
10
|
+
ap.awesome self
|
11
|
+
end
|
12
|
+
alias :awesome_inspect :ai
|
13
|
+
|
14
|
+
def ap(object, options = {})
|
15
|
+
puts object.ai(options)
|
16
|
+
object
|
17
|
+
end
|
18
|
+
alias :awesome_print :ap
|
19
|
+
|
20
|
+
module_function :ap
|
21
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# Copyright (c) 2010-2011 Michael Dvorkin
|
2
|
+
#
|
3
|
+
# Awesome Print is freely distributable under the terms of MIT license.
|
4
|
+
# See LICENSE file or http://www.opensource.org/licenses/mit-license.php
|
5
|
+
#------------------------------------------------------------------------------
|
6
|
+
module AwesomePrint
|
7
|
+
module Logger
|
8
|
+
|
9
|
+
# Add ap method to logger
|
10
|
+
#------------------------------------------------------------------------------
|
11
|
+
def ap(object, level = nil)
|
12
|
+
level ||= AwesomePrint.defaults[:log_level] if AwesomePrint.defaults
|
13
|
+
level ||= :debug
|
14
|
+
send level, object.ai
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
Logger.send(:include, AwesomePrint::Logger)
|
20
|
+
ActiveSupport::BufferedLogger.send(:include, AwesomePrint::Logger) if defined?(ActiveSupport::BufferedLogger)
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# Copyright (c) 2010-2011 Michael Dvorkin
|
2
|
+
#
|
3
|
+
# Awesome Print is freely distributable under the terms of MIT license.
|
4
|
+
# See LICENSE file or http://www.opensource.org/licenses/mit-license.php
|
5
|
+
#------------------------------------------------------------------------------
|
6
|
+
#
|
7
|
+
# Method#name was intorduced in Ruby 1.8.7 so we define it here as necessary.
|
8
|
+
#
|
9
|
+
unless nil.method(:class).respond_to?(:name)
|
10
|
+
class Method
|
11
|
+
def name
|
12
|
+
inspect.split(/[#.>]/)[-1]
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
class UnboundMethod
|
17
|
+
def name
|
18
|
+
inspect.split(/[#.>]/)[-1]
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# Copyright (c) 2010-2011 Michael Dvorkin
|
2
|
+
#
|
3
|
+
# Awesome Print is freely distributable under the terms of MIT license.
|
4
|
+
# See LICENSE file or http://www.opensource.org/licenses/mit-license.php
|
5
|
+
#------------------------------------------------------------------------------
|
6
|
+
class Object #:nodoc:
|
7
|
+
#
|
8
|
+
# Intercept methods below to inject @__awesome_print__ instance variable
|
9
|
+
# so we know it is the *methods* array when formatting an array.
|
10
|
+
#
|
11
|
+
# Remaining instance '_methods' are handled in core_ext/class.rb.
|
12
|
+
#
|
13
|
+
%w(methods private_methods protected_methods public_methods singleton_methods).each do |name|
|
14
|
+
original_method = instance_method(name)
|
15
|
+
|
16
|
+
define_method name do |*args|
|
17
|
+
methods = original_method.bind(self).call(*args)
|
18
|
+
methods.instance_variable_set('@__awesome_methods__', self)
|
19
|
+
methods
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|