Skip to content

Commit

Permalink
specs for #as
Browse files Browse the repository at this point in the history
  • Loading branch information
chikamichi committed Jul 2, 2011
1 parent 42c85dc commit 3d427c7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
24 changes: 13 additions & 11 deletions lib/logg/core.rb
Expand Up @@ -84,8 +84,8 @@ def detect_syntax(options)

attr_reader :message, :namespace

# The Dispatcher is based on #method_missing. It auto-sets both the message
# and the namespace internally, then auto-sends the order +:output!+.
# The Dispatcher default behavior relies on #method_missing. It sets both the
# message and a namespace, then auto-sends the order to output.
#
def method_missing(meth, *args, &block)
@namespace = meth.to_s
Expand All @@ -94,9 +94,7 @@ def method_missing(meth, *args, &block)
end

def eigenclass
class << self
self
end
class << self; self; end
end

# Define a custom logger, using a template. The template may be defined
Expand Down Expand Up @@ -142,10 +140,10 @@ class << self
# TODO: memoize the Render instance somehow? Or find another trick to
# execute the block.
#
def as(method, *options, &block)
# TODO: respond_to? if/else
def as(method, &block)
raise ArgumentError, 'Missing mandatory block' unless block_given?

method = method.to_sym
options = options.first

# Define the guard at class-level, if not already defined.
if !eigenclass.respond_to?(method)
Expand All @@ -169,7 +167,9 @@ def as(method, *options, &block)

private

# TODO: handle the output with a templating system
# Default logging behavior. Outputs to $stdout using #puts and return
# the message.
#
def output!
output = "#{Time.now} | "
output += "[#{@namespace}] " unless @namespace.nil?
Expand All @@ -183,16 +183,18 @@ def output!
# simple meta-programming on this receiver to add support for the logger. It thus
# enable the receiver to use the logger's default implementation and/or define
# custom loggers.
#
module Machine
LOGGER = Logg::Dispatcher.new
NAME = (defined?(::Logg::LOG_METHOD) && ::Logg::LOG_METHOD) || :log
#NAME = (defined?(::Logg::LOG_METHOD) && ::Logg::LOG_METHOD) || :log
NAME = :log

def self.included(base)
if !base.respond_to?(NAME)
base.instance_eval do
# Memoized logger for the receiver's class.
#
# TODO: add support for defining the logger under a different name than #logger
# TODO: add support for defining the logger under a different name than #log,
# this means either defining a constant before mixin, or delaying the metaprog.
class << self; self; end.instance_eval do
define_method(NAME) do
Expand Down
14 changes: 11 additions & 3 deletions spec/logg_spec.rb
Expand Up @@ -73,9 +73,17 @@ def subject; Logg; end
it { should be_a Module }
end

context Logg::Machine do
def subject; Logg::Machine; end
it { should be_a Module }
context Logg::Dispatcher do
def subject; Logg::Dispatcher; end
it { should be_a Class }
describe '#as' do
it "should be provided the logger's name" do
lambda { subject.new.as }.should raise_error ArgumentError
end
it "should be provided a block defining the logger" do
lambda { subject.new.as(:toto) }.should raise_error ArgumentError, /missing/i
end
end
end

context Logg::Machine do
Expand Down

0 comments on commit 3d427c7

Please sign in to comment.