Class: Container
- Inherits:
-
Exist
show all
- Includes:
- Enumerable
- Defined in:
- lib/Olib/core/container.rb
Constant Summary
collapse
- TOPS =
%w(table)
Constants inherited
from Exist
Exist::GETTER, Exist::PATTERN
Instance Attribute Summary
Attributes inherited from Exist
#gameobj, #id
Instance Method Summary
collapse
Methods inherited from Exist
#==, #deconstruct_keys, #effects, #exists?, #fetch, fetch, #gone?, #method_missing, normalize_type_data, #respond_to_missing?, scan, #tags, #to_h, #to_s
Constructor Details
#initialize(obj) ⇒ Container
Returns a new instance of Container.
11
12
13
14
|
# File 'lib/Olib/core/container.rb', line 11
def initialize(obj)
super(obj)
fput "look in ##{obj.id}" unless GameObj.containers.fetch(id, false)
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
in the class Exist
Instance Method Details
#add(*items) ⇒ Object
54
55
56
57
58
59
60
|
# File 'lib/Olib/core/container.rb', line 54
def add(*items)
items.flatten.each do |item|
Command.try_or_fail(command: "_drag ##{item.id} ##{id}") do
contents.map(&:id).include?(item.id.to_s)
end
end
end
|
#check_contents ⇒ Object
16
17
18
|
# File 'lib/Olib/core/container.rb', line 16
def check_contents
fput TOPS.include?(noun) ? "look on ##{id}" : "look in ##{id}"
end
|
#closed? ⇒ Boolean
24
25
26
|
# File 'lib/Olib/core/container.rb', line 24
def closed?
not GameObj.containers[id]
end
|
#contents ⇒ Object
20
21
22
|
# File 'lib/Olib/core/container.rb', line 20
def contents
GameObj.containers.fetch(id, []).map do |item| Item.of(item, self) end
end
|
#each(&block) ⇒ Object
28
29
30
|
# File 'lib/Olib/core/container.rb', line 28
def each(&block)
contents.each(&block)
end
|
41
42
43
44
|
# File 'lib/Olib/core/container.rb', line 41
def find_by_tags(*tags)
tags = tags.map(&:to_sym)
contents.select do |item| (item.tags & tags).size.eql?(tags.size) end
end
|
#rummage ⇒ Object
46
47
48
|
# File 'lib/Olib/core/container.rb', line 46
def rummage
Rummage.new(self)
end
|
#to_json(*args) ⇒ Object
50
51
52
|
# File 'lib/Olib/core/container.rb', line 50
def to_json(*args)
{id: id, name: name, noun: noun, contents: contents}.to_json(*args)
end
|
#type(type = nil) ⇒ Object
36
37
38
39
|
# File 'lib/Olib/core/container.rb', line 36
def type(type = nil)
return super() if type.nil?
find_by_tags(type)
end
|
#where(**query) ⇒ Object
32
33
34
|
# File 'lib/Olib/core/container.rb', line 32
def where(**query)
contents.select(&Where[**query])
end
|