Class: GameMachine::Model

Inherits:
OpenStruct
  • Object
show all
Includes:
Commands
Defined in:
server/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 'server/lib/game_machine/model.rb', line 17

def attribute(*args)

end

.delete(id) ⇒ Object


21
22
23
# File 'server/lib/game_machine/model.rb', line 21

def delete(id)
  commands.datastore.delete(id)
end

.delete!(id) ⇒ Object


25
26
27
# File 'server/lib/game_machine/model.rb', line 25

def delete!(id)
  commands.datastore.delete!(id)
end

.find(id, timeout = 1000) ⇒ Object


38
39
40
41
42
43
44
45
# File 'server/lib/game_machine/model.rb', line 38

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


29
30
31
32
33
34
35
36
# File 'server/lib/game_machine/model.rb', line 29

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


48
49
50
51
52
53
54
55
56
# File 'server/lib/game_machine/model.rb', line 48

def from_entity(entity,type=:json_entity)
  if type == :json_storage
    json = entity.json_storage.json
  else
    json = entity.json_entity.json
  end

  self.from_hash(JSON.parse(json))
end

.from_hash(attributes) ⇒ Object


74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'server/lib/game_machine/model.rb', line 74

def from_hash(attributes)
  if klass = attributes.delete('klass')
    attributes = attributes.each_with_object({}) do |(k, v), h|
      if v.kind_of?(Hash)
        h[k] = from_hash(v)
      elsif v.kind_of?(Array)
        h[k] = v.collect do |e|
          e.kind_of?(Hash) ? from_hash(e) : e
        end
      else
        h[k] = v
      end
    end
    model = klass.constantize.new(attributes)
    model.id = model.unscoped_id
    model
  else
    OpenStruct.new(attributes)
  end
end

.id_scopeObject


70
71
72
# File 'server/lib/game_machine/model.rb', line 70

def id_scope
  @id_scope
end

.scope_for(id) ⇒ Object


58
59
60
61
62
63
64
# File 'server/lib/game_machine/model.rb', line 58

def scope_for(id)
  if id_scope
    "#{id_scope}|#{id}"
  else
    id
  end
end

.set_id_scope(scope) ⇒ Object


66
67
68
# File 'server/lib/game_machine/model.rb', line 66

def set_id_scope(scope)
  @id_scope = scope
end

Instance Method Details

#as_jsonObject


97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'server/lib/game_machine/model.rb', line 97

def as_json
  attributes['id'] = scoped_id
  attributes.merge!(:klass => self.class.name)
  attributes.each_with_object({}) do |(k, v), h|
    if v.kind_of?(OpenStruct) 
      h[k] = v.as_json
    elsif v.is_a?(Array)
      h[k] = v.collect do |e|
        e.kind_of?(OpenStruct) ? e.as_json : e
      end
    else
      h[k] = v
    end
  end
end

#attributesObject


117
118
119
# File 'server/lib/game_machine/model.rb', line 117

def attributes
  self.marshal_dump
end

#destroyObject


161
162
163
# File 'server/lib/game_machine/model.rb', line 161

def destroy
  commands.datastore.delete(scoped_id)
end

#destroy!Object


165
166
167
# File 'server/lib/game_machine/model.rb', line 165

def destroy!
  commands.datastore.delete!(scoped_id)
end

#saveObject


153
154
155
# File 'server/lib/game_machine/model.rb', line 153

def save
  commands.datastore.put(to_storage_entity)
end

#save!Object


157
158
159
# File 'server/lib/game_machine/model.rb', line 157

def save!
  commands.datastore.put!(to_storage_entity)
end

#scoped_idObject


121
122
123
124
125
126
127
# File 'server/lib/game_machine/model.rb', line 121

def scoped_id
  if self.class.id_scope
    self.class.scope_for(id)
  else
    id
  end
end

#to_entityObject


137
138
139
# File 'server/lib/game_machine/model.rb', line 137

def to_entity
  MessageLib::Entity.new.set_id(scoped_id).set_json_entity(to_json_entity)
end

#to_jsonObject


113
114
115
# File 'server/lib/game_machine/model.rb', line 113

def to_json
  JSON.generate(as_json)
end

#to_json_entityObject


141
142
143
# File 'server/lib/game_machine/model.rb', line 141

def to_json_entity
  MessageLib::JsonEntity.new.set_json(to_json).set_klass(self.class.name)
end

#to_json_storageObject


149
150
151
# File 'server/lib/game_machine/model.rb', line 149

def to_json_storage
  MessageLib::JsonStorage.new.set_json(to_json)
end

#to_storage_entityObject


145
146
147
# File 'server/lib/game_machine/model.rb', line 145

def to_storage_entity
  MessageLib::Entity.new.set_id(scoped_id).set_json_storage(to_json_storage)
end

#unscoped_idObject


129
130
131
132
133
134
135
# File 'server/lib/game_machine/model.rb', line 129

def unscoped_id
  if self.class.id_scope
    id.sub(/^#{self.class.id_scope}\|/,'')
  else
    id
  end
end