From 84e0f497e77bf3736166e3c6f7497d3d2a5d84fd Mon Sep 17 00:00:00 2001 From: tvallois Date: Tue, 14 Jan 2020 22:52:45 +0100 Subject: [PATCH 1/8] start to work on format on yard --- lib/annotate/annotate_models.rb | 18 ++++++++++++++++++ lib/annotate/constants.rb | 2 +- lib/annotate/parser.rb | 4 ++-- .../templates/auto_annotate_models.rake | 1 + lib/tasks/annotate_models.rake | 1 + .../lib/tasks/auto_annotate_models.rake | 1 + .../lib/tasks/auto_annotate_models.rake | 1 + 7 files changed, 25 insertions(+), 3 deletions(-) diff --git a/lib/annotate/annotate_models.rb b/lib/annotate/annotate_models.rb index 5debb6dac..73369e9e1 100644 --- a/lib/annotate/annotate_models.rb +++ b/lib/annotate/annotate_models.rb @@ -304,6 +304,14 @@ def get_schema_info(klass, header, options = {}) end if options[:format_rdoc] info << sprintf("# %-#{max_size}.#{max_size}s%s", "*#{col_name}*::", attrs.unshift(col_type).join(", ")).rstrip + "\n" + elsif options[:format_yard] + info << sprintf("# @!attribute #{col_name}") + "\n" + if col_type == 'boolean' + info << sprintf("# @return [TrueClass]") + "\n" + info << sprintf("# @return [FalseClass]") + "\n" + else + info << sprintf("# @return [#{map_col_type_to_ruby_classes(col_type)}]") + "\n" + end elsif options[:format_markdown] name_remainder = max_size - col_name.length - non_ascii_length(col_name) type_remainder = (md_type_allowance - 2) - col_type.length @@ -941,6 +949,16 @@ def non_ascii_length(string) end end + def map_col_type_to_ruby_classes(col_type) + case col_type + when 'integer' then Integer.to_s + when 'float' then Float.to_s + when 'decimal' then BigDecimal.to_s + when 'datetime', 'timestamp', 'time' then Time.to_s + when 'date' then Date.to_s + when 'text', 'string', 'binary', 'inet', 'uuid' then String.to_s + end + end class BadModelFileError < LoadError def to_s "file doesn't contain a valid model class" diff --git a/lib/annotate/constants.rb b/lib/annotate/constants.rb index bc7f6a01a..cd2148f39 100644 --- a/lib/annotate/constants.rb +++ b/lib/annotate/constants.rb @@ -14,7 +14,7 @@ module Constants FLAG_OPTIONS = [ :show_indexes, :simple_indexes, :include_version, :exclude_tests, :exclude_fixtures, :exclude_factories, :ignore_model_sub_dir, - :format_bare, :format_rdoc, :format_markdown, :sort, :force, :frozen, + :format_bare, :format_rdoc, :format_yard, :format_markdown, :sort, :force, :frozen, :trace, :timestamp, :exclude_serializers, :classified_sort, :show_foreign_keys, :show_complete_foreign_keys, :exclude_scaffolds, :exclude_controllers, :exclude_helpers, diff --git a/lib/annotate/parser.rb b/lib/annotate/parser.rb index 97b59551a..7f08939da 100644 --- a/lib/annotate/parser.rb +++ b/lib/annotate/parser.rb @@ -12,7 +12,7 @@ def self.parse(args, env = {}) ANNOTATION_POSITIONS = %w[before top after bottom].freeze FILE_TYPE_POSITIONS = %w[position_in_class position_in_factory position_in_fixture position_in_test position_in_routes position_in_serializer].freeze EXCLUSION_LIST = %w[tests fixtures factories serializers].freeze - FORMAT_TYPES = %w[bare rdoc markdown].freeze + FORMAT_TYPES = %w[bare rdoc yard markdown].freeze def initialize(args, env) @args = args @@ -196,7 +196,7 @@ def add_options_to_parser(option_parser) # rubocop:disable Metrics/MethodLength exclusions.each { |exclusion| env["exclude_#{exclusion}"] = 'yes' } end - option_parser.on('-f', '--format [bare|rdoc|markdown]', FORMAT_TYPES, 'Render Schema Infomation as plain/RDoc/Markdown') do |fmt| + option_parser.on('-f', '--format [bare|rdoc|yard|markdown]', FORMAT_TYPES, 'Render Schema Infomation as plain/RDoc/Yard/Markdown') do |fmt| env["format_#{fmt}"] = 'yes' end diff --git a/lib/generators/annotate/templates/auto_annotate_models.rake b/lib/generators/annotate/templates/auto_annotate_models.rake index 69e1e2a82..1f355249b 100644 --- a/lib/generators/annotate/templates/auto_annotate_models.rake +++ b/lib/generators/annotate/templates/auto_annotate_models.rake @@ -42,6 +42,7 @@ if Rails.env.development? 'skip_on_db_migrate' => 'false', 'format_bare' => 'true', 'format_rdoc' => 'false', + 'format_yard' => 'false', 'format_markdown' => 'false', 'sort' => 'false', 'force' => 'false', diff --git a/lib/tasks/annotate_models.rake b/lib/tasks/annotate_models.rake index dcb10892a..76d8cfe63 100644 --- a/lib/tasks/annotate_models.rake +++ b/lib/tasks/annotate_models.rake @@ -37,6 +37,7 @@ task annotate_models: :environment do options[:ignore_model_sub_dir] = Annotate::Helpers.true?(ENV['ignore_model_sub_dir']) options[:format_bare] = Annotate::Helpers.true?(ENV['format_bare']) options[:format_rdoc] = Annotate::Helpers.true?(ENV['format_rdoc']) + options[:format_yard] = Annotate::Helpers.true?(ENV['format_yard']) options[:format_markdown] = Annotate::Helpers.true?(ENV['format_markdown']) options[:sort] = Annotate::Helpers.true?(ENV['sort']) options[:force] = Annotate::Helpers.true?(ENV['force']) diff --git a/spec/integration/rails_4.1.1/lib/tasks/auto_annotate_models.rake b/spec/integration/rails_4.1.1/lib/tasks/auto_annotate_models.rake index 842eade72..d1983e31f 100644 --- a/spec/integration/rails_4.1.1/lib/tasks/auto_annotate_models.rake +++ b/spec/integration/rails_4.1.1/lib/tasks/auto_annotate_models.rake @@ -24,6 +24,7 @@ if Rails.env.development? 'skip_on_db_migrate' => "false", 'format_bare' => "true", 'format_rdoc' => "false", + 'format_yard' => "false", 'format_markdown' => "false", 'sort' => "false", 'force' => "false", diff --git a/spec/integration/rails_4.2.0/lib/tasks/auto_annotate_models.rake b/spec/integration/rails_4.2.0/lib/tasks/auto_annotate_models.rake index 842eade72..d1983e31f 100644 --- a/spec/integration/rails_4.2.0/lib/tasks/auto_annotate_models.rake +++ b/spec/integration/rails_4.2.0/lib/tasks/auto_annotate_models.rake @@ -24,6 +24,7 @@ if Rails.env.development? 'skip_on_db_migrate' => "false", 'format_bare' => "true", 'format_rdoc' => "false", + 'format_yard' => "false", 'format_markdown' => "false", 'sort' => "false", 'force' => "false", From 8857acef14c8f07b882ef9c1daf54b3093a5a069 Mon Sep 17 00:00:00 2001 From: tvallois Date: Wed, 15 Jan 2020 08:23:06 +0100 Subject: [PATCH 2/8] add json and jsonb to ruby classes --- lib/annotate/annotate_models.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/annotate/annotate_models.rb b/lib/annotate/annotate_models.rb index 73369e9e1..41f68721e 100644 --- a/lib/annotate/annotate_models.rb +++ b/lib/annotate/annotate_models.rb @@ -957,6 +957,7 @@ def map_col_type_to_ruby_classes(col_type) when 'datetime', 'timestamp', 'time' then Time.to_s when 'date' then Date.to_s when 'text', 'string', 'binary', 'inet', 'uuid' then String.to_s + when 'json', 'jsonb' then Hash.to_s end end class BadModelFileError < LoadError From bb4d4f07d4790f25f7cbf02292f857ab19d64b83 Mon Sep 17 00:00:00 2001 From: Thibault Date: Fri, 17 Jan 2020 14:46:36 +0100 Subject: [PATCH 3/8] map_col_type_to_ruby_classes should not be private --- lib/annotate/annotate_models.rb | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/lib/annotate/annotate_models.rb b/lib/annotate/annotate_models.rb index 41f68721e..080c68daf 100644 --- a/lib/annotate/annotate_models.rb +++ b/lib/annotate/annotate_models.rb @@ -904,6 +904,17 @@ def classified_sort(cols) ([id] << rest_cols << timestamps << associations).flatten.compact end + def map_col_type_to_ruby_classes(col_type) + case col_type + when 'integer' then Integer.to_s + when 'float' then Float.to_s + when 'decimal' then BigDecimal.to_s + when 'datetime', 'timestamp', 'time' then Time.to_s + when 'date' then Date.to_s + when 'text', 'string', 'binary', 'inet', 'uuid' then String.to_s + when 'json', 'jsonb' then Hash.to_s + end + end private def with_comments?(klass, options) @@ -949,17 +960,6 @@ def non_ascii_length(string) end end - def map_col_type_to_ruby_classes(col_type) - case col_type - when 'integer' then Integer.to_s - when 'float' then Float.to_s - when 'decimal' then BigDecimal.to_s - when 'datetime', 'timestamp', 'time' then Time.to_s - when 'date' then Date.to_s - when 'text', 'string', 'binary', 'inet', 'uuid' then String.to_s - when 'json', 'jsonb' then Hash.to_s - end - end class BadModelFileError < LoadError def to_s "file doesn't contain a valid model class" From 32c239b9a0cfae1b67004cadb9d098551c539b9b Mon Sep 17 00:00:00 2001 From: Thibault Date: Sat, 18 Jan 2020 16:22:38 +0100 Subject: [PATCH 4/8] add YARD Boolean type instead of FalseClass and TrueClass + specs --- Gemfile | 1 + lib/annotate/annotate_models.rb | 10 +++------- spec/lib/annotate/annotate_models_spec.rb | 20 ++++++++++++++++++++ 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/Gemfile b/Gemfile index cd3dae1be..01fa44113 100644 --- a/Gemfile +++ b/Gemfile @@ -16,6 +16,7 @@ end group :development, :test do gem 'byebug' + gem 'pry-byebug' gem 'guard-rspec', require: false gem 'rspec', require: false diff --git a/lib/annotate/annotate_models.rb b/lib/annotate/annotate_models.rb index 080c68daf..72b59badb 100644 --- a/lib/annotate/annotate_models.rb +++ b/lib/annotate/annotate_models.rb @@ -265,7 +265,7 @@ def get_schema_info(klass, header, options = {}) if col_type == 'decimal' col_type << "(#{col.precision}, #{col.scale})" elsif !%w[spatial geometry geography].include?(col_type) - if col.limit + if col.limit && !options[:format_yard] if col.limit.is_a? Array attrs << "(#{col.limit.join(', ')})" else @@ -306,12 +306,7 @@ def get_schema_info(klass, header, options = {}) info << sprintf("# %-#{max_size}.#{max_size}s%s", "*#{col_name}*::", attrs.unshift(col_type).join(", ")).rstrip + "\n" elsif options[:format_yard] info << sprintf("# @!attribute #{col_name}") + "\n" - if col_type == 'boolean' - info << sprintf("# @return [TrueClass]") + "\n" - info << sprintf("# @return [FalseClass]") + "\n" - else - info << sprintf("# @return [#{map_col_type_to_ruby_classes(col_type)}]") + "\n" - end + info << sprintf("# @return [#{map_col_type_to_ruby_classes(col_type)}]") + "\n" elsif options[:format_markdown] name_remainder = max_size - col_name.length - non_ascii_length(col_name) type_remainder = (md_type_allowance - 2) - col_type.length @@ -913,6 +908,7 @@ def map_col_type_to_ruby_classes(col_type) when 'date' then Date.to_s when 'text', 'string', 'binary', 'inet', 'uuid' then String.to_s when 'json', 'jsonb' then Hash.to_s + when 'boolean' then 'Boolean' end end private diff --git a/spec/lib/annotate/annotate_models_spec.rb b/spec/lib/annotate/annotate_models_spec.rb index 56195e352..8d83a4715 100644 --- a/spec/lib/annotate/annotate_models_spec.rb +++ b/spec/lib/annotate/annotate_models_spec.rb @@ -555,6 +555,26 @@ def mock_column(name, type, options = {}) EOS end + it 'should get schema info as YARD' do + klass = mock_class(:users, + :id, + [ + mock_column(:id, :integer), + mock_column(:name, :string, limit: 50), + ]) + expect(AnnotateModels.get_schema_info(klass, AnnotateModels::PREFIX, format_yard: true)).to eql(<<-EOS) +# #{AnnotateModels::PREFIX} +# +# Table name: users +# +# @!attribute id +# @return [Integer] +# @!attribute name +# @return [String] +# +EOS + end + it 'should get schema info as Markdown' do klass = mock_class(:users, :id, From 5720c4e57aa6a0a998df60b1ad9d91824e038965 Mon Sep 17 00:00:00 2001 From: Thibault Date: Sat, 18 Jan 2020 16:59:08 +0100 Subject: [PATCH 5/8] adding array support for yard --- lib/annotate/annotate_models.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/annotate/annotate_models.rb b/lib/annotate/annotate_models.rb index 72b59badb..e4d4c7d89 100644 --- a/lib/annotate/annotate_models.rb +++ b/lib/annotate/annotate_models.rb @@ -306,7 +306,8 @@ def get_schema_info(klass, header, options = {}) info << sprintf("# %-#{max_size}.#{max_size}s%s", "*#{col_name}*::", attrs.unshift(col_type).join(", ")).rstrip + "\n" elsif options[:format_yard] info << sprintf("# @!attribute #{col_name}") + "\n" - info << sprintf("# @return [#{map_col_type_to_ruby_classes(col_type)}]") + "\n" + ruby_class = col.respond_to?(:array) && col.array ? "Array<#{map_col_type_to_ruby_classes(col_type)}>": map_col_type_to_ruby_classes(col_type) + info << sprintf("# @return [#{ruby_class}]") + "\n" elsif options[:format_markdown] name_remainder = max_size - col_name.length - non_ascii_length(col_name) type_remainder = (md_type_allowance - 2) - col_type.length From e3b9cfefc93410bb5248693ddc0aa64adf16c5dd Mon Sep 17 00:00:00 2001 From: Thibault Date: Sat, 18 Jan 2020 17:14:56 +0100 Subject: [PATCH 6/8] make the ci pass --- .rubocop_todo.yml | 8 ++++---- Gemfile | 1 - lib/annotate/annotate_models.rb | 1 + spec/lib/annotate/annotate_models_spec.rb | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 0238dc4b4..5c0697b68 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -408,7 +408,7 @@ Lint/ShadowingOuterLocalVariable: # Offense count: 20 Metrics/AbcSize: - Max: 141 + Max: 155 # Offense count: 31 # Configuration parameters: CountComments, ExcludedMethods. @@ -423,16 +423,16 @@ Metrics/BlockNesting: # Offense count: 10 Metrics/CyclomaticComplexity: - Max: 36 + Max: 41 # Offense count: 30 # Configuration parameters: CountComments, ExcludedMethods. Metrics/MethodLength: - Max: 75 + Max: 80 # Offense count: 8 Metrics/PerceivedComplexity: - Max: 42 + Max: 47 # Offense count: 1 Naming/AccessorMethodName: diff --git a/Gemfile b/Gemfile index 01fa44113..cd3dae1be 100644 --- a/Gemfile +++ b/Gemfile @@ -16,7 +16,6 @@ end group :development, :test do gem 'byebug' - gem 'pry-byebug' gem 'guard-rspec', require: false gem 'rspec', require: false diff --git a/lib/annotate/annotate_models.rb b/lib/annotate/annotate_models.rb index e4d4c7d89..76baf2e54 100644 --- a/lib/annotate/annotate_models.rb +++ b/lib/annotate/annotate_models.rb @@ -912,6 +912,7 @@ def map_col_type_to_ruby_classes(col_type) when 'boolean' then 'Boolean' end end + private def with_comments?(klass, options) diff --git a/spec/lib/annotate/annotate_models_spec.rb b/spec/lib/annotate/annotate_models_spec.rb index 8d83a4715..c2006b2f4 100644 --- a/spec/lib/annotate/annotate_models_spec.rb +++ b/spec/lib/annotate/annotate_models_spec.rb @@ -557,7 +557,7 @@ def mock_column(name, type, options = {}) it 'should get schema info as YARD' do klass = mock_class(:users, - :id, + :id, [ mock_column(:id, :integer), mock_column(:name, :string, limit: 50), From cf303fde14b1a420708c7c106b8290fd9813219f Mon Sep 17 00:00:00 2001 From: Thibault Date: Wed, 22 Jan 2020 22:01:26 +0100 Subject: [PATCH 7/8] set map_col_type_to_ruby_classes to private --- lib/annotate/annotate_models.rb | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/lib/annotate/annotate_models.rb b/lib/annotate/annotate_models.rb index bfe8c3643..050392fc0 100644 --- a/lib/annotate/annotate_models.rb +++ b/lib/annotate/annotate_models.rb @@ -898,19 +898,6 @@ def classified_sort(cols) ([id] << rest_cols << timestamps << associations).flatten.compact end - def map_col_type_to_ruby_classes(col_type) - case col_type - when 'integer' then Integer.to_s - when 'float' then Float.to_s - when 'decimal' then BigDecimal.to_s - when 'datetime', 'timestamp', 'time' then Time.to_s - when 'date' then Date.to_s - when 'text', 'string', 'binary', 'inet', 'uuid' then String.to_s - when 'json', 'jsonb' then Hash.to_s - when 'boolean' then 'Boolean' - end - end - private def with_comments?(klass, options) @@ -954,6 +941,19 @@ def mb_chars_ljust(string, length) def non_ascii_length(string) string.to_s.chars.reject(&:ascii_only?).length end + + def map_col_type_to_ruby_classes(col_type) + case col_type + when 'integer' then Integer.to_s + when 'float' then Float.to_s + when 'decimal' then BigDecimal.to_s + when 'datetime', 'timestamp', 'time' then Time.to_s + when 'date' then Date.to_s + when 'text', 'string', 'binary', 'inet', 'uuid' then String.to_s + when 'json', 'jsonb' then Hash.to_s + when 'boolean' then 'Boolean' + end + end end class BadModelFileError < LoadError From 4f489b85e875d73f84efd70ed5cfd895e8e27d21 Mon Sep 17 00:00:00 2001 From: Thibault Date: Thu, 23 Jan 2020 10:16:01 +0100 Subject: [PATCH 8/8] remove whitespace --- lib/annotate/annotate_models.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/annotate/annotate_models.rb b/lib/annotate/annotate_models.rb index 1ebf9565b..d6e3a0866 100644 --- a/lib/annotate/annotate_models.rb +++ b/lib/annotate/annotate_models.rb @@ -947,7 +947,7 @@ def map_col_type_to_ruby_classes(col_type) when 'boolean' then 'Boolean' end end - + def columns(klass, options) cols = klass.columns cols += translated_columns(klass)