Class: Region

Inherits:
Object
  • Object
show all
Defined in:
lib/capybara/screenshot/diff/region.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x, y, width, height) ⇒ Region

Returns a new instance of Region.



6
7
8
# File 'lib/capybara/screenshot/diff/region.rb', line 6

def initialize(x, y, width, height)
  @x, @y, @width, @height = x, y, width, height
end

Instance Attribute Details

#heightObject

Returns the value of attribute height.



4
5
6
# File 'lib/capybara/screenshot/diff/region.rb', line 4

def height
  @height
end

#widthObject

Returns the value of attribute width.



4
5
6
# File 'lib/capybara/screenshot/diff/region.rb', line 4

def width
  @width
end

#xObject

Returns the value of attribute x.



4
5
6
# File 'lib/capybara/screenshot/diff/region.rb', line 4

def x
  @x
end

#yObject

Returns the value of attribute y.



4
5
6
# File 'lib/capybara/screenshot/diff/region.rb', line 4

def y
  @y
end

Class Method Details

.from_edge_coordinates(left, top, right, bottom) ⇒ Object



10
11
12
13
14
15
# File 'lib/capybara/screenshot/diff/region.rb', line 10

def self.from_edge_coordinates(left, top, right, bottom)
  return nil unless left && top && right && bottom
  return nil if right < left || bottom < top

  Region.new(left, top, right - left, bottom - top)
end

Instance Method Details

#==(other) ⇒ Object

need to add this method to make it work with assert_equal



97
98
99
100
101
102
103
104
105
106
# File 'lib/capybara/screenshot/diff/region.rb', line 97

def ==(other)
  case other
  when Region
    x == other.x && y == other.y && width == other.width && height == other.height
  when Array
    to_a == other
  else
    false
  end
end

#blank?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/capybara/screenshot/diff/region.rb', line 84

def blank?
  empty?
end

#bottomObject



29
30
31
# File 'lib/capybara/screenshot/diff/region.rb', line 29

def bottom
  y + height
end

#cover?(x, y) ⇒ Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/capybara/screenshot/diff/region.rb', line 76

def cover?(x, y)
  left <= x && x <= right && top <= y && y <= bottom
end

#empty?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/capybara/screenshot/diff/region.rb', line 80

def empty?
  width.zero? || height.zero?
end

#find_intersect_with(region) ⇒ Object



52
53
54
55
56
57
58
59
# File 'lib/capybara/screenshot/diff/region.rb', line 52

def find_intersect_with(region)
  return nil unless intersect?(region)

  new_left = [x, region.x].max
  new_top = [y, region.y].max

  Region.new(new_left, new_top, [right, region.right].min - new_left, [bottom, region.bottom].min - new_top)
end

#find_relative_intersect(region) ⇒ Object



69
70
71
72
73
74
# File 'lib/capybara/screenshot/diff/region.rb', line 69

def find_relative_intersect(region)
  intersect = find_intersect_with(region)
  return nil unless intersect

  intersect.move_by(-x, -y)
end

#inspectObject



92
93
94
# File 'lib/capybara/screenshot/diff/region.rb', line 92

def inspect
  "Region(x: #{x}, y: #{y}, width: #{width}, height: #{height})"
end

#intersect?(region) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/capybara/screenshot/diff/region.rb', line 61

def intersect?(region)
  left <= region.right && right >= region.left && top <= region.bottom && bottom >= region.top
end

#leftObject



33
34
35
# File 'lib/capybara/screenshot/diff/region.rb', line 33

def left
  x
end

#move_by(right_by, down_by) ⇒ Object



65
66
67
# File 'lib/capybara/screenshot/diff/region.rb', line 65

def move_by(right_by, down_by)
  Region.new(x + right_by, y + down_by, width, height)
end

#present?Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/capybara/screenshot/diff/region.rb', line 88

def present?
  !empty?
end

#rightObject



37
38
39
# File 'lib/capybara/screenshot/diff/region.rb', line 37

def right
  x + width
end

#sizeObject



41
42
43
44
45
46
# File 'lib/capybara/screenshot/diff/region.rb', line 41

def size
  return 0 if width < 0 || height < 0

  result = width * height
  result.zero? ? 1 : result
end

#to_aObject



48
49
50
# File 'lib/capybara/screenshot/diff/region.rb', line 48

def to_a
  [@x, @y, @width, @height]
end

#to_edge_coordinatesObject



17
18
19
# File 'lib/capybara/screenshot/diff/region.rb', line 17

def to_edge_coordinates
  [left, top, right, bottom]
end

#to_top_left_corner_coordinatesObject



21
22
23
# File 'lib/capybara/screenshot/diff/region.rb', line 21

def to_top_left_corner_coordinates
  [x, y, width, height]
end

#topObject



25
26
27
# File 'lib/capybara/screenshot/diff/region.rb', line 25

def top
  y
end