toffee 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,18 +1,23 @@
1
- = toffee
1
+ = Toffee
2
+
3
+ Candy for your "puts"-debugging!
2
4
 
3
5
  = Usage
4
6
 
5
- Put +.d+ (almost) anywhere in your code to print out debug information:
7
+ Put the method call: "d" (almost) anywhere in your code to print out debug
8
+ information:
6
9
 
7
10
  ['a', 'b', 'c'].d.first.d.upcase.d
8
11
 
9
12
  will print out:
10
13
 
11
- ["a", "b", "c"]
12
- "a"
13
- "A"
14
+ ["a", "b", "c"]
15
+ "a"
16
+ "A"
17
+
18
+ "d" always returns self, so it will not interfere or do any harm.
14
19
 
15
- = Configure the output (optional)
20
+ == Configure the output (optional)
16
21
 
17
22
  Write to standard output (default):
18
23
 
@@ -23,19 +28,19 @@ or
23
28
  Toffee.configure(:stdout)
24
29
 
25
30
 
26
- Write to any object that implements +:puts+:
31
+ Write to any object that implements ":puts":
27
32
 
28
33
  Toffee.configure(IO.new(2, 'w'))
29
34
 
30
35
 
31
- Write to any object that implements +:debug+:
36
+ Write to any object that implements ":debug":
32
37
 
33
38
  Toffee.configure(Rails.logger)
34
39
 
35
40
 
36
41
  Write to any object that implements the second parameter:
37
42
 
38
- Toffee.configure(Rails.logger, :info)
43
+ Toffee.configure(Rails.logger, :info)
39
44
 
40
45
 
41
46
  Write to file using the shell command:
@@ -44,10 +49,14 @@ Write to file using the shell command:
44
49
 
45
50
  Toffee.configure('/tmp/foo.log')
46
51
 
52
+ == Options
53
+
54
+ :timestamp Turn on timestamps with true, turn if off with nil or false; to use
55
+ a custom timestamp format, pass a string with a Time.strftime kind of format
47
56
 
48
57
  == Copyright
49
58
 
50
59
  Copyright (c) 2010 Christoph Petschnig. See LICENSE for details.
51
60
 
