Sha256: 86cfdaeef683cd1a7ce5e4fc4674245e262d21d094cde98db481707fd782c359

Contents?: true

Size: 1.16 KB

Versions: 9

Compression:

Stored size: 1.16 KB

Contents

require "ostruct"

class CompareLinker
  class Formatter
    class Base
      attr_reader :g, :downgraded

      def format(gem_info)
        @g = OpenStruct.new(gem_info)
        @downgraded = downgrade?(g.old_ver, g.new_ver, g.old_tag, g.new_tag, g.old_rev, g.new_rev)
        post_format
      end

      private

      def post_format
        raise NotImplementedError
      end

      def downgrade?(old_ver, new_ver, old_tag, new_tag, old_rev, new_rev)
        Gem::Version.new(new_ver) < Gem::Version.new(old_ver) ||
          (old_tag && new_tag && Gem::Version.new(to_ver(new_tag)) < Gem::Version.new(to_ver(old_tag))) ||
          (old_rev && new_rev && Gem::Version.new(to_ver(new_rev)) < Gem::Version.new(to_ver(old_rev)))
      end

      def to_ver(tag)
        return $1 if tag =~ /(\d+(?:\.\d+)+)\z/
      end

      def github_url(repo_owner, repo_name)
        "https://github.com/#{repo_owner}/#{repo_name}"
      end

      def github_compare_url(repo_owner, repo_name, old_tag, new_tag)
        range = downgraded ? "#{new_tag}...#{old_tag}" : "#{old_tag}...#{new_tag}"
        "#{github_url(repo_owner, repo_name)}/compare/#{range}"
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
compare_linker-1.4.0 lib/compare_linker/formatter/base.rb
compare_linker-1.3.8 lib/compare_linker/formatter/base.rb
compare_linker-1.3.7 lib/compare_linker/formatter/base.rb
compare_linker-1.3.6 lib/compare_linker/formatter/base.rb
compare_linker-1.3.5 lib/compare_linker/formatter/base.rb
compare_linker-1.3.4 lib/compare_linker/formatter/base.rb
compare_linker-1.3.3 lib/compare_linker/formatter/base.rb
compare_linker-1.3.2 lib/compare_linker/formatter/base.rb
compare_linker-1.3.1 lib/compare_linker/formatter/base.rb