Class: Hyperloop::InternalPolicy
- Defined in:
- lib/hyper-operation/transport/policy.rb
Constant Summary collapse
- EXPOSED_METHODS =
[:send_all, :send_all_but, :send_only, :obj]
Class Method Summary collapse
- .channel_to_string(channel) ⇒ Object
- .regulate_broadcast(model, &block) ⇒ Object
- .regulate_connection(acting_user, channel_string) ⇒ Object
Instance Method Summary collapse
- #add_unassigned_send_set(send_set) ⇒ Object
- #broadcast(&block) ⇒ Object
- #channel_available?(channel) ⇒ Boolean
- #channel_list ⇒ Object
- #channel_to_string(channel) ⇒ Object
- #filter(h, attribute_set) ⇒ Object
- #has_unassigned_sets? ⇒ Boolean
- #id ⇒ Object
-
#initialize(obj, attribute_names, available_channels) ⇒ InternalPolicy
constructor
A new instance of InternalPolicy.
- #merge_set(send_set, channel) ⇒ Object
- #obj ⇒ Object
- #send_all ⇒ Object
- #send_all_but(*execeptions) ⇒ Object
- #send_message(header, channel, attribute_set) {|message| ... } ⇒ Object
- #send_only(*white_list) ⇒ Object
- #send_set_to(send_set, channels) ⇒ Object
- #send_unassigned_sets_to(channel) ⇒ Object
Constructor Details
#initialize(obj, attribute_names, available_channels) ⇒ InternalPolicy
Returns a new instance of InternalPolicy.
347 348 349 350 351 352 353 |
# File 'lib/hyper-operation/transport/policy.rb', line 347 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
.channel_to_string(channel) ⇒ Object
397 398 399 400 401 402 403 404 405 |
# File 'lib/hyper-operation/transport/policy.rb', line 397 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
338 339 340 341 342 343 344 345 |
# File 'lib/hyper-operation/transport/policy.rb', line 338 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
327 328 329 330 331 332 333 334 335 336 |
# File 'lib/hyper-operation/transport/policy.rb', line 327 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
#add_unassigned_send_set(send_set) ⇒ Object
376 377 378 |
# File 'lib/hyper-operation/transport/policy.rb', line 376 def add_unassigned_send_set(send_set) @unassigned_send_sets << send_set end |
#broadcast(&block) ⇒ Object
433 434 435 436 437 438 439 |
# File 'lib/hyper-operation/transport/policy.rb', line 433 def broadcast(&block) klass = @obj.class header = {broadcast_id: id, channels: channel_list, klass: klass.name} @channel_sets.each do |channel, attribute_set| header, channel, attribute_set, &block end end |
#channel_available?(channel) ⇒ Boolean
355 356 357 |
# File 'lib/hyper-operation/transport/policy.rb', line 355 def channel_available?(channel) channel && @available_channels.include?(channel_to_string(channel)) end |
#channel_list ⇒ Object
393 394 395 |
# File 'lib/hyper-operation/transport/policy.rb', line 393 def channel_list @channel_sets.collect { |channel, _value| channel_to_string channel } end |
#channel_to_string(channel) ⇒ Object
407 408 409 |
# File 'lib/hyper-operation/transport/policy.rb', line 407 def channel_to_string(channel) self.class.channel_to_string(channel) end |
#filter(h, attribute_set) ⇒ Object
411 412 413 414 415 416 417 418 419 |
# File 'lib/hyper-operation/transport/policy.rb', line 411 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
363 364 365 |
# File 'lib/hyper-operation/transport/policy.rb', line 363 def has_unassigned_sets? !@unassigned_send_sets.empty? end |
#id ⇒ Object
359 360 361 |
# File 'lib/hyper-operation/transport/policy.rb', line 359 def id @id ||= "#{self.object_id}-#{Time.now.to_f}" end |
#merge_set(send_set, channel) ⇒ Object
387 388 389 390 391 |
# File 'lib/hyper-operation/transport/policy.rb', line 387 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 |
#obj ⇒ Object
323 324 325 |
# File 'lib/hyper-operation/transport/policy.rb', line 323 def obj @obj end |
#send_all ⇒ Object
311 312 313 |
# File 'lib/hyper-operation/transport/policy.rb', line 311 def send_all SendSet.new(self) end |
#send_all_but(*execeptions) ⇒ Object
315 316 317 |
# File 'lib/hyper-operation/transport/policy.rb', line 315 def send_all_but(*execeptions) SendSet.new(self, exclude: execeptions) end |
#send_message(header, channel, attribute_set) {|message| ... } ⇒ Object
421 422 423 424 425 426 427 428 429 430 431 |
# File 'lib/hyper-operation/transport/policy.rb', line 421 def (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? = header.merge( channel: channel_to_string(channel), record: record, previous_changes: previous_changes ) yield end |
#send_only(*white_list) ⇒ Object
319 320 321 |
# File 'lib/hyper-operation/transport/policy.rb', line 319 def send_only(*white_list) SendSet.new(self, white_list: white_list) end |
#send_set_to(send_set, channels) ⇒ Object
380 381 382 383 384 385 |
# File 'lib/hyper-operation/transport/policy.rb', line 380 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
367 368 369 370 371 372 373 374 |
# File 'lib/hyper-operation/transport/policy.rb', line 367 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 |