Module: Moon::Rect::Cast

Defined in:
lib/moon/packages/std/rect.rb

Class Method Summary collapse

Class Method Details

.extract(obj) ⇒ Object


19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/moon/packages/std/rect.rb', line 19

def self.extract(obj)
  case obj
  when Array   then extract_array(obj)
  when Hash    then obj.fetch_multi(:x, :y, :w, :h)
  when Numeric then return 0, 0, obj, obj
  when Rect    then obj.to_a
  when Vector2 then return 0, 0, *obj
  when Vector4 then obj.to_a
  else
    raise TypeError,
          "wrong argument type #{obj.class.inspect} (expected Array, Hash, Numeric, Rect or Vector2)"
  end
end

.extract_array(obj) ⇒ Object


7
8
9
10
11
12
13
14
15
16
17
# File 'lib/moon/packages/std/rect.rb', line 7

def self.extract_array(obj)
  case obj.size
  # v2, v2
  when 2 then [*Vector2.extract(obj[0]), *Vector2.extract(obj[1])]
  # x, y, w, h
  when 4 then obj.to_a
  else
    raise ArgumentError,
          "wrong Array size #{obj.size} (expected 1, 2 or 4)"
  end
end