Class: EventMachine::WebSocket::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/nali/connection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#browser_idObject

Returns the value of attribute browser_id.



5
6
7
# File 'lib/nali/connection.rb', line 5

def browser_id
  @browser_id
end

Instance Method Details

#[](name = nil) ⇒ Object



29
30
31
# File 'lib/nali/connection.rb', line 29

def []( name = nil )
  name ? ( storage[ name ] or nil ) : storage
end

#[]=(name, value) ⇒ Object



33
34
35
# File 'lib/nali/connection.rb', line 33

def []=( name, value )
  storage[ name ] = value
end

#all_tabsObject



7
8
9
10
11
# File 'lib/nali/connection.rb', line 7

def all_tabs
  Nali::Clients.list
    .select { |client| client.browser_id == self.browser_id }
    .each{ |client| yield( client ) if block_given? }
end

#app_run(method, params = nil) ⇒ Object



106
107
108
109
# File 'lib/nali/connection.rb', line 106

def app_run( method, params = nil )
  send_json action: :_appRun, method: method, params: params
  self
end

#call_method(method, model, params = nil) ⇒ Object



80
81
82
83
84
# File 'lib/nali/connection.rb', line 80

def call_method( method, model, params = nil )
  model = "#{ model.class.name }.#{ model.id }" if model.is_a?( ActiveRecord::Base )
  send_json action: :_callMethod, model: model, method: method, params: params
  self
end

#error(params) ⇒ Object



101
102
103
104
# File 'lib/nali/connection.rb', line 101

def error( params )
  notice :error, params
  self
end

#info(params) ⇒ Object



91
92
93
94
# File 'lib/nali/connection.rb', line 91

def info( params )
  notice :info, params
  self
end

#notice(method, params = nil) ⇒ Object



86
87
88
89
# File 'lib/nali/connection.rb', line 86

def notice( method, params = nil )
  call_method method, :Notice, params
  self
end

#other_tabsObject



13
14
15
16
17
# File 'lib/nali/connection.rb', line 13

def other_tabs
  Nali::Clients.list
    .select { |client| client != self and client.browser_id == self.browser_id }
    .each{ |client| yield( client ) if block_given? }
end

#resetObject



19
20
21
22
23
# File 'lib/nali/connection.rb', line 19

def reset
  @storage = {} 
  @watches = {} 
  self
end

#send_json(hash) ⇒ Object



61
62
63
64
# File 'lib/nali/connection.rb', line 61

def send_json( hash )
  send hash.to_json
  self
end

#storageObject



25
26
27
# File 'lib/nali/connection.rb', line 25

def storage
  @storage ||= {} 
end

#sync(*models) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/nali/connection.rb', line 66

def sync( *models )
  models.flatten.compact.each do |model|
    if watch_time( model ) < model.updated_at.to_f or model.destroyed?
      params, relations = model.get_sync_params( self )
      unless params.empty?
        if model.destroyed? then unwatch( model ) else watch_time_up model end
        relations.each { |relation| sync relation }
        send_json action: :_sync, params: params
      end
    end
  end
  self
end

#unwatch(model) ⇒ Object



45
46
47
# File 'lib/nali/connection.rb', line 45

def unwatch( model )
  watches.delete model.class.name + model.id.to_s
end

#warning(params) ⇒ Object



96
97
98
99
# File 'lib/nali/connection.rb', line 96

def warning( params )
  notice :warning, params
  self
end

#watch(model) ⇒ Object



41
42
43
# File 'lib/nali/connection.rb', line 41

def watch( model )
  watches[ model.class.name + model.id.to_s ] ||= 0
end

#watch?(model) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/nali/connection.rb', line 49

def watch?( model )
  if watches[ model.class.name + model.id.to_s ] then true else false end
end

#watch_time(model) ⇒ Object



53
54
55
# File 'lib/nali/connection.rb', line 53

def watch_time( model )
  watches[ model.class.name + model.id.to_s ] or 0
end

#watch_time_up(model) ⇒ Object



57
58
59
# File 'lib/nali/connection.rb', line 57

def watch_time_up( model )
  watches[ model.class.name + model.id.to_s ] = model.updated_at.to_f
end

#watchesObject



37
38
39
# File 'lib/nali/connection.rb', line 37

def watches
  @watches ||= {} 
end