Class: Saper::Logger
- Inherits:
-
Object
- Object
- Saper::Logger
- Defined in:
- lib/saper/core/logger.rb
Instance Attribute Summary collapse
-
#io ⇒ Object
readonly
TODO: requires serious overhaul.
Instance Method Summary collapse
- #action(instance) ⇒ Object
- #action_input(instance) ⇒ Object
- #action_name(instance) ⇒ Object
- #action_output(instance) ⇒ Object
- #bold(string) ⇒ Object
- #download(url) ⇒ Object
- #green(string) ⇒ Object
-
#initialize(io = nil) ⇒ Logger
constructor
A new instance of Logger.
- #item(instance) ⇒ Object
- #red(string) ⇒ Object
- #runtime(instance, indent = 0) ⇒ Object
Constructor Details
#initialize(io = nil) ⇒ Logger
Returns a new instance of Logger.
8 9 10 |
# File 'lib/saper/core/logger.rb', line 8 def initialize(io = nil) @io = io || StringIO.new end |
Instance Attribute Details
#io ⇒ Object (readonly)
TODO: requires serious overhaul
6 7 8 |
# File 'lib/saper/core/logger.rb', line 6 def io @io end |
Instance Method Details
#action(instance) ⇒ Object
36 37 38 |
# File 'lib/saper/core/logger.rb', line 36 def action(instance) "%s %s > %s" % [action_name(instance), action_input(instance), action_output(instance)] end |
#action_input(instance) ⇒ Object
40 41 42 |
# File 'lib/saper/core/logger.rb', line 40 def action_input(instance) item(instance.input) end |
#action_name(instance) ⇒ Object
48 49 50 |
# File 'lib/saper/core/logger.rb', line 48 def action_name(instance) instance.error? ? red(instance.action.name) : green(instance.action.name) end |
#action_output(instance) ⇒ Object
44 45 46 |
# File 'lib/saper/core/logger.rb', line 44 def action_output(instance) item(instance.action.multiple? ? instance.output : instance.output.first) end |
#bold(string) ⇒ Object
24 25 26 |
# File 'lib/saper/core/logger.rb', line 24 def bold(string) "\033[1m%s\033[0m" % string end |
#download(url) ⇒ Object
12 13 14 |
# File 'lib/saper/core/logger.rb', line 12 def download(url) io.write "%s %s\r\n" % [bold("Downloading"), url] end |
#green(string) ⇒ Object
32 33 34 |
# File 'lib/saper/core/logger.rb', line 32 def green(string) "\033[32m%s\033[0m" % string end |
#item(instance) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/saper/core/logger.rb', line 52 def item(instance) case instance when Array "[%s]" % instance.map { |i| item(i) }.join(",") when nil 'nil' when Items::Document 'Document' when Items::HTML 'HTML' when String '%s' % instance when Items::Text '%s' % instance.to_s when Items::Atom '%s' % instance.to_hash else instance.class end end |
#red(string) ⇒ Object
28 29 30 |
# File 'lib/saper/core/logger.rb', line 28 def red(string) "\033[31m%s\033[0m" % string end |
#runtime(instance, indent = 0) ⇒ Object
16 17 18 19 20 21 22 |
# File 'lib/saper/core/logger.rb', line 16 def runtime(instance, indent = 0) io.write bold("Recipe tree\r\n") if indent == 0 io.write "%s%s\r\n" % [" " * indent, action(instance)] instance.children.each do |child| runtime(child, indent + 1) end end |