awesome_print 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,3 +1,9 @@
1
+ 0.1.4
2
+ - Format BigDecimal and Rational objects as Float scalars
3
+ - Explicit options parameter can override custom defaults
4
+ - Custom defaults are not interfering when running specs
5
+ - Custom defaults now work correctly with Ruby 1.9.x
6
+
1
7
  0.1.3
2
8
  - Added support for setting custom defaults in ~/.aprc
3
9
 
data/README.md CHANGED
@@ -23,7 +23,7 @@ Default options:
23
23
  :multiline => true,
24
24
  :plain => false,
25
25
  :indent => 4,
26
- :colors => {
26
+ :color => {
27
27
  :array => :white,
28
28
  :bignum => :blue,
29
29
  :class => :yellow,
@@ -47,7 +47,7 @@ Supported color names:
47
47
  ### Examples ###
48
48
  $ cat > 1.rb
49
49
  require "ap"
50
- data = [ false, 42, %w(fourty two), { :now => Time.now, :class => Time.now.class, :distance => 42e42 } ]
50
+ data = [ false, 42, %w(forty two), { :now => Time.now, :class => Time.now.class, :distance => 42e42 } ]
51
51
  ap data
52
52
  ^D
53
53
  $ ruby 1.rb
@@ -55,7 +55,7 @@ Supported color names:
55
55
  [0] false,
56
56
  [1] 42,
57
57
  [2] [
58
- [0] "fourty",
58
+ [0] "forty",
59
59
  [1] "two"
60
60
  ],
61
61
  [3] {
@@ -79,12 +79,12 @@ Supported color names:
79
79
 
80
80
  $ cat > 3.rb
81
81
  require "ap"
82
- data = [ false, 42, %w(fourty two) ]
82
+ data = [ false, 42, %w(forty two) ]
83
83
  data << data # <-- Nested array.
84
84
  ap data, :multiline => false
85
85
  ^D
86
86
  $ ruby 3.rb
87
- [ false, 42, [ "fourty", "two" ], [...] ]
87
+ [ false, 42, [ "forty", "two" ], [...] ]
88
88
 
89
89
  ### Example (Rails console) ###
90
90
  $ ruby script/console
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.3
1
+ 0.1.4
@@ -5,7 +5,7 @@
5
5
  #------------------------------------------------------------------------------
6
6
  class AwesomePrint
7
7
  AP = :__awesome_print__
8
- CORE = [ :array, :hash, :class, :file, :dir ]
8
+ CORE = [ :array, :hash, :class, :file, :dir, :bigdecimal, :rational ]
9
9
 
10
10
  def initialize(options = {})
11
11
  @options = {
@@ -14,7 +14,7 @@ class AwesomePrint
14
14
  :indent => 4,
15
15
  :color => {
16
16
  :array => :white,
17
- :bignum => :blue,
17
+ :bigdecimal => :blue,
18
18
  :class => :yellow,
19
19
  :date => :greenish,
20
20
  :falseclass => :red,
@@ -26,10 +26,12 @@ class AwesomePrint
26
26
  :symbol => :cyanish,
27
27
  :time => :greenish,
28
28
  :trueclass => :green
29
- }.merge(options.delete(:color) || {})
30
- }.merge(options)
29
+ }
30
+ }
31
31
 
32
- load_custom_defaults
32
+ # Merge custom defaults and let explicit options parameter override them.
33
+ merge_custom_defaults!
34
+ merge_options!(options)
33
35
 
34
36
  @indentation = @options[:indent].abs
35
37
  Thread.current[AP] ||= []
@@ -113,6 +115,13 @@ class AwesomePrint
113
115
  awesome_self(d, :with => ls.empty? ? nil : "\n#{ls.chop}")
114
116
  end
115
117
 
118
+ # Format BigDecimal and Rational objects by convering them to Float.
119
+ #------------------------------------------------------------------------------
120
+ def awesome_bigdecimal(n)
121
+ awesome_self(n.to_f, :as => :bigdecimal)
122
+ end
123
+ alias :awesome_rational :awesome_bigdecimal
124
+
116
125
  # Catch all method to format an arbitrary object.
117
126
  #------------------------------------------------------------------------------
118
127
  def awesome_self(object, appear = {})
@@ -188,31 +197,28 @@ class AwesomePrint
188
197
  @indentation -= @options[:indent].abs
189
198
  end
190
199
 
191
- #------------------------------------------------------------------------------
192
200
  def indent
193
201
  @indent = ' ' * @indentation
194
202
  end
195
203
 
196
- #------------------------------------------------------------------------------
197
204
  def outdent
198
205
  @outdent = ' ' * (@indentation - @options[:indent].abs)
199
206
  end
200
207
 
201
- # Load ~/.aprc file that can store custom defaults, for example:
202
- #
203
- # AwesomePrint.defaults = {
204
- # :indent => -2,
205
- # :color => {
206
- # :trueclass => :red
207
- # }
208
- # }
208
+ # Update @options by first merging the :color hash and then the remaining keys.
209
+ #------------------------------------------------------------------------------
210
+ def merge_options!(options = {})
211
+ @options[:color].merge!(options.delete(:color) || {})
212
+ @options.merge!(options)
213
+ end
214
+
215
+ # Load ~/.aprc file with custom defaults that override default options.
209
216
  #------------------------------------------------------------------------------
210
- def load_custom_defaults
217
+ def merge_custom_defaults!
211
218
  dotfile = File.join(ENV["HOME"], ".aprc")
212
219
  if File.readable?(dotfile)
213
220
  load dotfile
214
- @options[:color].merge!(self.class.defaults.delete(:color) || {})
215
- @options.merge!(self.class.defaults)
221
+ merge_options!(self.class.defaults)
216
222
  end
217
223
  rescue => e
218
224
  $stderr.puts "Could not load #{dotfile}: #{e}"
@@ -224,8 +230,8 @@ class AwesomePrint
224
230
  @@defaults ||= {}
225
231
  end
226
232
 
227
- def self.defaults=(*args)
228
- @@defaults = *args
233
+ def self.defaults=(args = {})
234
+ @@defaults = args
229
235
  end
230
236
 
231
237
  end
@@ -1,6 +1,11 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+ require "bigdecimal"
3
+ require "rational"
2
4
 
3
5
  describe "AwesomePrint" do
6
+ before(:each) do
7
+ stub_dotfile!
8
+ end
4
9
 
5
10
  describe "Array" do
6
11
  before(:each) do
@@ -259,4 +264,28 @@ EOS
259
264
  end
260
265
  end
261
266
 
267
+ #------------------------------------------------------------------------------
268
+ describe "BigDecimal and Rational" do
269
+ it "should present BigDecimal object as Float scalar" do
270
+ big = BigDecimal("2010.04")
271
+ big.ai(:plain => true).should == "2010.4"
272
+ end
273
+
274
+ it "should present Rational object as Float scalar" do
275
+ rat = Rational(2010, 2)
276
+ rat.ai(:plain => true).should == "1005.0"
277
+ end
278
+ end
279
+
280
+ #------------------------------------------------------------------------------
281
+ describe "Utility methods" do
282
+ it "should merge options" do
283
+ ap = AwesomePrint.new
284
+ ap.send(:merge_options!, { :color => { :array => :black }, :indent => 0 })
285
+ options = ap.instance_variable_get("@options")
286
+ options[:color][:array].should == :black
287
+ options[:indent].should == 0
288
+ end
289
+ end
290
+
262
291
  end
data/spec/rails_spec.rb CHANGED
@@ -20,17 +20,22 @@ if defined?(::Rails)
20
20
  column :created_at, :datetime
21
21
  end
22
22
 
23
- #------------------------------------------------------------------------------
24
- describe "ActiveRecord instance" do
23
+ describe "AwesomePrint/Rails" do
25
24
  before(:each) do
26
- @diana = User.new(:name => "Diana", :rank => 1, :admin => false, :created_at => "1992-10-10 12:30:00")
27
- @laura = User.new(:name => "Laura", :rank => 2, :admin => true, :created_at => "2003-05-26 14:15:00")
28
- @ap = AwesomePrint.new(:plain => true)
25
+ stub_dotfile!
29
26
  end
30
27
 
31
- it "display single record" do
32
- out = @ap.send(:awesome, @diana)
33
- out.gsub(/0x([a-f\d]+)/, "0x01234567").should == <<-EOS.strip
28
+ #------------------------------------------------------------------------------
29
+ describe "ActiveRecord instance" do
30
+ before(:each) do
31
+ @diana = User.new(:name => "Diana", :rank => 1, :admin => false, :created_at => "1992-10-10 12:30:00")
32
+ @laura = User.new(:name => "Laura", :rank => 2, :admin => true, :created_at => "2003-05-26 14:15:00")
33
+ @ap = AwesomePrint.new(:plain => true)
34
+ end
35
+
36
+ it "display single record" do
37
+ out = @ap.send(:awesome, @diana)
38
+ out.gsub(/0x([a-f\d]+)/, "0x01234567").should == <<-EOS.strip
34
39
  #<User:0x01234567> {
35
40
  :id => nil,
36
41
  :name => "Diana",
@@ -39,11 +44,11 @@ if defined?(::Rails)
39
44
  :created_at => Sat, 10 Oct 1992 12:30:00 UTC +00:00
40
45
  }
