Class: Yell::Formatter::Modifier

Inherits:
Object
  • Object
show all
Defined in:
lib/yell/formatter.rb

Overview

Message modifier class to allow different modifiers for different requirements.

Instance Method Summary collapse

Constructor Details

#initializeModifier

Returns a new instance of Modifier.



129
130
131
# File 'lib/yell/formatter.rb', line 129

def initialize
  @repository = {}
end

Instance Method Details

#call(message) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/yell/formatter.rb', line 137

def call( message )
  case
  when mod = @repository[message.class] || @repository[message.class.to_s]
    mod.call(message)
  when message.is_a?(Array)
    message.map { |m| call(m) }.join(" ")
  when message.is_a?(Hash)
    message.map { |k, v| "#{k}: #{v}" }.join(", ")
  when message.is_a?(Exception)
    backtrace = message.backtrace ? "\n\t#{message.backtrace.join("\n\t")}" : ""
    sprintf("%s: %s%s", message.class, message.message, backtrace)
  else
    message
  end
end

#set(key, &block) ⇒ Object



133
134
135
# File 'lib/yell/formatter.rb', line 133

def set( key, &block )
  @repository.merge!(key => block)
end