class Crimson::Client

Attributes

connection[R]
id[R]
notification_bus[R]

Public Class Methods

new(id, connection) click to toggle source
# File lib/crimson/client.rb, line 10
def initialize(id, connection)
  @id = id
  @connection = connection
  @notification_bus = NotificationBus.new
end

Public Instance Methods

==(other) click to toggle source
# File lib/crimson/client.rb, line 74
def ==(other)
  other.is_a?(Client) && other.id == id
end
eql?(other) click to toggle source
# File lib/crimson/client.rb, line 78
def eql?(other)
  self == other
end
hash() click to toggle source
# File lib/crimson/client.rb, line 82
def hash
  id.hash
end
observe(object) click to toggle source
# File lib/crimson/client.rb, line 44
def observe(object)
  object.postordered_each do |observable|
    write(
      action: :create,
      id: observable.id,
      tag: observable.tag,
      changes: observable.master
    )

    observable.add_observer(self)
    notification_bus.register(observable)
  end
end
observing?(object) click to toggle source
# File lib/crimson/client.rb, line 70
def observing?(object)
  object.observers.key?(self)
end
on_commit(object, changes) click to toggle source
# File lib/crimson/client.rb, line 36
def on_commit(object, changes)
  write(
    action: :update,
    id: object.id,
    changes: changes
  )
end
on_message(message) click to toggle source
# File lib/crimson/client.rb, line 16
def on_message(message)
  message = Hashie::Mash.new(message)

  begin
    case message.action
    when "event"
      notification_bus.notify(message)
    end
  rescue ArgumentError => e
    puts e
  end
end
unobserve(object) click to toggle source
# File lib/crimson/client.rb, line 58
def unobserve(object)
  object.postordered_each do |observable|
    observable.remove_observer(self)
    notification_bus.unregister(observable)

    write(
      action: :destroy,
      id: observable.id
    )
  end
end
write(message = {}) click to toggle source
# File lib/crimson/client.rb, line 29
def write(message = {})
  unless connection.closed?
    connection.write(message)
    connection.flush
  end
end