Class: PerfectShape::Square

Inherits:
Rectangle show all
Defined in:
lib/perfect_shape/square.rb

Constant Summary collapse

MESSAGE_WIDTH_AND_HEIGHT_AND_LENGTH_NOT_EQUAL =
'Square width, height, and length must all be equal if more than one is specified; or otherwise keep only one of them in arguments!'

Instance Attribute Summary collapse

Attributes included from RectangularShape

#height, #width

Attributes included from PointLocation

#x, #y

Instance Method Summary collapse

Methods inherited from Rectangle

#contain?

Methods included from RectangularShape

#max_x, #max_y

Methods included from PointLocation

#min_x, #min_y

Methods inherited from Shape

#==, #bounding_box, #center_x, #center_y, #height, #max_x, #max_y, #min_x, #min_y, #normalize_point, #width

Constructor Details

#initialize(x: 0, y: 0, length: nil, width: nil, height: nil) ⇒ Square

Constructs with x, y, length (optionally width or height can be passed as alias for length)



31
32
33
34
35
# File 'lib/perfect_shape/square.rb', line 31

def initialize(x: 0, y: 0, length: nil, width: nil, height: nil)
  raise MESSAGE_WIDTH_AND_HEIGHT_AND_LENGTH_NOT_EQUAL if (length && width && length != width) || (length && height && length != height) || (width && height && width != height)
  length ||= width || height || 1
  super(x: x, y: y, width: length, height: length)
end

Instance Attribute Details

#lengthObject

Returns the value of attribute length.



28
29
30
# File 'lib/perfect_shape/square.rb', line 28

def length
  @length
end

Instance Method Details

#height=(value) ⇒ Object



50
51
52
53
54
# File 'lib/perfect_shape/square.rb', line 50

def height=(value)
  super
  self.length = value unless length == value
  self.width = value unless width == value
end

#width=(value) ⇒ Object



44
45
46
47
48
# File 'lib/perfect_shape/square.rb', line 44

def width=(value)
  super
  self.length = value unless length == value
  self.height = value unless height == value
end