Module: Containers

Defined in:
lib/Olib/core/containers.rb

Class Method Summary collapse

Class Method Details

.[](name) ⇒ Object



40
41
42
43
# File 'lib/Olib/core/containers.rb', line 40

def self.[](name)
  return define(name) if name.is_a?(Symbol)
  find_game_obj!(name)
end

.define(name) ⇒ Object



33
34
35
36
37
38
# File 'lib/Olib/core/containers.rb', line 33

def self.define(name)
  var = Vars[name.to_s] or fail Exception, "Var[#{name}] is not set\n\t;vars set #{name}=<whatever>"
  pattern = %r[#{var}]
  @@containers[name] = Containers.find_game_obj!(pattern)
  @@containers[name]
end

.find_game_obj!(pattern) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/Olib/core/containers.rb', line 6

def self.find_game_obj!(pattern)
  candidates = GameObj.inv.select do |item|
    if pattern.class.is_a?(String)
      item.name.include?(pattern)
    else
      item.name.match(pattern)
    end
  end
  case candidates.size
  when 1
    return Container.new(candidates.first)
  when 0
    fail Exception, <<~ERROR
      Source(GameObj.inv)

       reason: no matches for Pattern(#{pattern}) found in GameObj.inv
    ERROR
  else
    fail Exception, <<~ERROR
      Source(GameObj.inv)

       reason: aspecific Container[#{pattern.to_s}] found
      matches: #{candidates.map(&:name)}
    ERROR
  end
end

.left_handObject



49
50
51
# File 'lib/Olib/core/containers.rb', line 49

def self.left_hand
  Container.new(Char.left)
end

.method_missing(name, *args) ⇒ Object



57
58
59
60
# File 'lib/Olib/core/containers.rb', line 57

def self.method_missing(name, *args)
  return @@containers[name] if @@containers[name]
  return self.define(name)
end

.registryObject



53
54
55
# File 'lib/Olib/core/containers.rb', line 53

def self.registry
  @@containers
end

.right_handObject



45
46
47
# File 'lib/Olib/core/containers.rb', line 45

def self.right_hand
  Container.new(Char.right)
end