Class: GameMachine::Model
- Inherits:
-
OpenStruct
- Object
- OpenStruct
- GameMachine::Model
show all
- Includes:
- Commands
- Defined in:
- lib/game_machine/model.rb
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Commands
#commands
Class Method Details
.attribute(*args) ⇒ Object
17
18
19
|
# File 'lib/game_machine/model.rb', line 17
def attribute(*args)
end
|
.find(id, timeout = 1000) ⇒ Object
30
31
32
33
34
35
36
37
|
# File 'lib/game_machine/model.rb', line 30
def find(id,timeout=1000)
scoped_id = scope_for(id)
if entity = Commands::Base.commands.datastore.get(scoped_id,timeout)
from_entity(entity,:json_storage)
else
nil
end
end
|
.find!(id) ⇒ Object
21
22
23
24
25
26
27
28
|
# File 'lib/game_machine/model.rb', line 21
def find!(id)
scoped_id = scope_for(id)
if entity = Commands::Base.commands.datastore.get!(scoped_id)
from_entity(entity,:json_storage)
else
nil
end
end
|
.from_entity(entity, type = :json_entity) ⇒ Object
TODO cache klass names in hash to avoid constantize
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
# File 'lib/game_machine/model.rb', line 40
def from_entity(entity,type=:json_entity)
if type == :json_storage
json = entity.json_storage.json
else
json = entity.json_entity.json
end
attributes = JSON.parse(json)
if klass = attributes.delete('klass')
model = klass.constantize.new(attributes)
model.id = model.unscoped_id
model
else
raise "Unable to find klass attribute in #{attributes.inspect}"
end
end
|
.id_scope ⇒ Object
69
70
71
|
# File 'lib/game_machine/model.rb', line 69
def id_scope
@id_scope
end
|
.scope_for(id) ⇒ Object
57
58
59
60
61
62
63
|
# File 'lib/game_machine/model.rb', line 57
def scope_for(id)
if id_scope
"#{id_scope}|#{id}"
else
id
end
end
|
.set_id_scope(scope) ⇒ Object
65
66
67
|
# File 'lib/game_machine/model.rb', line 65
def set_id_scope(scope)
@id_scope = scope
end
|
Instance Method Details
#attributes ⇒ Object
74
75
76
|
# File 'lib/game_machine/model.rb', line 74
def attributes
self.marshal_dump
end
|
#save ⇒ Object
115
116
117
|
# File 'lib/game_machine/model.rb', line 115
def save
commands.datastore.put(to_storage_entity)
end
|
#save! ⇒ Object
119
120
121
|
# File 'lib/game_machine/model.rb', line 119
def save!
commands.datastore.put!(to_storage_entity)
end
|
#scoped_id ⇒ Object
78
79
80
81
82
83
84
|
# File 'lib/game_machine/model.rb', line 78
def scoped_id
if self.class.id_scope
self.class.scope_for(id)
else
id
end
end
|
#to_entity ⇒ Object
99
100
101
|
# File 'lib/game_machine/model.rb', line 99
def to_entity
MessageLib::Entity.new.set_id(scoped_id).set_json_entity(to_json_entity)
end
|
#to_json ⇒ Object
94
95
96
97
|
# File 'lib/game_machine/model.rb', line 94
def to_json
attributes['id'] = scoped_id
JSON.dump(attributes.merge(:klass => self.class.name))
end
|
#to_json_entity ⇒ Object
103
104
105
|
# File 'lib/game_machine/model.rb', line 103
def to_json_entity
MessageLib::JsonEntity.new.set_json(to_json).set_klass(self.class.name)
end
|
#to_json_storage ⇒ Object
111
112
113
|
# File 'lib/game_machine/model.rb', line 111
def to_json_storage
MessageLib::JsonStorage.new.set_json(to_json)
end
|
#to_storage_entity ⇒ Object
107
108
109
|
# File 'lib/game_machine/model.rb', line 107
def to_storage_entity
MessageLib::Entity.new.set_id(scoped_id).set_json_storage(to_json_storage)
end
|
#unscoped_id ⇒ Object
86
87
88
89
90
91
92
|
# File 'lib/game_machine/model.rb', line 86
def unscoped_id
if self.class.id_scope
id.sub(/^#{self.class.id_scope}\|/,'')
else
id
end
end
|