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 constructor arguments!'

Constants inherited from Rectangle

Rectangle::OUT_BOTTOM, Rectangle::OUT_LEFT, Rectangle::OUT_RIGHT, Rectangle::OUT_TOP, Rectangle::RECT_INTERSECTS

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?, #edges, #empty?, #intersect?, #out_state, #to_path_shapes

Methods included from RectangularShape

#max_x, #max_y

Methods included from PointLocation

#first_point, #min_x, #min_y

Methods inherited from Shape

#==, #bounding_box, #center_point, #center_x, #center_y, #contain?, #height, #max_x, #max_y, #min_x, #min_y, #width

Constructor Details

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

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



32
33
34
35
36
37
38
# File 'lib/perfect_shape/square.rb', line 32

def initialize(x: 0, y: 0, length: nil, size: nil, width: nil, height: nil)
  raise MESSAGE_WIDTH_AND_HEIGHT_AND_LENGTH_NOT_EQUAL if (length && size && length != size)
  length ||= size
  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 Also known as: size

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



54
55
56
57
58
# File 'lib/perfect_shape/square.rb', line 54

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

#width=(value) ⇒ Object



48
49
50
51
52
# File 'lib/perfect_shape/square.rb', line 48

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