toffee 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/LICENSE +20 -0
- data/README.rdoc +53 -0
- data/lib/toffee/d.rb +24 -0
- data/lib/toffee/toffee.rb +100 -0
- data/lib/toffee.rb +2 -0
- data/spec/spec_helper.rb +9 -0
- data/spec/toffee_spec.rb +7 -0
- metadata +90 -0
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2010 Christoph Petschnig
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
= toffee
|
2
|
+
|
3
|
+
= Usage
|
4
|
+
|
5
|
+
Put +.d+ (almost) anywhere in your code to print out debug information:
|
6
|
+
|
7
|
+
['a', 'b', 'c'].d.first.d.upcase.d
|
8
|
+
|
9
|
+
will print out:
|
10
|
+
|
11
|
+
["a", "b", "c"]
|
12
|
+
"a"
|
13
|
+
"A"
|
14
|
+
|
15
|
+
= Configure the output (optional)
|
16
|
+
|
17
|
+
Write to standard output (default):
|
18
|
+
|
19
|
+
Toffee.configure(STDOUT)
|
20
|
+
|
21
|
+
or
|
22
|
+
|
23
|
+
Toffee.configure(:stdout)
|
24
|
+
|
25
|
+
|
26
|
+
Write to any object that implements +:puts+:
|
27
|
+
|
28
|
+
Toffee.configure(IO.new(2, 'w'))
|
29
|
+
|
30
|
+
|
31
|
+
Write to any object that implements +:debug+:
|
32
|
+
|
33
|
+
Toffee.configure(Rails.logger)
|
34
|
+
|
35
|
+
|
36
|
+
Write to any object that implements the second parameter:
|
37
|
+
|
38
|
+
Toffee.configure(Rails.logger, :info)
|
39
|
+
|
40
|
+
|
41
|
+
Write to file using the shell command:
|
42
|
+
|
43
|
+
$ echo "my output here" > /tmp/foo.log
|
44
|
+
|
45
|
+
Toffee.configure('/tmp/foo.log')
|
46
|
+
|
47
|
+
|
48
|
+
== Copyright
|
49
|
+
|
50
|
+
Copyright (c) 2010 Christoph Petschnig. See LICENSE for details.
|
51
|
+
|
52
|
+
Inspiration and part of the code taken from
|
53
|
+
Jan Lelis (http://github.com/janlelis/zucker)
|
data/lib/toffee/d.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# This part was very much inspired by Jan Lelis and his talk at RUG-B on
|
2
|
+
# August 5th, 2010
|
3
|
+
# Find his code at http://github.com/janlelis/zucker/blob/master/lib/zucker/D.rb
|
4
|
+
|
5
|
+
# Copyright (c) 2010 Jan Lelis
|
6
|
+
|
7
|
+
module Kernel
|
8
|
+
|
9
|
+
# TODO: check, if there is already a method named d
|
10
|
+
# show a warning in that case!
|
11
|
+
|
12
|
+
#def d(*args, &block)
|
13
|
+
def d(*args)
|
14
|
+
if args.empty?
|
15
|
+
tap do
|
16
|
+
Toffee.output(block_given? ? yield(self) : self.inspect)
|
17
|
+
end
|
18
|
+
else
|
19
|
+
raise ArgumentError, "Toffee: .d - The parser thought that the code after .d are method arguments... Please don't put a space after d or use .d() or .d{} in this case!"
|
20
|
+
# eval ...
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
@@ -0,0 +1,100 @@
|
|
1
|
+
module Toffee
|
2
|
+
|
3
|
+
# There are several ways, where debugging output can be directed to:
|
4
|
+
# STDOUT, :stdout Standard out is the default destination
|
5
|
+
# Any other Object that responds to :puts
|
6
|
+
# A filename; append to this file using system command `echo "foo" >> /tmp/my-file
|
7
|
+
# A logger object, that responds to the second argument
|
8
|
+
|
9
|
+
class << self
|
10
|
+
|
11
|
+
# Set up the default configuration
|
12
|
+
def init_configuration
|
13
|
+
@@configuration ||= {:target => STDOUT, :target_type => :io}
|
14
|
+
end
|
15
|
+
|
16
|
+
# Configure the output:
|
17
|
+
#
|
18
|
+
# Toffee.configure(STDOUT) write to standard output (default)
|
19
|
+
# Toffee.configure(:stdout) write to standard output (default)
|
20
|
+
#
|
21
|
+
# Toffee.configure(IO.new(2, 'w')) write to any object that
|
22
|
+
# implements :puts
|
23
|
+
#
|
24
|
+
# Toffee.configure(Rails.logger) write to any object that
|
25
|
+
# implements :debug
|
26
|
+
#
|
27
|
+
# Toffee.configure(Rails.logger, :info)
|
28
|
+
# write to any object that
|
29
|
+
# implements the second parameter
|
30
|
+
#
|
31
|
+
# Toffee.configure('/tmp/foo.log') write to file using the shell command:
|
32
|
+
# $ echo "my output here" > /tmp/foo.log
|
33
|
+
#
|
34
|
+
def configure(*args)
|
35
|
+
raise ArgumentError if args.empty?
|
36
|
+
|
37
|
+
unless args.first.kind_of?(Hash)
|
38
|
+
target = args.delete_at(0)
|
39
|
+
method = args.delete_at(0) unless args.empty? || args.first.kind_of?(Hash)
|
40
|
+
method.to_sym if method.kind_of?(String)
|
41
|
+
raise TypeError.new("Argument two should be kind'a Symbol.") if method && !method.kind_of?(Symbol)
|
42
|
+
end
|
43
|
+
|
44
|
+
options = args.last
|
45
|
+
|
46
|
+
init_configuration
|
47
|
+
|
48
|
+
if target
|
49
|
+
@@configuration[:target_type], @@configuration[:target],
|
50
|
+
@@configuration[:method] = if [STDOUT, :stdout].include?(target)
|
51
|
+
[:io, STDOUT]
|
52
|
+
elsif target.respond_to?(:puts)
|
53
|
+
[:io, target]
|
54
|
+
elsif target.kind_of?(String)
|
55
|
+
raise IOError.new("Cannot write to file '#{target}'.") unless File.writable?(target) || File.writable?(File.dirname(target))
|
56
|
+
[:file, target]
|
57
|
+
elsif (method)
|
58
|
+
if target.respond_to?(method)
|
59
|
+
[:logger, target, method]
|
60
|
+
else
|
61
|
+
raise TypeError.new("Target object does not respond to method :#{method}.")
|
62
|
+
end
|
63
|
+
else # no method was given
|
64
|
+
if target.respond_to?(:debug)
|
65
|
+
[:logger, target, :debug]
|
66
|
+
else
|
67
|
+
raise TypeError.new("Target object does not respond to method :debug.")
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
# TODO: implement much, much more features
|
73
|
+
|
74
|
+
# option :with_timestamp
|
75
|
+
|
76
|
+
# option :with_backtrace
|
77
|
+
|
78
|
+
self
|
79
|
+
end
|
80
|
+
|
81
|
+
# clear the output file
|
82
|
+
def clear
|
83
|
+
init_configuration
|
84
|
+
return unless @@configuration[:target_type] == :file
|
85
|
+
%x{echo -n > #{@@configuration[:target]}}
|
86
|
+
end
|
87
|
+
|
88
|
+
# write to STDOUT, or to the destination that was configured
|
89
|
+
def output(string)
|
90
|
+
init_configuration
|
91
|
+
case @@configuration[:target_type]
|
92
|
+
when :io then @@configuration[:target].puts(string)
|
93
|
+
when :file then %x{echo "#{string.gsub('"', '\\"')}" >> #{@@configuration[:target]}}
|
94
|
+
when :logger then @@configuration[:target].send(@@configuration[:method], string)
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
end
|
99
|
+
|
100
|
+
end
|
data/lib/toffee.rb
ADDED
data/spec/spec_helper.rb
ADDED
data/spec/toffee_spec.rb
ADDED
metadata
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: toffee
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Christoph Petschnig
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-08-06 00:00:00 +02: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: 13
|
30
|
+
segments:
|
31
|
+
- 1
|
32
|
+
- 2
|
33
|
+
- 9
|
34
|
+
version: 1.2.9
|
35
|
+
type: :development
|
36
|
+
version_requirements: *id001
|
37
|
+
description: Debugging convenience for your ruby application
|
38
|
+
email: info@purevirtual.de
|
39
|
+
executables: []
|
40
|
+
|
41
|
+
extensions: []
|
42
|
+
|
43
|
+
extra_rdoc_files:
|
44
|
+
- LICENSE
|
45
|
+
- README.rdoc
|
46
|
+
files:
|
47
|
+
- lib/toffee.rb
|
48
|
+
- lib/toffee/d.rb
|
49
|
+
- lib/toffee/toffee.rb
|
50
|
+
- LICENSE
|
51
|
+
- README.rdoc
|
52
|
+
- spec/spec_helper.rb
|
53
|
+
- spec/toffee_spec.rb
|
54
|
+
has_rdoc: true
|
55
|
+
homepage: http://github.com/cpetschnig/toffee
|
56
|
+
licenses: []
|
57
|
+
|
58
|
+
post_install_message:
|
59
|
+
rdoc_options:
|
60
|
+
- --charset=UTF-8
|
61
|
+
require_paths:
|
62
|
+
- lib
|
63
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
64
|
+
none: false
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
hash: 3
|
69
|
+
segments:
|
70
|
+
- 0
|
71
|
+
version: "0"
|
72
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ">="
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
hash: 3
|
78
|
+
segments:
|
79
|
+
- 0
|
80
|
+
version: "0"
|
81
|
+
requirements: []
|
82
|
+
|
83
|
+
rubyforge_project:
|
84
|
+
rubygems_version: 1.3.7
|
85
|
+
signing_key:
|
86
|
+
specification_version: 3
|
87
|
+
summary: Give "puts"-debugging some candy
|
88
|
+
test_files:
|
89
|
+
- spec/spec_helper.rb
|
90
|
+
- spec/toffee_spec.rb
|