Skip to content

Commit

Permalink
Expanded table lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
xea committed Jan 10, 2017
1 parent e6b4115 commit fe88aa9
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 9 deletions.
2 changes: 1 addition & 1 deletion config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
watch_dir: packages
scan_interval: 5
:console:
mode: normal
mode: dumb
22 changes: 19 additions & 3 deletions lib/console/interpreter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,25 @@ def lookup_arg(key, arg)
end

# Perform a key lookup in the registered data tables
def table_lookup(key, arg)
table = @tables.find { |id, current_table| current_table.has_key? arg.to_sym }
table[1][arg.to_sym] unless table.nil?
def table_lookup(key, arg, table = nil)
lookup_key = key[1..-1].to_sym

lookup_table = table || @tables[lookup_key]

if lookup_table.kind_of? Hash
lookup_table[arg.to_sym]
elsif lookup_table.kind_of? Array
if lookup_table.member? arg or lookup_table.member? arg.to_sym
arg
else
raise "Invalid argument"
end
elsif lookup_table.kind_of? Proc
table_lookup(key, arg, lookup_table.call)
else
raise "Unknown lookup table type"
end
#table[1][arg.to_sym] unless table.nil?
end

def attribute_lookup(key, arg)
Expand Down
2 changes: 2 additions & 0 deletions lib/console/signature.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ def expand(pattern, ctx = nil)

if table.kind_of? Array
table
elsif table.kind_of? Proc
table.call
else
# Hash or Expandable
table.keys
Expand Down
8 changes: 7 additions & 1 deletion packages/ar/ar_mode.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ class ActiveRecordMode < BaseMode
mode_id :activerecord
access_from :home, "ar :namespace_id?", "Enter ActiveRecord browser"

tables({
sample_vars: -> { [ "dynamic" ] }
})

register_command(:exit_mode, "exit", "Exit ActiveRecord browser") { |intp| intp.modes.exit_mode }
register_command(:list_model, "list :model_id", "List model instances")
register_command(:use_namespace, "use :namespace_id", "Use the current namespace")
Expand All @@ -22,7 +26,9 @@ class ActiveRecordMode < BaseMode
end
}

register_command(:show_associations, "show associations of :model_id", "Show associations of the selected model") { |intp, ar, out, model_id|
register_command(:show_associations, "show associations of $sample_vars", "Show associations of the selected model") { |intp, ar, out, sample_vars|
binding.pry
puts "#{current_models}"
model = @ns.lookup(model_id.to_sym)

if model.nil?
Expand Down
26 changes: 22 additions & 4 deletions spec/console/interpreter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ class IntTestMode < BaseMode

mode_id :test

tables({
array_lookup: [ "apple", "pear", "strawberry" ],
hash_lookup: { red: '#f00', green: '#0f0', blue: '#00f' },
lambda_lookup: -> { [ "dynamic" ] },
lambda_hash_lookup: -> { { apple: "red", grape: "blue", banana: "yellow" } }
})

register_command(:test_cmd, "test", "help")
register_command(:test_param, "param :a", "help")
register_command(:test_inline, "inline", "help") { @calls << [ :test_inline ]; :test_inline_success }
Expand Down Expand Up @@ -43,16 +50,16 @@ def test_param(a)

context "#sanitize_input" do
it 'should leave sanitised inputs as they are' do
expect(@intp.sanitize_input "a").to eq("a")
expect(@intp.sanitize_input "a").to eq(["a", :basic])
end

it 'should remove leading and trailing whitespaces' do
expect(@intp.sanitize_input ' spaces ').to eq('spaces')
expect(@intp.sanitize_input "\ttab\t").to eq('tab')
expect(@intp.sanitize_input ' spaces ').to eq(['spaces', :basic])
expect(@intp.sanitize_input "\ttab\t").to eq(['tab', :basic])
end

it 'should leave quoted parts as they are' do
expect(@intp.sanitize_input " \"\tquoted and voted \" ").to eq("\"\tquoted and voted \"")
expect(@intp.sanitize_input " \"\tquoted and voted \" ").to eq(["\"\tquoted and voted \"", :basic])
end
end

Expand All @@ -76,6 +83,17 @@ def test_param(a)
end

context "#lookup_args" do
it 'should return the same key for array tables when the element is defined' do
expect(@intp.lookup_args({ ':just_something' => "foo"})).to eq({ just_something: "foo" })
expect(@intp.lookup_args({ '$array_lookup' => "apple" })).to eq({ array_lookup: "apple" })
expect(@intp.lookup_args({ '$array_lookup' => "pear" })).to eq({ array_lookup: "pear" })
expect(@intp.lookup_args({ '$array_lookup' => "strawberry" })).to eq({ array_lookup: "strawberry" })
expect(@intp.lookup_args({ '$hash_lookup' => "red"})).to eq({ hash_lookup: '#f00' })
expect(@intp.lookup_args({ '$hash_lookup' => "green"})).to eq({ hash_lookup: '#0f0' })
expect(@intp.lookup_args({ '$hash_lookup' => "blue"})).to eq({ hash_lookup: '#00f' })
expect(@intp.lookup_args({ '$lambda_lookup' => "dynamic"})).to eq({ lambda_lookup: 'dynamic' })
expect(@intp.lookup_args({ '$lambda_hash_lookup' => "apple"})).to eq({ lambda_hash_lookup: 'red' })
end
end

context "#table_lookup" do
Expand Down

0 comments on commit fe88aa9

Please sign in to comment.