From fe88aa95ff92b2c6d8b7145a4263e0013369f568 Mon Sep 17 00:00:00 2001 From: Alex Pecsi Date: Tue, 10 Jan 2017 14:09:10 +0000 Subject: [PATCH] Expanded table lookup --- config/config.yml | 2 +- lib/console/interpreter.rb | 22 +++++++++++++++++++--- lib/console/signature.rb | 2 ++ packages/ar/ar_mode.rb | 8 +++++++- spec/console/interpreter_spec.rb | 26 ++++++++++++++++++++++---- 5 files changed, 51 insertions(+), 9 deletions(-) diff --git a/config/config.yml b/config/config.yml index 18e1c9d..8bff202 100644 --- a/config/config.yml +++ b/config/config.yml @@ -4,4 +4,4 @@ watch_dir: packages scan_interval: 5 :console: - mode: normal + mode: dumb diff --git a/lib/console/interpreter.rb b/lib/console/interpreter.rb index 4bf048a..190114f 100644 --- a/lib/console/interpreter.rb +++ b/lib/console/interpreter.rb @@ -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) diff --git a/lib/console/signature.rb b/lib/console/signature.rb index c0539bd..2955b82 100644 --- a/lib/console/signature.rb +++ b/lib/console/signature.rb @@ -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 diff --git a/packages/ar/ar_mode.rb b/packages/ar/ar_mode.rb index e29e46a..ff8d1c4 100644 --- a/packages/ar/ar_mode.rb +++ b/packages/ar/ar_mode.rb @@ -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") @@ -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? diff --git a/spec/console/interpreter_spec.rb b/spec/console/interpreter_spec.rb index b7480c2..5c6e95c 100644 --- a/spec/console/interpreter_spec.rb +++ b/spec/console/interpreter_spec.rb @@ -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 } @@ -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 @@ -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