Class: Instant::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/instant/context.rb

Instance Method Summary collapse

Constructor Details

#initializeContext

Returns a new instance of Context.



40
41
42
43
44
45
46
47
48
49
# File 'lib/instant/context.rb', line 40

def initialize
  @stringio = StringIO.new
  @assigns = Set.new
  @loop_counter = 0
  @logger = Logger.new(@stringio)
  @logger.formatter = proc { |severity, datetime, progname, msg| "#{msg}\n" }

  @log_collectors = []
  @loop_counter   = 0
end

Instance Method Details

#closeObject



82
83
84
85
86
# File 'lib/instant/context.rb', line 82

def close
  while collector = @log_collectors.pop
    @logger.info collector.to_s
  end
end

#log_assign(name, value) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/instant/context.rb', line 51

def log_assign(name, value)
  if @log_collectors.size > 0
    @log_collectors.last.append name, value
  else
    @assigns << name
    @logger.info "#{name.to_s.ljust(8)} = #{value.to_s.center(5)}"
  end
  return value
end

#loop_beginObject



61
62
63
# File 'lib/instant/context.rb', line 61

def loop_begin
  @log_collectors.push LogCollector.new(@assigns)
end

#loop_endObject



77
78
79
80
# File 'lib/instant/context.rb', line 77

def loop_end
  collector = @log_collectors.pop
  @logger.info collector.to_s
end

#loop_inside_beginObject



65
66
# File 'lib/instant/context.rb', line 65

def loop_inside_begin
end

#loop_inside_endObject



68
69
70
71
72
73
74
75
# File 'lib/instant/context.rb', line 68

def loop_inside_end
  @log_collectors.last.fill_empty
  @loop_counter = @loop_counter + 1

  if @loop_counter > 1000
    raise ::Instant::LoopTooDeepError.new("Loop too much")
  end
end

#to_sObject



88
89
90
# File 'lib/instant/context.rb', line 88

def to_s
  @stringio.string
end