Class: HyperMesh::Connection
Defined Under Namespace
Classes: QueuedMessage
Constant Summary
ActiveRecord::ClassMethods::SERVER_METHODS
Class Attribute Summary collapse
#acting_user
#backing_record
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from AutoCreate
create_table, needs_init?
#<=>, _synchromesh_scope_args_check, #all, #all=, #all_changed?, #any_changed?, belongs_to, belongs_to_without_reactive_record_add_is_method, #check_permission_with_acting_user, #create, #create_permitted?, #default_scope, #destroy_permitted?, do_not_synchronize, do_not_synchronize?, #do_not_synchronize?, #finder_method, #method_missing, #none_changed?, #only_changed?, #pre_synchromesh_default_scope, #pre_synchromesh_method_missing, pre_synchromesh_scope, public_columns_hash, reflect_on_aggregation, reflect_on_all_aggregations, reflect_on_all_associations, reflect_on_association, #scope, #server_method, #synchromesh_after_change, #synchromesh_after_create, #synchromesh_after_destroy, #unscoped, #update, #update_attribute, #update_permitted?, #view_permitted?
#_react_param_conversion, #abstract_class=, #abstract_class?, #all, #all=, #base_class, #column_names, #columns_hash, #composed_of, #define_attribute_methods, #enum, #find, #find_by, #inheritance_column, #inheritance_column=, #method_missing, #model_name, #primary_key, #primary_key=, #scope, #server_method, #server_methods
#==, #attributes, #changed?, #destroy, #destroyed?, #dup, #errors, #id, #id=, #initialize, #itself, #load, #method_missing, #method_missing_warning, #model_name, #new?, #primary_key, #revert, #save, #saving?, #type, #type=
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
in the class ActiveRecord::Base
Class Attribute Details
.transport ⇒ Object
Returns the value of attribute transport.
104
105
106
|
# File 'lib/synchromesh/connection.rb', line 104
def transport
@transport
end
|
Class Method Details
106
107
108
109
110
111
112
|
# File 'lib/synchromesh/connection.rb', line 106
def active
if HyperMesh.on_server?
expired.delete_all
refresh_connections if needs_refresh?
end
all.pluck(:channel).uniq
end
|
.build_tables ⇒ Object
58
59
60
61
62
63
64
65
66
67
68
69
70
|
# File 'lib/synchromesh/connection.rb', line 58
def self.build_tables
create_table(force: true) do |t|
t.string :channel
t.string :session
t.datetime :created_at
t.datetime :expires_at
t.datetime :refresh_at
end
QueuedMessage.create_table(force: true) do |t|
t.text :data
t.integer :connection_id
end
end
|
.connect_to_transport(channel, session, root_path) ⇒ Object
133
134
135
136
137
138
139
140
141
142
143
|
# File 'lib/synchromesh/connection.rb', line 133
def connect_to_transport(channel, session, root_path)
self.root_path = root_path
if (connection = find_by(channel: channel, session: session))
messages = connection.messages.pluck(:data)
connection.destroy
else
messages = []
end
open(channel)
messages
end
|
.disconnect(channel) ⇒ Object
145
146
147
|
# File 'lib/synchromesh/connection.rb', line 145
def disconnect(channel)
find_by(channel: channel, session: nil).destroy
end
|
.needs_refresh? ⇒ Boolean
87
88
89
|
# File 'lib/synchromesh/connection.rb', line 87
def self.needs_refresh?
exists?(['refresh_at IS NOT NULL AND refresh_at < ?', Time.zone.now])
end
|
.open(channel, session = nil, root_path = nil) ⇒ Object
114
115
116
117
|
# File 'lib/synchromesh/connection.rb', line 114
def open(channel, session = nil, root_path = nil)
self.root_path = root_path
find_or_create_by(channel: channel, session: session)
end
|
.read(session, root_path) ⇒ Object
126
127
128
129
130
131
|
# File 'lib/synchromesh/connection.rb', line 126
def read(session, root_path)
self.root_path = root_path
where(session: session)
.update_all(expires_at: Time.now + transport.expire_polled_connection_in)
QueuedMessage.for_session(session).destroy_all.pluck(:data)
end
|
.refresh_connections ⇒ Object
159
160
161
162
163
164
165
166
167
|
# File 'lib/synchromesh/connection.rb', line 159
def refresh_connections
refresh_started_at = Time.zone.now
channels = transport.refresh_channels
next_refresh = refresh_started_at + transport.refresh_channels_every
channels.each do |channel|
find_by(channel: channel, session: nil).update(refresh_at: next_refresh)
end
inactive.delete_all
end
|
.root_path ⇒ Object
153
154
155
156
157
|
# File 'lib/synchromesh/connection.rb', line 153
def root_path
QueuedMessage.root_path
rescue
nil
end
|
.root_path=(path) ⇒ Object
149
150
151
|
# File 'lib/synchromesh/connection.rb', line 149
def root_path=(path)
QueuedMessage.root_path = path if path
end
|
.send_to_channel(channel, data) ⇒ Object
119
120
121
122
123
124
|
# File 'lib/synchromesh/connection.rb', line 119
def send_to_channel(channel, data)
pending_for(channel).each do |connection|
QueuedMessage.create(data: data, synchromesh_connection: connection)
end
transport.send(channel, data) if exists?(channel: channel, session: nil)
end
|
Instance Method Details
#transport ⇒ Object
91
92
93
|
# File 'lib/synchromesh/connection.rb', line 91
def transport
self.class.transport
end
|