module Slides::Log
Public Instance Methods
log(event, attrs={}, &block)
click to toggle source
# File lib/slides.rb, line 6 def log(event, attrs={}, &block) log_array(event, attrs.map { |k, v| [k, v] }, &block) end
log_array(event, arr) { || ... }
click to toggle source
# File lib/slides.rb, line 10 def log_array(event, arr, &block) unless block str = "#{event} #{unparse(arr)}" mtx.synchronize { stream.puts str } else arr = arr.dup start = Time.now arr << [:at, :start] log(event, arr) res = yield arr.pop arr << [:at, :finish] << [:elapsed, (Time.now - start).to_f] log(event, arr) res end end
stream()
click to toggle source
# File lib/slides.rb, line 27 def stream @stream || $stdout end
stream=(val)
click to toggle source
# File lib/slides.rb, line 31 def stream=(val) @stream = val end
Private Instance Methods
mtx()
click to toggle source
# File lib/slides.rb, line 37 def mtx @mtx ||= Mutex.new end
quote_string(k, v)
click to toggle source
# File lib/slides.rb, line 41 def quote_string(k, v) # try to find a quote style that fits if !v.include?('"') %{#{k}="#{v}"} elsif !v.include?("'") %{#{k}='#{v}'} else %{#{k}="#{v.gsub(/"/, '\\"')}"} end end
unparse(attrs)
click to toggle source
# File lib/slides.rb, line 52 def unparse(attrs) attrs.map { |k, v| unparse_pair(k, v) }.compact.join(" ") end
unparse_pair(k, v)
click to toggle source
# File lib/slides.rb, line 56 def unparse_pair(k, v) v = v.call if v.is_a?(Proc) # only quote strings if they include whitespace if v == nil nil elsif v.is_a?(Float) "#{k}=#{format("%.3f", v)}" elsif v.is_a?(String) && v =~ /\s/ quote_string(k, v) elsif v.is_a?(Time) "#{k}=#{v.iso8601}" else "#{k}=#{v}" end end