forked from gsamokovarov/web-console
-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b6769aa
commit d42d509
Showing
5 changed files
with
83 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
module WebConsole | ||
# A context lets you get object names related to the current session binding. | ||
class Context | ||
def initialize(binding) | ||
@binding = binding | ||
end | ||
|
||
# Extracts entire objects which can be called by the current session unless the objpath is present. | ||
# Otherwise, it extracts methods and constants of the object specified by the objpath. | ||
def extract(objpath) | ||
if objpath.present? | ||
local(objpath) | ||
else | ||
global | ||
end | ||
end | ||
|
||
private | ||
|
||
GLOBAL_OBJECTS = [ | ||
'global_variables', | ||
'local_variables', | ||
'instance_variables', | ||
'instance_methods', | ||
'class_variables', | ||
'methods', | ||
'Object.constants', | ||
'Kernel.methods', | ||
] | ||
|
||
def global | ||
GLOBAL_OBJECTS.map { |cmd| eval(cmd) }.flatten | ||
end | ||
|
||
def local(objpath) | ||
[ | ||
eval("#{objpath}.methods").map { |m| "#{objpath}.#{m}" }, | ||
eval("#{objpath}.constants").map { |c| "#{objpath}::#{c}" }, | ||
].flatten | ||
end | ||
|
||
def eval(cmd) | ||
@binding.eval(cmd) rescue [] | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
require 'test_helper' | ||
|
||
module WebConsole | ||
class ContextTest < ActiveSupport::TestCase | ||
test '#extract(empty) includes local variables' do | ||
local_var = 'local' | ||
assert Context.new(binding).extract('').include?(:local_var) | ||
end | ||
|
||
test '#extract(empty) includes instance variables' do | ||
@instance_var = 'instance' | ||
assert Context.new(binding).extract('').include?(:@instance_var) | ||
end | ||
|
||
test '#extract(empty) includes global variables' do | ||
$global_var = 'global' | ||
assert Context.new(binding).extract('').include?(:$global_var) | ||
end | ||
|
||
test '#extract(obj) returns methods' do | ||
assert Context.new(binding).extract('Rails').include?('Rails.root') | ||
end | ||
|
||
test '#extract(obj) returns constants' do | ||
assert Context.new(binding).extract('WebConsole').include?('WebConsole::Middleware') | ||
end | ||
end | ||
end |