Skip to content

Commit

Permalink
refactoring: #logger is now a dual head instance/class method
Browse files Browse the repository at this point in the history
  • Loading branch information
chikamichi committed Dec 31, 2010
1 parent f0c0389 commit 357f3c5
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 13 deletions.
26 changes: 17 additions & 9 deletions lib/logg/core.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
module Logg
class Machine
attr_accessor :message, :namespace
attr_reader :message, :namespace

def self.method_missing(meth, *args, &block)
@@loggy ||= ::Logg::Machine.new
@@loggy.namespace = meth.to_s
@@loggy.message = (args.first.to_s == 'debug') ? nil : args.first.to_s
@@loggy.send :output!
def method_missing(meth, *args, &block)
@namespace = meth.to_s
@message = (args.first.to_s == 'debug') ? nil : args.first.to_s
self.send :output!
end

private

def output!
debug = "#{Time.now} | "
debug += "[#{self.namespace}] " unless self.namespace.nil?
debug += self.message
debug += "[#{@namespace}] " unless @namespace.nil?
debug += @message
puts debug
return debug
end
Expand All @@ -25,7 +24,16 @@ def self.included(base)
if !base.respond_to?(:logger)
base.instance_eval do
def self.logger
::Logg::Machine
@@_logg_er ||= ::Logg::Machine.new
end
end

def initialize
super
class << self
self
end.send(:define_method, :logger) do
@_logg_er ||= ::Logg::Machine.new
end
end
else
Expand Down
23 changes: 19 additions & 4 deletions spec/logg_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ class Foo
class Bar < Foo
end

shared_examples_for "a Logg::Er user" do
describe "the class" do
it "should know about self#logger" do
u.should respond_to?(:logger)
end
end
end

describe Logg do
context Logg do
def subject; Logg; end
Expand All @@ -23,19 +31,26 @@ def subject; Logg::Machine; end
it { should be_a Module }
end

context Foo do
context Foo, "instance" do
subject { Foo.new }
#it_should_behave_like "a Logg::Er user" do
#let(:u) { subject }
#end
it "should output the logging message" do
Foo.logger.debug("test").should =~ /\[debug\] test/
subject.logger.debug("test").should =~ /\[debug\] test/
subject.class.logger.debug("test").should =~ /\[debug\] test/
end

it "should allow to define the message using a template" do
pending
end
end

context Bar do
context Bar, "instance" do
subject { Bar.new }
it "should output the logging message" do
Bar.logger.debug("test").should =~ /\[debug\] test/
subject.logger.debug("test").should =~ /\[debug\] test/
subject.class.logger.debug("test").should =~ /\[debug\] test/
end
end
end

0 comments on commit 357f3c5

Please sign in to comment.