41
46
  EOS
42
- end
47
+ end
43
48
 
44
- it "display multiple records" do
45
- out = @ap.send(:awesome, [ @diana, @laura ])
46
- out.gsub(/0x([a-f\d]+)/, "0x01234567").should == <<-EOS.strip
49
+ it "display multiple records" do
50
+ out = @ap.send(:awesome, [ @diana, @laura ])
51
+ out.gsub(/0x([a-f\d]+)/, "0x01234567").should == <<-EOS.strip
47
52
  [
48
53
  [0] #<User:0x01234567> {
49
54
  :id => nil,
@@ -61,14 +66,14 @@ EOS
61
66
  }
62
67
  ]
63
68
  EOS
69
+ end
64
70
  end
65
- end
66
71
 
67
- #------------------------------------------------------------------------------
68
- describe "ActiveRecord class" do
69
- it "should" do
70
- @ap = AwesomePrint.new(:plain => true)
71
- @ap.send(:awesome, User).should == <<-EOS.strip
72
+ #------------------------------------------------------------------------------
73
+ describe "ActiveRecord class" do
74
+ it "should" do
75
+ @ap = AwesomePrint.new(:plain => true)
76
+ @ap.send(:awesome, User).should == <<-EOS.strip
72
77
  class User < ActiveRecord::Base {
73
78
  :id => :integer,
74
79
  :name => :string,
@@ -77,7 +82,7 @@ class User < ActiveRecord::Base {
77
82
  :created_at => :datetime
78
83
  }
79
84
  EOS
85
+ end
80
86
  end
81
87
  end
82
-
83
88
  end
data/spec/spec_helper.rb CHANGED
@@ -5,5 +5,9 @@ require 'spec'
5
5
  require 'spec/autorun'
6
6
 
7
7
  Spec::Runner.configure do |config|
8
-
8
+ end
9
+
10
+ def stub_dotfile!
11
+ dotfile = File.join(ENV["HOME"], ".aprc")
12
+ File.should_receive(:readable?).at_least(:once).with(dotfile).and_return(false)
9
13
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 3
9
- version: 0.1.3
8
+ - 4
9
+ version: 0.1.4
10
10
  platform: ruby
11
11
  authors:
12
12
  - Michael Dvorkin
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-04-07 00:00:00 -07:00
17
+ date: 2010-04-08 00:00:00 -07:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency