module ArSync
Constants
- Config
- VERSION
Public Class Methods
config()
click to toggle source
# File lib/ar_sync/config.rb, line 11 def self.config @config ||= Config.new :current_user, nil, nil end
configure() { |config| ... }
click to toggle source
# File lib/ar_sync/config.rb, line 15 def self.configure yield config end
on_notification(&block)
click to toggle source
# File lib/ar_sync/core.rb, line 12 def self.on_notification(&block) @sync_send_block = block end
serialize(record_or_records, query, user: nil)
click to toggle source
# File lib/ar_sync/core.rb, line 84 def self.serialize(record_or_records, query, user: nil) ArSerializer.serialize record_or_records, query, context: user, include_id: true, use: :sync end
signed_key(key, time)
click to toggle source
# File lib/ar_sync/core.rb, line 80 def self.signed_key(key, time) "#{key};#{time};#{Digest::SHA256.hexdigest("#{config.key_secret}#{key};#{time}")[0, 16]}" end
skip_notification?()
click to toggle source
# File lib/ar_sync/core.rb, line 26 def self.skip_notification? Thread.current[:ar_sync_skip_notification] end
sync_key(model, to_user = nil, signature: true)
click to toggle source
# File lib/ar_sync/core.rb, line 60 def self.sync_key(model, to_user = nil, signature: true) if model.is_a? ArSync::Collection key = [to_user&.id, model.klass.name, model.name].join '/' else key = [to_user&.id, model.class.name, model.id].join '/' end key = Digest::SHA256.hexdigest("#{config.key_secret}#{key}")[0, 32] if config.key_secret key = "#{config.key_prefix}#{key}" if config.key_prefix key = signature && config.key_expires_in ? signed_key(key, Time.now.to_i.to_s) : key key + ';/' end
sync_keys(model, user)
click to toggle source
# File lib/ar_sync/core.rb, line 56 def self.sync_keys(model, user) [sync_key(model), sync_key(model, user)] end
sync_send(to:, action:, model:, path: nil, field: nil, to_user: nil)
click to toggle source
# File lib/ar_sync/core.rb, line 39 def self.sync_send(to:, action:, model:, path: nil, field: nil, to_user: nil) key = sync_key to, to_user, signature: false e = { action: action } e[:field] = field if field if model e[:class_name] = model.class.base_class.name e[:id] = model.id end event = ["#{key}#{path}", e] buffer = Thread.current[:ar_sync_compact_notifications] if buffer buffer << event else @sync_send_block&.call [event] end end
sync_serialize(target, user, query)
click to toggle source
# File lib/ar_sync/core.rb, line 88 def self.sync_serialize(target, user, query) case target when ArSync::Collection, ArSync::ModelBase serialized = ArSerializer.serialize target, query, context: user, include_id: true, use: :sync return serialized if target.is_a? ArSync::ModelBase { sync_keys: ArSync.sync_keys(target, user), order: { mode: target.order, limit: target.limit }, collection: serialized } when ActiveRecord::Relation, Array ArSync.serialize target.to_a, query, user: user when ArSerializer::Serializable ArSync.serialize target, query, user: user else target end end
validate_expiration(signed_key)
click to toggle source
# File lib/ar_sync/core.rb, line 72 def self.validate_expiration(signed_key) signed_key = signed_key.to_s return signed_key unless config.key_expires_in key, time, signature, other = signed_key.split ';', 4 return unless signed_key(key, time) == [key, time, signature].join(';') [key, other].compact.join ';' if Time.now.to_i - time.to_i < config.key_expires_in end
with_compact_notification() { || ... }
click to toggle source
# File lib/ar_sync/core.rb, line 16 def self.with_compact_notification key = :ar_sync_compact_notifications Thread.current[key] = [] yield ensure events = Thread.current[key] Thread.current[key] = nil @sync_send_block&.call events if events.present? end
without_notification() { || ... }
click to toggle source
# File lib/ar_sync/core.rb, line 30 def self.without_notification key = :ar_sync_skip_notification flag_was = Thread.current[key] Thread.current[key] = true yield ensure Thread.current[key] = flag_was end
Public Instance Methods
action_with_compact_ar_sync_notification(&block)
click to toggle source
# File lib/ar_sync/rails.rb, line 25 def action_with_compact_ar_sync_notification(&block) ArSync.with_compact_notification(&block) end