Module: RackConsole::AppHelpers

Includes:
Configuration, ExprHelpers, Formatting, MethodIntrospection
Defined in:
lib/rack_console/app_helpers.rb

Constant Summary

Constants included from MethodIntrospection

MethodIntrospection::DUMMY_SOURCE_LOCATION

Instance Method Summary collapse

Methods included from ExprHelpers

#expr_for_method, #expr_for_module, #expr_for_object, #expr_for_object!

Methods included from Configuration

#check_access!, #css_dir, #eval_target, #find_template, #has_console_access?, #has_file_access?, #layout, #locals, #server_info, #unauthorized!, #url_root

Methods included from MethodIntrospection

#instance_method_names, #match_pred, #methods_for_module, #methods_for_module_by_kind, #methods_matching, #methods_within_file, #singleton_method_names, #sort_methods!, #sort_methods_by_source_location!

Methods included from Formatting

#ansi2html, #e, #file_line_to_href, #file_name_tag, #format_as_terminal, #format_backtrace, #format_method, #format_methods, #format_module, #format_object, #format_other, #format_source_location, #h, #href_to_file_line, #limit_string, #literal_tag, #method_href, #method_name_tag, #module_name_tag, #safe_format, #safe_format_structured, #safe_pp, #source_file, #source_file_methods_href, #wrap_line, #wrap_lines

Instance Method Details

#capture_stdio!Object



87
88
89
90
91
92
93
94
95
96
# File 'lib/rack_console/app_helpers.rb', line 87

def capture_stdio!
  @captured_stdio = true
  _stdin, _stdout, _stderr = $stdin, $stdout, $stderr
  $stdin, $stdout, $stderr = @stdin, @stdout, @stderr
  begin
    yield
  ensure
    $stdin, $stdout, $stderr = _stdin, _stdout, _stderr
  end
end

#console!Object



21
22
23
# File 'lib/rack_console/app_helpers.rb', line 21

def console!
  haml :console, locals: locals, layout: layout
end

#const_get_safe(m, name) ⇒ Object



145
146
147
148
149
# File 'lib/rack_console/app_helpers.rb', line 145

def const_get_safe m, name
  m.const_get(name)
rescue Object
  "ERROR: #{$!.inspect}"
end

#eval_expr(et, expr) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/rack_console/app_helpers.rb', line 45

def eval_expr et, expr
  expr_str = "begin; #{@expr} \n; end"
  case et
  when nil, false
    eval(expr_str)
  when Module
    et.module_eval(expr_str)
  else
    et.instance_eval(expr_str)
  end
end

#evaluate_expr!Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rack_console/app_helpers.rb', line 25

def evaluate_expr!
  return if @result_evaled
  result_capture! do
    @stdin  = StringIO.new('')
    @stdout = StringIO.new('')
    @stderr = StringIO.new('')
    @result_ok = false
    @expr = (params[:expr] || '').strip
    unless @expr.blank?
      @result_evaled = true
      Timeout.timeout(config[:eval_timeout] || 120) do
        capture_stdio! do
          @result = eval_expr(eval_target, @expr)
          @result_ok = true
        end
      end
    end
  end
end

#evaluate_method!Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/rack_console/app_helpers.rb', line 68

def evaluate_method!
  evaluate_expr!
  @show_stdio = @show_result = false
  if @result_ok && @result.is_a?(Module)
    result_capture! do
      @module = @result
      @method_name = params[:name]
      @method_kind = params[:kind].to_s =~ /i/ ? :instance_method : :method
      @method = @module.send(@method_kind, @method_name) rescue nil
      unless @method
        @method = @module.send(:method, @method_name)
        @method_kind = :method
      end
      @result = @method
      expr_for_object! @method, @module, @method_kind
    end
  end
end

#evaluate_methods!Object



98
99
100
101
102
103
# File 'lib/rack_console/app_helpers.rb', line 98

def evaluate_methods!
  @methods = nil
  result_capture! do
    @methods = methods_matching(params)
  end
end

#evaluate_module!Object



57
58
59
60
61
62
63
64
65
66
# File 'lib/rack_console/app_helpers.rb', line 57

def evaluate_module!
  evaluate_expr!
  @show_stdio = @show_result = false
  if @result_ok && @result.is_a?(Module)
    result_capture! do
      @module = @result
      expr_for_object! @module
    end
  end
end

#prepare_file!Object



105
106
107
108
109
110
111
112
113
114
115
# File 'lib/rack_console/app_helpers.rb', line 105

def prepare_file!
  path = params[:splat][0]
  file, line = href_to_file_line(path)
  result_capture! do
    unless has_file_access? file
      content_type 'text/plain'
      return "NOT A LOADABLE FILE"
    end
    @source_file = SourceFile.new([ file, line ]).load!
  end
end

#result_capture!Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/rack_console/app_helpers.rb', line 117

def result_capture!
  @result_ok = false
  result = yield
  @result_ok = true
  result
rescue
  @error = $!
  @error_description = @error.inspect
ensure
  @result_extended = @result.singleton_class.included_modules rescue nil
  @result_class = @result.class.name
  if @is_module = (::Module === @result)
    @module = @result
    @ancestors = @module.ancestors.drop(1)
    if @is_class = (::Class === @module)
      @superclass = @module.superclass
      @subclasses = @module.subclasses.sort_by{|c| c.name || ''}
    end
    @constants = @module.constants(false).sort.map{|n| [ n, const_get_safe(@module, n) ]}
    @methods = methods_for_module(@module)
  end
  if @is_method = (::Method === @result || ::UnboundMethod === @result || MockMethod === @result)
    @method = @result
    @method_source_location = @method.source_location
    @method_source = @method_source_location && SourceFile.new(@method_source_location).load!.narrow_to_block!
  end
end

#with_accessObject



13
14
15
16
17
18
19
# File 'lib/rack_console/app_helpers.rb', line 13

def with_access
  if has_console_access?
    yield
  else
    raise Error, "not authorized"
  end
end