Class: Foreplay::Engine::Logger

Inherits:
Object
  • Object
show all
Defined in:
lib/foreplay/engine/logger.rb

Constant Summary collapse

INDENT =
4
MARGIN =
24

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(m, o = {}) ⇒ Logger

Returns a new instance of Logger.



9
10
11
12
13
14
# File 'lib/foreplay/engine/logger.rb', line 9

def initialize(m, o = {})
  @message = m
  @options = o

  output
end

Instance Attribute Details

#messageObject (readonly)

Returns the value of attribute message.



7
8
9
# File 'lib/foreplay/engine/logger.rb', line 7

def message
  @message
end

#optionsObject (readonly)

Returns the value of attribute options.



7
8
9
# File 'lib/foreplay/engine/logger.rb', line 7

def options
  @options
end

Instance Method Details

#formatted_messageObject



20
21
22
23
24
25
# File 'lib/foreplay/engine/logger.rb', line 20

def formatted_message
  @formatted_message ||= header + message
                                  .gsub(/\A\s+/, '')
                                  .gsub(/\s+\z/, '')
                                  .gsub(/(\r\n|\r|\n)/, "\n#{margin}")
end

#headerObject



27
28
29
# File 'lib/foreplay/engine/logger.rb', line 27

def header
  @header ||= (margin_format % header_content[0, margin_width - 1]).white
end

#header_contentObject



31
32
33
# File 'lib/foreplay/engine/logger.rb', line 31

def header_content
  @header_content ||= (options[:host] || '')
end

#marginObject



35
36
37
# File 'lib/foreplay/engine/logger.rb', line 35

def margin
  @margin ||= margin_format % ''
end

#margin_formatObject



47
48
49
# File 'lib/foreplay/engine/logger.rb', line 47

def margin_format
  @margin_format ||= "%-#{margin_width}s"
end

#margin_widthObject



43
44
45
# File 'lib/foreplay/engine/logger.rb', line 43

def margin_width
  @margin_width ||= MARGIN + INDENT * (options[:indent] || 0)
end

#outputObject



16
17
18
# File 'lib/foreplay/engine/logger.rb', line 16

def output
  puts formatted_message unless silent?
end

#silent?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/foreplay/engine/logger.rb', line 39

def silent?
  @silent ||= (options[:silent] == true) || message.blank?
end