Class: Exist

Inherits:
Object
  • Object
show all
Defined in:
lib/Olib/core/exist.rb

Direct Known Subclasses

Container, Creature, Item, NPC, Scroll, Shop::Container, Transaction

Constant Summary collapse

GETTER =
%r[\w$]
PATTERN =
%r(<a exist=(?:'|")(?<id>.*?)(?:'|") noun=(?:'|")(?<noun>.*?)(?:'|")>(?<name>.*?)</a>)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(obj) ⇒ Exist

Returns a new instance of Exist.



25
26
27
28
29
30
31
32
# File 'lib/Olib/core/exist.rb', line 25

def initialize(obj)
  if obj.respond_to?(:id)
    @id = obj.id
  else
    @id = obj
  end
  fail Exception, "Id<#{@id}> was not of String|Fixnum" if @id.nil?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/Olib/core/exist.rb', line 42

def method_missing(method, *args)
  return nil if fetch.nil?

  if respond_to_missing?(method)
    fetch.send(method, *args)
  else
    super
  end
end

Instance Attribute Details

#gameobjObject (readonly)

Returns the value of attribute gameobj.



24
25
26
# File 'lib/Olib/core/exist.rb', line 24

def gameobj
  @gameobj
end

#idObject (readonly)

Returns the value of attribute id.



24
25
26
# File 'lib/Olib/core/exist.rb', line 24

def id
  @id
end

Class Method Details

.fetch(id) ⇒ Object



8
9
10
11
12
13
14
# File 'lib/Olib/core/exist.rb', line 8

def self.fetch(id)
  [ GameObj.inv, GameObj.containers.values, 
    GameObj.loot, GameObj.room_desc,
    GameObj.pcs, GameObj.npcs, 
    GameObj.right_hand, GameObj.left_hand ].flatten
                                           .find do |item| item.id.to_s.eql?(id.to_s) end
end

.normalize_type_data(type) ⇒ Object



20
21
22
# File 'lib/Olib/core/exist.rb', line 20

def self.normalize_type_data(type)
  (type or "").gsub(",", " ").split(" ").compact
end

.scan(str) ⇒ Object



16
17
18
# File 'lib/Olib/core/exist.rb', line 16

def self.scan(str)
  str.scan(PATTERN).map do |matches| Item.new(GameObj.new(*matches)) end
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



72
73
74
# File 'lib/Olib/core/exist.rb', line 72

def ==(other)
  id.to_s.eql?(other.id.to_s)
end

#deconstruct_keys(keys) ⇒ Object



82
83
84
85
86
# File 'lib/Olib/core/exist.rb', line 82

def deconstruct_keys(keys)
  keys.each_with_object({}) do |key, acc|
    acc[key] = self.send(key)
  end
end

#effectsObject



64
65
66
# File 'lib/Olib/core/exist.rb', line 64

def effects
  (status or "").split(",").map(&:to_sym)
end

#exists?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/Olib/core/exist.rb', line 52

def exists?
  not GameObj[id].nil?
end

#fetchObject



34
35
36
# File 'lib/Olib/core/exist.rb', line 34

def fetch()
  @gameobj ||= Exist.fetch(id)
end

#gone?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/Olib/core/exist.rb', line 56

def gone?
  not exists?
end

#respond_to_missing?(method, *args) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/Olib/core/exist.rb', line 38

def respond_to_missing?(method, *args)
  fetch.respond_to?(method) and method.to_s.match(GETTER) 
end

#tagsObject



60
61
62
# File 'lib/Olib/core/exist.rb', line 60

def tags
  Exist.normalize_type_data("#{type},#{sellable}").map(&:to_sym)
end

#to_hObject



68
69
70
# File 'lib/Olib/core/exist.rb', line 68

def to_h()
  {id: id, name: name, noun: noun}
end

#to_sObject



78
79
80
# File 'lib/Olib/core/exist.rb', line 78

def to_s()
  inspect()
end