Class: Warchat::Model

Inherits:
Object
  • Object
show all
Defined in:
lib/warchat/model.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Model

Returns a new instance of Model.



32
33
34
35
# File 'lib/warchat/model.rb', line 32

def initialize data
  @data = data
  self.class.send(:add,self)
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



30
31
32
# File 'lib/warchat/model.rb', line 30

def data
  @data
end

Class Method Details

.allObject



17
18
19
# File 'lib/warchat/model.rb', line 17

def all
  @all ||= {}
end

.find(data) ⇒ Object



8
9
10
11
# File 'lib/warchat/model.rb', line 8

def find data
  realm,name = data['r'],data['n']
  mutex.synchronize do all[realm] and all[realm][name] or nil end
end

.find_or_create(data) ⇒ Object



4
5
6
# File 'lib/warchat/model.rb', line 4

def find_or_create data
  find(data).tap do |m| m and m.update(data) end or new(data)
end

.mutexObject



13
14
15
# File 'lib/warchat/model.rb', line 13

def mutex
  @mutex ||= Mutex.new
end

Instance Method Details

#idObject



37
38
39
# File 'lib/warchat/model.rb', line 37

def id 
  [self.class.name.underscore,name,realm].join ':'
end

#nameObject



45
46
47
# File 'lib/warchat/model.rb', line 45

def name
  data['n']
end

#realmObject



49
50
51
# File 'lib/warchat/model.rb', line 49

def realm
  data['r']
end

#update(data) ⇒ Object



41
42
43
# File 'lib/warchat/model.rb', line 41

def update data
  @data = @data.merge data
end