Module: Press::Printer

Defined in:
lib/press/printer.rb

Class Method Summary collapse

Class Method Details

.ctx=(data) ⇒ Object



6
7
8
# File 'lib/press/printer.rb', line 6

def self.ctx=(data)
  @ctx = data
end

.hashify(*data, initial) ⇒ Object



46
47
48
# File 'lib/press/printer.rb', line 46

def self.hashify(*data, initial)
  data.compact.reduce(initial.merge(@ctx || {})) { |d, v| d.merge v }
end

.mpd(*data, &blk) ⇒ Object



18
19
20
# File 'lib/press/printer.rb', line 18

def self.mpd(*data, &blk)
  mwrite $stdout, @mtx, hashify(*data, {}), &blk
end

.mpde(e, *data) ⇒ Object



34
35
36
# File 'lib/press/printer.rb', line 34

def self.mpde(e, *data)
  mwrite $stderr, [@mtx, "error"].compact.join("."), hashify(*data, :at => "error", :class => e.class, :message => e.message.lines.to_a.first, :trace => e.backtrace.map { |i| i.match(/(#{Gem.dir}|#{Dir.getwd})?\/(.*):in (.*)/) && $2 }[0..5].compact.inspect)
end

.mpdfm(file, m, *data, &blk) ⇒ Object



26
27
28
# File 'lib/press/printer.rb', line 26

def self.mpdfm(file, m, *data, &blk)
  mwrite $stdout, [@mtx, File.basename(file, ".rb"), m].compact.join("."), hashify(*data, :file => File.basename(file, ".rb"), :fn => m), &blk
end

.mpdfme(file, m, e, *data) ⇒ Object



42
43
44
# File 'lib/press/printer.rb', line 42

def self.mpdfme(file, m, e, *data)
  mwrite $stderr, [@mtx, "error"].compact.join("."), hashify(*data, :at => "error", :class => e.class, :message => e.message.lines.to_a.first, :trace => e.backtrace.map { |i| i.match(/(#{Gem.dir}|#{Dir.getwd})?\/(.*):in (.*)/) && $2 }[0..5].compact.inspect, :file => File.basename(file, ".rb"), :fn => m)
end

.mtag(tag, data) ⇒ Object



50
51
52
# File 'lib/press/printer.rb', line 50

def self.mtag(tag, data)
  data.tap { |d| d[:measure] = [tag, d[:event]].compact.join(".") if tag }
end

.mtx=(tag) ⇒ Object



10
11
12
# File 'lib/press/printer.rb', line 10

def self.mtx=(tag)
  @mtx = tag
end

.mwrite(file, tag, data, &blk) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/press/printer.rb', line 85

def self.mwrite(file, tag, data, &blk)
  unless blk
    file.puts stringify(mtag(tag, data))
    file.flush
  else
    start = Time.now
    write file, { :at => "start" }.merge(data)
    yield.tap do
      elapsed = Time.now - start
      mwrite file, tag, { :at => "finish", :elapsed => elapsed }.merge(data).tap { |d| d[:val] = elapsed if tag }
    end
  end
end

.pd(*data, &blk) ⇒ Object



14
15
16
# File 'lib/press/printer.rb', line 14

def self.pd(*data, &blk)
  write $stdout, hashify(*data, {}), &blk
end

.pde(e, *data) ⇒ Object



30
31
32
# File 'lib/press/printer.rb', line 30

def self.pde(e, *data)
  write $stderr, hashify(*data, :at => "error", :class => e.class, :message => e.message.lines.to_a.first, :trace => e.backtrace.map { |i| i.match(/(#{Gem.dir}|#{Dir.getwd})?\/(.*):in (.*)/) && $2 }[0..5].compact.inspect)
end

.pdfm(file, m, *data, &blk) ⇒ Object



22
23
24
# File 'lib/press/printer.rb', line 22

def self.pdfm(file, m, *data, &blk)
  write $stdout, hashify(*data, :file => File.basename(file, ".rb"), :fn => m), &blk
end

.pdfme(file, m, e, *data) ⇒ Object



38
39
40
# File 'lib/press/printer.rb', line 38

def self.pdfme(file, m, e, *data)
  write $stderr, hashify(*data, :at => "error", :class => e.class, :message => e.message.lines.to_a.first, :trace => e.backtrace.map { |i| i.match(/(#{Gem.dir}|#{Dir.getwd})?\/(.*):in (.*)/) && $2 }[0..5].compact.inspect, :file => File.basename(file, ".rb"), :fn => m)
end

.stringify(data) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/press/printer.rb', line 54

def self.stringify(data)
  data.map do |(k, v)|
    case v
    when Hash
      "#{k}={.."
    when Array
      "#{k}=[.."
    when NilClass
      "#{k}=nil"
    when Float
      "#{k}=#{format("%.3f", v)}"
    when Time
      "#{k}=#{v.iso8601}"
    else
      v_str = v.to_s.strip
      v_str.match(/\s/) ? "#{k}=\"#{v_str}\"" : "#{k}=#{v_str}"
    end
  end.join(" ")
end

.write(file, data, &blk) ⇒ Object



74
75
76
77
78
79
80
81
82
83
# File 'lib/press/printer.rb', line 74

def self.write(file, data, &blk)
  unless blk
    file.puts stringify(data)
    file.flush
  else
    start = Time.now
    write file, { :at => "start" }.merge(data)
    yield.tap { write file, { :at => "finish", :elapsed => Time.now - start }.merge(data) }
  end
end