Class: Saper::Logger

Inherits:
Object
  • Object
show all
Defined in:
lib/saper/core/logger.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#ioObject (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