Class: HyperMesh::InternalPolicy

Inherits:
Object
  • Object
show all
Defined in:
lib/synchromesh/policy.rb,
lib/reactive_record/permissions.rb

Constant Summary collapse

EXPOSED_METHODS =
[:send_all, :send_all_but, :send_only, :obj]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(obj, attribute_names, available_channels) ⇒ InternalPolicy

Returns a new instance of InternalPolicy.



316
317
318
319
320
321
322
# File 'lib/synchromesh/policy.rb', line 316

def initialize(obj, attribute_names, available_channels)
  @obj = obj
  attribute_names = attribute_names.map(&:to_sym).to_set
  @unassigned_send_sets = []
  @channel_sets = Hash.new { |hash, key| hash[key] = attribute_names }
  @available_channels = available_channels
end

Class Method Details

.accessible_attributes_for(model, acting_user) ⇒ Object



12
13
14
15
16
17
18
19
# File 'lib/reactive_record/permissions.rb', line 12

def self.accessible_attributes_for(model, acting_user)
  user_channels = ClassConnectionRegulation.connections_for(acting_user, false) +
    InstanceConnectionRegulation.connections_for(acting_user, false)
  internal_policy = InternalPolicy.new(model, model.attribute_names, user_channels)
  ChannelBroadcastRegulation.broadcast(internal_policy)
  InstanceBroadcastRegulation.broadcast(model, internal_policy)
  internal_policy.accessible_attributes_for
end

.channel_to_string(channel) ⇒ Object



366
367
368
369
370
371
372
373
374
# File 'lib/synchromesh/policy.rb', line 366

def self.channel_to_string(channel)
  if channel.is_a?(Class) && channel.name
    channel.name
  elsif channel.is_a? String
    channel
  else
    "#{channel.class.name}-#{channel.id}"
  end
end

.regulate_broadcast(model, &block) ⇒ Object



307
308
309
310
311
312
313
314
# File 'lib/synchromesh/policy.rb', line 307

def self.regulate_broadcast(model, &block)
  internal_policy = InternalPolicy.new(
    model, model.attribute_names, Connection.active
  )
  ChannelBroadcastRegulation.broadcast(internal_policy)
  InstanceBroadcastRegulation.broadcast(model, internal_policy)
  internal_policy.broadcast &block
end

.regulate_connection(acting_user, channel_string) ⇒ Object



296
297
298
299
300
301
302
303
304
305
# File 'lib/synchromesh/policy.rb', line 296

def self.regulate_connection(acting_user, channel_string)
  channel = channel_string.split("-")
  if channel.length > 1
    id = channel[1..-1].join("-")
    object = Object.const_get(channel[0]).find(id)
    InstanceConnectionRegulation.connect(object, acting_user)
  else
    ClassConnectionRegulation.connect(channel[0], acting_user)
  end
end

Instance Method Details

#accessible_attributes_forObject



21
22
23
24
25
26
27
28
# File 'lib/reactive_record/permissions.rb', line 21

def accessible_attributes_for
  accessible_attributes = Set.new
  @channel_sets.each do |channel, attribute_set|
    accessible_attributes.merge attribute_set
  end
  accessible_attributes << :id unless accessible_attributes.empty?
  accessible_attributes
end

#add_unassigned_send_set(send_set) ⇒ Object



345
346
347
# File 'lib/synchromesh/policy.rb', line 345

def add_unassigned_send_set(send_set)
  @unassigned_send_sets << send_set
end

#broadcast(&block) ⇒ Object



402
403
404
405
406
407
408
# File 'lib/synchromesh/policy.rb', line 402

def broadcast(&block)
  klass = @obj.class
  header = {broadcast_id: id, channels: channel_list, klass: klass.name}
  @channel_sets.each do |channel, attribute_set|
    send_message header, channel, attribute_set, &block
  end
end

#channel_available?(channel) ⇒ Boolean

Returns:

  • (Boolean)


324
325
326
# File 'lib/synchromesh/policy.rb', line 324

def channel_available?(channel)
  channel && @available_channels.include?(channel_to_string(channel))
end

#channel_listObject



362
363
364
# File 'lib/synchromesh/policy.rb', line 362

def channel_list
  @channel_sets.collect { |channel, _value| channel_to_string channel }
end

#channel_to_string(channel) ⇒ Object



376
377
378
# File 'lib/synchromesh/policy.rb', line 376

def channel_to_string(channel)
  self.class.channel_to_string(channel)
end

#filter(h, attribute_set) ⇒ Object



380
381
382
383
384
385
386
387
388
# File 'lib/synchromesh/policy.rb', line 380

def filter(h, attribute_set)
  r = {}
  h.each do |key, value|
    r[key.to_sym] = value if attribute_set.member?(key.to_sym) || (key.to_sym == :id)
  end unless attribute_set.empty?
  #model_id = h[:id] || h["id"]
  #r[:id] = model_id if model_id && !attribute_set.empty?
  r
end

#has_unassigned_sets?Boolean

Returns:

  • (Boolean)


332
333
334
# File 'lib/synchromesh/policy.rb', line 332

def has_unassigned_sets?
  !@unassigned_send_sets.empty?
end

#idObject



328
329
330
# File 'lib/synchromesh/policy.rb', line 328

def id
  @id ||= "#{self.object_id}-#{Time.now.to_f}"
end

#merge_set(send_set, channel) ⇒ Object



356
357
358
359
360
# File 'lib/synchromesh/policy.rb', line 356

def merge_set(send_set, channel)
  return unless channel
  channel = channel.name if channel.is_a?(Class) && channel.name
  @channel_sets[channel] = send_set.merge(@channel_sets[channel])
end

#objObject



292
293
294
# File 'lib/synchromesh/policy.rb', line 292

def obj
  @obj
end

#send_allObject



280
281
282
# File 'lib/synchromesh/policy.rb', line 280

def send_all
  SendSet.new(self)
end

#send_all_but(*execeptions) ⇒ Object



284
285
286
# File 'lib/synchromesh/policy.rb', line 284

def send_all_but(*execeptions)
  SendSet.new(self, exclude: execeptions)
end

#send_message(header, channel, attribute_set) {|message| ... } ⇒ Object

Yields:

  • (message)


390
391
392
393
394
395
396
397
398
399
400
# File 'lib/synchromesh/policy.rb', line 390

def send_message(header, channel, attribute_set, &block)
  record = filter(@obj.react_serializer, attribute_set)
  previous_changes = filter(@obj.previous_changes, attribute_set)
  return if record.empty? && previous_changes.empty?
  message = header.merge(
    channel: channel_to_string(channel),
    record: record,
    previous_changes: previous_changes
  )
  yield message
end

#send_only(*white_list) ⇒ Object



288
289
290
# File 'lib/synchromesh/policy.rb', line 288

def send_only(*white_list)
  SendSet.new(self, white_list: white_list)
end

#send_set_to(send_set, channels) ⇒ Object



349
350
351
352
353
354
# File 'lib/synchromesh/policy.rb', line 349

def send_set_to(send_set, channels)
  channels.flatten(1).each do |channel|
    merge_set(send_set, channel) if channel_available? channel
    @unassigned_send_sets.delete(send_set)
  end
end

#send_unassigned_sets_to(channel) ⇒ Object



336
337
338
339
340
341
342
343
# File 'lib/synchromesh/policy.rb', line 336

def send_unassigned_sets_to(channel)
  if channel_available?(channel) && has_unassigned_sets?
    @channel_sets[channel] = @unassigned_send_sets.inject(@channel_sets[channel]) do |set, send_set|
      send_set.merge(set)
    end
  end
  @unassigned_send_sets = []
end