Module: SPQR::PrettyPrinter

Included in:
AppBoilerplateGenerator, ModelClassGenerator
Defined in:
lib/spqr/utils.rb

Instance Method Summary collapse

Instance Method Details

#dec_indentObject



34
35
36
# File 'lib/spqr/utils.rb', line 34

def dec_indent
  @indent = indent - 2
end

#inc_indentObject



30
31
32
# File 'lib/spqr/utils.rb', line 30

def inc_indent
  @indent = indent + 2
end

#indentObject



38
39
40
# File 'lib/spqr/utils.rb', line 38

def indent
  @indent ||= 0
end

#outfileObject



42
43
44
# File 'lib/spqr/utils.rb', line 42

def outfile
  @fstack[-1] or STDOUT
end

#pp(s) ⇒ Object



46
47
48
# File 'lib/spqr/utils.rb', line 46

def pp(s)
  outfile.puts "#{' ' * indent}#{s}\n"
end

#pp_call(callable, args) ⇒ Object



58
59
60
61
# File 'lib/spqr/utils.rb', line 58

def pp_call(callable, args)
  arg_repr = args.map {|arg| (arg.inspect if arg.kind_of? Hash) or arg}.join(', ')
  pp "#{callable}(#{arg_repr})"
end

#pp_decl(kind, name, etc = nil) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/spqr/utils.rb', line 50

def pp_decl(kind, name, etc=nil)
  pp "#{kind} #{name}#{etc}"
  inc_indent
  yield if block_given?
  dec_indent
  pp "end"
end

#pp_invoke(receiver, method, args) ⇒ Object



63
64
65
# File 'lib/spqr/utils.rb', line 63

def pp_invoke(receiver, method, args)
  pp_call "#{receiver}.#{method}", args
end

#stackObject



26
27
28
# File 'lib/spqr/utils.rb', line 26

def stack
  @fstack ||= [STDOUT]
end

#with_output_to(filename, &action) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/spqr/utils.rb', line 67

def with_output_to(filename, &action)
  File::open(filename, writemode) do |of|
    stack << of
    action.call      
    stack.pop
  end
end

#writemodeObject



22
23
24
# File 'lib/spqr/utils.rb', line 22

def writemode
  $PP_WRITEMODE ||= File::WRONLY|File::CREAT|File::TRUNC
end