Module: Moon::Cuboid::Cast

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

Class Method Summary collapse

Class Method Details

.extract(obj) ⇒ Object


29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/moon/packages/std/cuboid.rb', line 29

def self.extract(obj)
  case obj
  when Array   then extract_array(obj)
  when Cuboid  then obj.to_a
  when Hash    then extract_hash(obj)
  when Numeric then return 0, 0, 0, obj, obj, obj
  when Vector3 then return 0, 0, 0, *obj
  else
    raise TypeError,
          "wrong argument type #{obj.class} (expected Array, Cuboid, Hash, Numeric, Vector3)"
  end
end

.extract_array(obj) ⇒ Object


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

def self.extract_array(obj)
  case obj.size
  when 2
    pos, size = *obj
    return [*Vector3.extract(pos), *Vector3.extract(size)]
  when 6 then return obj
  else
    raise ArgumentError, "expected Array of size 2 or 6" if obj.size != 6
  end
end

.extract_hash(obj) ⇒ Object


17
18
19
20
21
22
23
24
25
26
27
# File 'lib/moon/packages/std/cuboid.rb', line 17

def self.extract_hash(obj)
  if obj.key?(:position)
    pos  = obj.fetch(:position)
    size = obj.fetch(:size)
    x, y, z = *Vector3.extract(pos)
    w, h, d = *Vector3.extract(size)
    return x, y, z, w, h, d
  else
    return obj.fetch_multi(:x, :y, :z, :w, :h, :d)
  end
end