Class: Crimson::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/crimson/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, connection) ⇒ Client

Returns a new instance of Client.



10
11
12
13
14
# File 'lib/crimson/client.rb', line 10

def initialize(id, connection)
  @id = id
  @connection = connection
  @notification_bus = NotificationBus.new
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



8
9
10
# File 'lib/crimson/client.rb', line 8

def connection
  @connection
end

#idObject (readonly)

Returns the value of attribute id.



8
9
10
# File 'lib/crimson/client.rb', line 8

def id
  @id
end

#notification_busObject (readonly)

Returns the value of attribute notification_bus.



8
9
10
# File 'lib/crimson/client.rb', line 8

def notification_bus
  @notification_bus
end

Instance Method Details

#==(other) ⇒ Object



72
73
74
# File 'lib/crimson/client.rb', line 72

def ==(other)
  other.is_a?(Client) && other.id == id
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/crimson/client.rb', line 76

def eql?(other)
  self == other
end

#hashObject



80
81
82
# File 'lib/crimson/client.rb', line 80

def hash
  id.hash
end

#observe(object) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/crimson/client.rb', line 42

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) ⇒ Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/crimson/client.rb', line 68

def observing?(object)
  object.observers.key?(self)
end

#on_commit(object, changes) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/crimson/client.rb', line 34

def on_commit(object, changes)
  write(
    action: :update,
    id: object.id,
    changes: changes
  )
end

#on_message(message) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
# 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) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/crimson/client.rb', line 56

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 = {}) ⇒ Object



29
30
31
32
# File 'lib/crimson/client.rb', line 29

def write(message = {})
  connection.write(message)
  connection.flush
end