63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
# File 'lib/geometry/rectangle.rb', line 63
def self.new(*args)
options, args = args.partition {|a| a.is_a? Hash}
options = options.reduce({}, :merge)
if options.has_key?(:size)
if options.has_key?(:center)
CenteredRectangle.new(center: options[:center], size: options[:size])
elsif options.has_key?(:origin)
SizedRectangle.new(origin: options[:origin], size: options[:size])
else
SizedRectangle.new(size: options[:size])
end
elsif options.has_key?(:from) and options.has_key?(:to)
original_new(options[:from], options[:to])
elsif options.has_key?(:height) and options.has_key?(:width)
SizedRectangle.new(height: options[:height], width: options[:width])
elsif (2==args.count) and (args.all? {|a| a.is_a?(Array) || a.is_a?(Point) })
original_new(*args)
elsif options.empty?
raise ArgumentError, "#{self} arguments must be named, not: #{args}"
else
raise ArgumentError, "Bad Rectangle arguments: #{args}, #{options}"
end
end
|