Class: Applitools::Location

Inherits:
Object
  • Object
show all
Defined in:
lib/applitools/core/location.rb

Constant Summary collapse

TOP_LEFT =
Location.new(0, 0).freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x, y) ⇒ Location

Returns a new instance of Location.



44
45
46
47
# File 'lib/applitools/core/location.rb', line 44

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

Instance Attribute Details

#xObject (readonly) Also known as: left

Returns the value of attribute x.



39
40
41
# File 'lib/applitools/core/location.rb', line 39

def x
  @x
end

#yObject (readonly) Also known as: top

Returns the value of attribute y.



39
40
41
# File 'lib/applitools/core/location.rb', line 39

def y
  @y
end

Class Method Details

.from_any_attribute(*args) ⇒ Object Also known as: for



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/applitools/core/location.rb', line 7

def from_any_attribute(*args)
  if args.size == 2
    new args[0], args[1]
  elsif args.size == 1
    value = args.shift
    from_hash(value) if value.is_a? Hash
    from_array(value) if value.is_a? Array
    from_string(value) if value.is_a? String
    from_struct(value) if value.respond_to?(:x) & value.respond_to?(:y)
  end
end

.from_array(value) ⇒ Object



25
26
27
# File 'lib/applitools/core/location.rb', line 25

def from_array(value)
  new value.shift, value.shift
end

.from_hash(value) ⇒ Object



21
22
23
# File 'lib/applitools/core/location.rb', line 21

def from_hash(value)
  new value[:x], value[:y]
end

.from_string(value) ⇒ Object



29
30
31
32
# File 'lib/applitools/core/location.rb', line 29

def from_string(value)
  x, y = value.split(/x/)
  new x, y
end

.from_struct(value) ⇒ Object



34
35
36
# File 'lib/applitools/core/location.rb', line 34

def from_struct(value)
  new value.x, value.y
end

Instance Method Details

#!=(other) ⇒ Object



60
61
62
# File 'lib/applitools/core/location.rb', line 60

def !=(other)
  !(self == other)
end

#==(other) ⇒ Object Also known as: eql?



55
56
57
58
# File 'lib/applitools/core/location.rb', line 55

def ==(other)
  return super.==(other) unless other.is_a?(Location)
  @x == other.x && @y == other.y
end

#hashObject



66
67
68
# File 'lib/applitools/core/location.rb', line 66

def hash
  @x.hash & @y.hash
end

#negative_partObject



70
71
72
73
74
# File 'lib/applitools/core/location.rb', line 70

def negative_part
  new_x = @x < 0 ? @x : 0
  new_y = @y < 0 ? @y : 0
  self.class.new(new_x.round, new_y.round)
end

#offset(other) ⇒ Object



90
91
92
93
94
# File 'lib/applitools/core/location.rb', line 90

def offset(other)
  @x += other.x
  @y += other.y
  self
end

#offset_negative(other) ⇒ Object



96
97
98
99
100
# File 'lib/applitools/core/location.rb', line 96

def offset_negative(other)
  @x -= other.x
  @y -= other.y
  self
end

#positive_partObject



76
77
78
79
80
# File 'lib/applitools/core/location.rb', line 76

def positive_part
  new_x = @x < 0 ? 0 : @x
  new_y = @y < 0 ? 0 : @y
  self.class.new(new_x.round, new_y.round)
end

#scale_it!(scale_factor) ⇒ Object

Raises:

  • (Applitools::EyesIllegalArgument)


102
103
104
105
106
107
# File 'lib/applitools/core/location.rb', line 102

def scale_it!(scale_factor)
  raise Applitools::EyesIllegalArgument, 'scale_factor must be an integer' unless scale_factor.is_a? Integer
  @x *= scale_factor
  @y *= scale_factor
  self
end

#to_hash(options = {}) ⇒ Object



82
83
84
# File 'lib/applitools/core/location.rb', line 82

def to_hash(options = {})
  options[:region] ? { left: left, top: top } : { x: x, y: y }
end

#to_sObject



51
52
53
# File 'lib/applitools/core/location.rb', line 51

def to_s
  "#{x} x #{y}"
end

#valuesObject



86
87
88
# File 'lib/applitools/core/location.rb', line 86

def values
  [x, y]
end