52
- Inspiration and part of the code taken from
61
+ Inspiration and parts of the code taken from
53
62
  Jan Lelis (http://github.com/janlelis/zucker)
@@ -9,7 +9,6 @@ module Kernel
9
9
  # TODO: check, if there is already a method named d
10
10
  # show a warning in that case!
11
11
 
12
- #def d(*args, &block)
13
12
  def d(*args)
14
13
  if args.empty?
15
14
  tap do
@@ -1,5 +1,7 @@
1
1
  module Toffee
2
2
 
3
+ DEFAULT_TIMESTAMP_FORMAT = '%Y-%m-%d %H:%M:%S '
4
+
3
5
  # There are several ways, where debugging output can be directed to:
4
6
  # STDOUT, :stdout Standard out is the default destination
5
7
  # Any other Object that responds to :puts
@@ -31,6 +33,14 @@ module Toffee
31
33
  # Toffee.configure('/tmp/foo.log') write to file using the shell command:
32
34
  # $ echo "my output here" > /tmp/foo.log
33
35
  #
36
+ # Hash with options:
37
+ #
38
+ # :timestamp prepend a timestamp on each log message
39
+ # use true to turn logging on; turn it off
40
+ # by passing nil or false; pass a string
41
+ # value to supply a custom format (like
42
+ # with Time.strftime); default is true
43
+ #
34
44
  def configure(*args)
35
45
  raise ArgumentError if args.empty?
36
46
 
@@ -41,7 +51,7 @@ module Toffee
41
51
  raise TypeError.new("Argument two should be kind'a Symbol.") if method && !method.kind_of?(Symbol)
42
52
  end
43
53
 
44
- options = args.last
54
+ options = args.last || {}
45
55
 
46
56
  init_configuration
47
57
 
@@ -58,7 +68,7 @@ module Toffee
58
68
  if target.respond_to?(method)
59
69
  [:logger, target, method]
60
70
  else
61
- raise TypeError.new("Target object does not respond to method :#{method}.")
71
+ raise ArgumentError.new("Target object does not respond to method :#{method}.")
62
72
  end
63
73
  else # no method was given
64
74
  if target.respond_to?(:debug)
@@ -71,10 +81,20 @@ module Toffee
71
81
 
72
82
  # TODO: implement much, much more features
73
83
 
74
- # option :with_timestamp
84
+ # option :timestamp
85
+ if options.key?(:timestamp)
86
+ timestamp = options.delete(:timestamp)
87
+ @@configuration[:timestamp] = timestamp
88
+ @@configuration[:timestamp_format] = timestamp if timestamp.kind_of?(String)
89
+ end
90
+
91
+
92
+ # option :with_file_position
75
93
 
76
- # option :with_backtrace
94
+ # option :stacktrace
77
95
 
96
+ # option :prefix, :suffix
97
+
78
98
  self
79
99
  end
80
100
 
@@ -88,12 +108,21 @@ module Toffee
88
108
  # write to STDOUT, or to the destination that was configured
89
109
  def output(string)
90
110
  init_configuration
111
+
112
+ string = prepend_timestamp(string) if @@configuration[:timestamp]
113
+
91
114
  case @@configuration[:target_type]
92
115
  when :io then @@configuration[:target].puts(string)
93
116
  when :file then %x{echo "#{string.gsub('"', '\\"')}" >> #{@@configuration[:target]}}
94
117
  when :logger then @@configuration[:target].send(@@configuration[:method], string)
95
118
  end
96
119
  end
120
+
121
+ private
122
+
123
+ def prepend_timestamp(string)
124
+ Time.new.strftime(@@configuration[:timestamp_format] || DEFAULT_TIMESTAMP_FORMAT) << string
125
+ end
97
126
 
98
127
  end
99
128
 
@@ -7,3 +7,14 @@ require 'spec/autorun'
7
7
  Spec::Runner.configure do |config|
8
8
 
9
9
  end
10
+
11
+ class SpecLogger
12
+ attr_reader :output
13
+ def test_log(output)
14
+ @output ||= []
15
+ @output << output
16
+ end
17
+ def clear_log
18
+ @output = []
19
+ end
20
+ end
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $:.unshift(File.join(File.dirname(__FILE__), "..", "..", "lib"))
4
+
5
+ require 'toffee'
6
+
7
+ Toffee.configure STDOUT
8
+
9
+ ('x'.d * 3.d).d
10
+
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # Test: don't set any configuration properties, only use defaults
4
+
5
+ $:.unshift(File.join(File.dirname(__FILE__), "..", "..", "lib"))
6
+
7
+ require 'toffee'
8
+
9
+ ('x'.d * 3.d).d
10
+
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $:.unshift(File.join(File.dirname(__FILE__), "..", "..", "lib"))
4
+
5
+ require 'toffee'
6
+
7
+ Toffee.configure :stdout
8
+
9
+ ('x'.d * 3.d).d
10
+
@@ -1,7 +1,101 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
2
 
3
+ require 'logger'
4
+
3
5
  describe "Toffee" do
4
- it "fails" do
5
- fail "hey buddy, you should probably rename this file and start specing for real"
6
+
7
+ before :all do
8
+ @log_obj = Logger.new(STDOUT)
9
+ @spec_logger = SpecLogger.new
6
10
  end
11
+
12
+ it "configure does some checks on its arguments" do
13
+ # check argument must be given
14
+ lambda {Toffee.configure}.should raise_exception ArgumentError
15
+
16
+ # second argument must be a Symbol or Hash
17
+ lambda {Toffee.configure(:foo, Time)}.should raise_exception TypeError
18
+ lambda {Toffee.configure(@log_obj, :error)}.should_not raise_exception
19
+ # will result in logging to :debug
20
+ lambda {Toffee.configure(@log_obj, :a => 1)}.should_not raise_exception
21
+
22
+ # try to log to write protected file
23
+ lambda {Toffee.configure('/etc/oh-no-this-must-not-work.log')}.should raise_exception IOError
24
+
25
+ # method must be valid
26
+ lambda {Toffee.configure(@log_obj, :foo)}.should raise_exception ArgumentError
27
+
28
+ # test for default :debug method
29
+ lambda {Toffee.configure(Time)}.should raise_exception TypeError
30
+ end
31
+
32
+ it "writes to STDOUT, when no configuration was made" do
33
+ %x{#{File.join(File.dirname(__FILE__), 'test-apps/all_defaults_app.rb')}}.should == %{"x"\n3\n"xxx"\n}
34
+ end
35
+
36
+ it "accepts various configurations for STDOUT" do
37
+ %x{#{File.join(File.dirname(__FILE__), 'test-apps/all_defaults_app.rb')}}.should == %{"x"\n3\n"xxx"\n}
38
+ %x{#{File.join(File.dirname(__FILE__), 'test-apps/STDOUT_app.rb')}}.should == %{"x"\n3\n"xxx"\n}
39
+ %x{#{File.join(File.dirname(__FILE__), 'test-apps/stdout_symbol_app.rb')}}.should == %{"x"\n3\n"xxx"\n}
40
+ end
41
+
42
+ it "works with any object that implements a 'puts' method" do
43
+
44
+ end
45
+
46
+ it "writes to a file when configured with a string value" do
47
+
48
+ end
49
+
50
+ it "writes to an object with an arbitrary method name" do
51
+
52
+ end
53
+
54
+ it "writes to any object with a 'debug' method" do
55
+
56
+ end
57
+
58
+ it "truncates a file to zero length with the 'clear' method" do
59
+
60
+ end
61
+
62
+ it "writes timestamps when configured to do so" do
63
+ # turn off timestamping by passing nil
64
+ Toffee.configure @spec_logger, :test_log, :timestamp => nil
65
+ 'foo'.d
66
+ @spec_logger.output.last.should == '"foo"'
67
+
68
+ # turn on timestamp and use the default format
69
+ Toffee.configure :timestamp => true
70
+ 'foo'.d
71
+ time_result = Time.utc(*@spec_logger.output.last[0..(Toffee::DEFAULT_TIMESTAMP_FORMAT.size)].gsub(/[: ]/, '-').split('-'))
72
+ # there should not be more than half a second in between
73
+ (Time.new - time_result).should < 0.5
74
+
75
+
76
+ # turn off timestamping passing false
77
+ Toffee.configure :timestamp => false
78
+ 'foo'.d
79
+ @spec_logger.output.last.should == '"foo"'
80
+
81
+
82
+ # turn on timestamp and use the default format
83
+ Toffee.configure :timestamp => '[%Y|%m|%d|%H|%M|%S] '
84
+ 'foo'.d
85
+ time_result = Time.utc(*@spec_logger.output.last[1..20].split('|'))
86
+ # there should not be more than half a second in between
87
+ (Time.new - time_result).should < 0.5
88
+
89
+ Toffee.configure :timestamp => false
90
+ @spec_logger.clear_log
91
+ end
92
+
93
+ it "can compute multiple calls to 'configure'" do
94
+
95
+ end
96
+
97
+ it "gives correct file position and call stack output" do
98
+
99
+ end
100
+
7
101
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: toffee
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 27
5
5
  prerelease: false
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
  - Christoph Petschnig
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-08-06 00:00:00 +02:00
18
+ date: 2010-08-16 00:00:00 +02:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -50,6 +50,9 @@ files:
50
50
  - LICENSE
51
51
  - README.rdoc
52
52
  - spec/spec_helper.rb
53
+ - spec/test-apps/all_defaults_app.rb
54
+ - spec/test-apps/STDOUT_app.rb
55
+ - spec/test-apps/stdout_symbol_app.rb
53
56
  - spec/toffee_spec.rb
54
57
  has_rdoc: true
55
58
  homepage: http://github.com/cpetschnig/toffee
@@ -87,4 +90,7 @@ specification_version: 3
87
90
  summary: Give "puts"-debugging some candy
88
91
  test_files:
89
92
  - spec/spec_helper.rb
93
+ - spec/test-apps/all_defaults_app.rb
94
+ - spec/test-apps/STDOUT_app.rb
95
+ - spec/test-apps/stdout_symbol_app.rb
90
96
  - spec/toffee_spec.rb