Class: Crimson::Client
- Inherits:
-
Object
- Object
- Crimson::Client
- Defined in:
- lib/crimson/client.rb
Instance Attribute Summary collapse
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#notification_bus ⇒ Object
readonly
Returns the value of attribute notification_bus.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #eql?(other) ⇒ Boolean
- #hash ⇒ Object
-
#initialize(id, connection) ⇒ Client
constructor
A new instance of Client.
- #observe(object) ⇒ Object
- #observing?(object) ⇒ Boolean
- #on_commit(object, changes) ⇒ Object
- #on_message(message) ⇒ Object
- #unobserve(object) ⇒ Object
- #write(message = {}) ⇒ Object
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
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
8 9 10 |
# File 'lib/crimson/client.rb', line 8 def connection @connection end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
8 9 10 |
# File 'lib/crimson/client.rb', line 8 def id @id end |
#notification_bus ⇒ Object (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
71 72 73 |
# File 'lib/crimson/client.rb', line 71 def ==(other) other.is_a?(Client) && other.id == id end |
#eql?(other) ⇒ Boolean
75 76 77 |
# File 'lib/crimson/client.rb', line 75 def eql?(other) self == other end |
#hash ⇒ Object
79 80 81 |
# File 'lib/crimson/client.rb', line 79 def hash id.hash end |
#observe(object) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/crimson/client.rb', line 41 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
67 68 69 |
# File 'lib/crimson/client.rb', line 67 def observing?(object) object.observers.key?(self) end |
#on_commit(object, changes) ⇒ Object
33 34 35 36 37 38 39 |
# File 'lib/crimson/client.rb', line 33 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 () = Hashie::Mash.new() begin case .action when "event" notification_bus.notify() end rescue ArgumentError => e puts e end end |
#unobserve(object) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/crimson/client.rb', line 55 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 |
# File 'lib/crimson/client.rb', line 29 def write( = {}) connection.write() end |