From 7904f9a69e2bcb9704b43b2fffda66f17c948c3b Mon Sep 17 00:00:00 2001 From: Tomas Strachota Date: Thu, 9 Jun 2016 16:56:24 +0200 Subject: [PATCH] Fixes #15257 - let print adapters decide whether to paginate --- config/cli_config.template.yml | 2 +- lib/hammer_cli/output/adapter/abstract.rb | 5 +++++ lib/hammer_cli/output/adapter/csv.rb | 5 +++++ lib/hammer_cli/output/adapter/tree_structure.rb | 5 +++++ lib/hammer_cli/output/output.rb | 2 +- test/unit/output/adapter/abstract_test.rb | 4 ++++ test/unit/output/adapter/base_test.rb | 4 ++++ test/unit/output/adapter/csv_test.rb | 4 ++++ test/unit/output/adapter/json_test.rb | 4 ++++ test/unit/output/adapter/table_test.rb | 4 ++++ test/unit/output/adapter/yaml_test.rb | 4 ++++ 11 files changed, 41 insertions(+), 2 deletions(-) diff --git a/config/cli_config.template.yml b/config/cli_config.template.yml index 4742171a..1292ecd2 100644 --- a/config/cli_config.template.yml +++ b/config/cli_config.template.yml @@ -3,7 +3,7 @@ # Enable interactive queries? :interactive: true # Number of records listed per page - :per_page: 20 + # :per_page: 20 # Location of shell history file :history_file: '~/.hammer/history' diff --git a/lib/hammer_cli/output/adapter/abstract.rb b/lib/hammer_cli/output/adapter/abstract.rb index 193ca9f6..f802e144 100644 --- a/lib/hammer_cli/output/adapter/abstract.rb +++ b/lib/hammer_cli/output/adapter/abstract.rb @@ -9,6 +9,11 @@ def tags def initialize(context={}, formatters={}) @context = context @formatters = HammerCLI::Output::Formatters::FormatterLibrary.new(filter_formatters(formatters)) + @paginate_by_default = true + end + + def paginate_by_default? + !!@paginate_by_default end def print_message(msg, msg_params={}) diff --git a/lib/hammer_cli/output/adapter/csv.rb b/lib/hammer_cli/output/adapter/csv.rb index 2f0ac9b9..8bbf8b31 100644 --- a/lib/hammer_cli/output/adapter/csv.rb +++ b/lib/hammer_cli/output/adapter/csv.rb @@ -135,6 +135,11 @@ def is_id? end end + def initialize(context={}, formatters={}) + super + @paginate_by_default = false + end + def tags [:flat] end diff --git a/lib/hammer_cli/output/adapter/tree_structure.rb b/lib/hammer_cli/output/adapter/tree_structure.rb index a0dd1586..3caf672b 100644 --- a/lib/hammer_cli/output/adapter/tree_structure.rb +++ b/lib/hammer_cli/output/adapter/tree_structure.rb @@ -1,6 +1,11 @@ module HammerCLI::Output::Adapter class TreeStructure < Abstract + def initialize(context={}, formatters={}) + super + @paginate_by_default = false + end + def prepare_collection(fields, collection) collection.map do |element| render_fields(fields, element) diff --git a/lib/hammer_cli/output/output.rb b/lib/hammer_cli/output/output.rb index 46ab1f57..62ecc9df 100644 --- a/lib/hammer_cli/output/output.rb +++ b/lib/hammer_cli/output/output.rb @@ -66,7 +66,7 @@ def self.register_formatter(formatter, *field_types) def init_adapter(adapter_name) raise NameError unless self.class.adapters.has_key? adapter_name - self.class.adapters[adapter_name].new(context, self.class.formatters) + @adapter ||= self.class.adapters[adapter_name].new(context, self.class.formatters) end end diff --git a/test/unit/output/adapter/abstract_test.rb b/test/unit/output/adapter/abstract_test.rb index 6f2dfa40..e61bc88c 100644 --- a/test/unit/output/adapter/abstract_test.rb +++ b/test/unit/output/adapter/abstract_test.rb @@ -20,6 +20,10 @@ def tags end end + it "allows default pagination" do + adapter.paginate_by_default?.must_equal true + end + it "should filter formatters with incompatible tags" do HammerCLI::Output::Formatters::FormatterLibrary.expects(:new).with({ :type => [] }) diff --git a/test/unit/output/adapter/base_test.rb b/test/unit/output/adapter/base_test.rb index a7e1b3a9..7b24013b 100644 --- a/test/unit/output/adapter/base_test.rb +++ b/test/unit/output/adapter/base_test.rb @@ -5,6 +5,10 @@ let(:context) {{}} let(:adapter) { HammerCLI::Output::Adapter::Base.new(context, HammerCLI::Output::Output.formatters) } + it "allows default pagination" do + adapter.paginate_by_default?.must_equal true + end + context "print_collection" do let(:id) { Fields::Id.new(:path => [:id], :label => "Id") } diff --git a/test/unit/output/adapter/csv_test.rb b/test/unit/output/adapter/csv_test.rb index 40ad41e3..b8d21528 100644 --- a/test/unit/output/adapter/csv_test.rb +++ b/test/unit/output/adapter/csv_test.rb @@ -4,6 +4,10 @@ let(:adapter) { HammerCLI::Output::Adapter::CSValues.new } + it "forbids default pagination" do + adapter.paginate_by_default?.must_equal false + end + context "print_collection" do let(:field_name) { Fields::Field.new(:path => [:name], :label => "Name") } diff --git a/test/unit/output/adapter/json_test.rb b/test/unit/output/adapter/json_test.rb index 21eee663..29681dbc 100644 --- a/test/unit/output/adapter/json_test.rb +++ b/test/unit/output/adapter/json_test.rb @@ -5,6 +5,10 @@ let(:context) {{}} let(:adapter) { HammerCLI::Output::Adapter::Json.new(context, HammerCLI::Output::Output.formatters) } + it "forbids default pagination" do + adapter.paginate_by_default?.must_equal false + end + context "print_message" do it "prints the message" do params = { :a => 'Test', :b => 83 } diff --git a/test/unit/output/adapter/table_test.rb b/test/unit/output/adapter/table_test.rb index c674fabd..0ec7b497 100644 --- a/test/unit/output/adapter/table_test.rb +++ b/test/unit/output/adapter/table_test.rb @@ -4,6 +4,10 @@ let(:adapter) { HammerCLI::Output::Adapter::Table.new } + it "allows default pagination" do + adapter.paginate_by_default?.must_equal true + end + context "print_collection" do let(:field_name) { Fields::Field.new(:path => [:fullname], :label => "Name") } diff --git a/test/unit/output/adapter/yaml_test.rb b/test/unit/output/adapter/yaml_test.rb index 342a5c67..240a803d 100644 --- a/test/unit/output/adapter/yaml_test.rb +++ b/test/unit/output/adapter/yaml_test.rb @@ -5,6 +5,10 @@ let(:context) {{}} let(:adapter) { HammerCLI::Output::Adapter::Yaml.new(context, HammerCLI::Output::Output.formatters) } + it "forbids default pagination" do + adapter.paginate_by_default?.must_equal false + end + context "print_message" do it "prints the message" do params = { :a => 'Test', :b => 83 }