From 3e17bf5bfc159241303c3f69ce194d5c1d511692 Mon Sep 17 00:00:00 2001 From: James Healy Date: Wed, 29 Dec 2021 23:31:08 +1100 Subject: [PATCH] Change two recently added classes to strict typing New code may as well go all in on type hints. I'll be interested to see if this helps with maintainability over time. --- lib/pdf/reader/point.rb | 2 +- lib/pdf/reader/rectangle.rb | 4 ++-- rbi/pdf-reader.rbi | 23 +++++++++++++++++++++-- 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/lib/pdf/reader/point.rb b/lib/pdf/reader/point.rb index 0312d18e..ec15f885 100644 --- a/lib/pdf/reader/point.rb +++ b/lib/pdf/reader/point.rb @@ -1,5 +1,5 @@ # coding: utf-8 -# typed: true +# typed: strict # frozen_string_literal: true module PDF diff --git a/lib/pdf/reader/rectangle.rb b/lib/pdf/reader/rectangle.rb index 16edae58..aca9c95f 100644 --- a/lib/pdf/reader/rectangle.rb +++ b/lib/pdf/reader/rectangle.rb @@ -1,5 +1,5 @@ # coding: utf-8 -# typed: true +# typed: strict # frozen_string_literal: true module PDF @@ -85,7 +85,7 @@ def apply_rotation(degrees) new_x2 = bottom_left.x new_y2 = bottom_left.y + width end - set_corners(new_x1, new_y1, new_x2, new_y2) + set_corners(new_x1 || 0, new_y1 || 0, new_x2 || 0, new_y2 || 0) end private diff --git a/rbi/pdf-reader.rbi b/rbi/pdf-reader.rbi index e2ee3a37..f8c7ca02 100644 --- a/rbi/pdf-reader.rbi +++ b/rbi/pdf-reader.rbi @@ -1110,7 +1110,10 @@ module PDF y: Numeric, ).void end - def initialize(x, y); end + def initialize(x, y) + @x = T.let(0, Numeric) + @y = T.let(0, Numeric) + end sig { returns(Numeric) } def x; end @@ -1138,6 +1141,7 @@ module PDF end class Rectangle + sig { params(arr: T::Array[Numeric]).returns(PDF::Reader::Rectangle) } def self.from_array(arr); end @@ -1149,7 +1153,13 @@ module PDF y2: Numeric ).void end - def initialize(x1, y1, x2, y2); end + + def initialize(x1, y1, x2, y2) + @bottom_left = T.let(PDF::Reader::Point.new(0,0), PDF::Reader::Point) + @bottom_right = T.let(PDF::Reader::Point.new(0,0), PDF::Reader::Point) + @top_left = T.let(PDF::Reader::Point.new(0,0), PDF::Reader::Point) + @top_right = T.let(PDF::Reader::Point.new(0,0), PDF::Reader::Point) + end sig { returns(PDF::Reader::Point) } def bottom_left; end @@ -1174,6 +1184,15 @@ module PDF sig { params(degrees: Integer).void } def apply_rotation(degrees); end + + sig { params(point: PDF::Reader::Point).void } + def contains?(point); end + + sig { params(other: PDF::Reader::Rectangle).void } + def ==(other); end + + sig { params(x1: Numeric, y1: Numeric, x2: Numeric, y2: Numeric).void } + def set_corners(x1, y1, x2, y2); end end class Reference