From 5f2d59ccadf004272d9e518cb6a95dc243ca945a Mon Sep 17 00:00:00 2001 From: HD Moore Date: Tue, 20 Mar 2007 18:25:19 +0000 Subject: [PATCH] Added the support class, confined it load within the OLE namespace git-svn-id: file:///home/svn/framework3/trunk@4542 4d416f70-5f16-0410-b530-b9f4589650da --- lib/ole/base.rb | 1 + lib/ole/storage.rb | 7 ++----- lib/ole/support.rb | 52 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 5 deletions(-) create mode 100644 lib/ole/support.rb diff --git a/lib/ole/base.rb b/lib/ole/base.rb index a47e0de39c..3b54c06756 100644 --- a/lib/ole/base.rb +++ b/lib/ole/base.rb @@ -1,5 +1,6 @@ module Ole # :nodoc: + require 'ole/support' Log = Logger.new_with_callstack end diff --git a/lib/ole/storage.rb b/lib/ole/storage.rb index 4292067f69..e140c803da 100755 --- a/lib/ole/storage.rb +++ b/lib/ole/storage.rb @@ -1,20 +1,17 @@ #! /usr/bin/ruby -w -$: << File.dirname(__FILE__) + '/..' - require 'iconv' require 'date' -require 'support' - require 'stringio' require 'tempfile' + require 'ole/base' require 'ole/types' -# not strictly ole related require 'ole/io_helpers' module Ole # :nodoc: + # # = Introduction # diff --git a/lib/ole/support.rb b/lib/ole/support.rb new file mode 100644 index 0000000000..746a62722e --- /dev/null +++ b/lib/ole/support.rb @@ -0,0 +1,52 @@ +#! /usr/bin/ruby + +# +# A file with general support functions used by most files in the project. +# + +require 'logger' + +class File # :nodoc: + # for consistency with StringIO and others. makes more sense than forcing + # them to provide a #stat + def size + stat.size + end +end + +class Symbol # :nodoc: + def to_proc + proc { |a| a.send self } + end +end + +module Enumerable # :nodoc: + # 1.9 backport + def group_by + hash = Hash.new { |hash, key| hash[key] = [] } + each { |item| hash[yield(item)] << item } + hash + end + + def sum initial=0 + inject(initial) { |a, b| a + b } + end +end + +class Logger # :nodoc: + # A helper method for creating Loggers which produce call stack + # in their output + def self.new_with_callstack logdev=STDERR + log = Logger.new logdev + log.level = WARN + log.formatter = proc do |severity, time, progname, msg| + # find where we were called from, in our code + callstack = caller.dup + callstack.shift while callstack.first =~ /\/logger\.rb:\d+:in/ + from = callstack.first.sub(/:in `(.*?)'/, ":\\1") + "[%s %s]\n%-7s%s\n" % [time.strftime('%H:%M:%S'), from, severity, msg.to_s] + end + log + end +end +