Class: Apiway::Client
- Inherits:
-
Object
- Object
- Apiway::Client
- Defined in:
- lib/apiway/client.rb,
lib/generator/application/app/base/client.rb
Constant Summary collapse
- HANDLERS =
Hash[ API::ALIVE, :alive, API::QUERY, :run_controller, RESOURCE::SYNC, :sync_resource, RESOURCE::DESTROY, :destroy_resource ]
Class Method Summary collapse
- .all ⇒ Object
- .on_connected(&block) ⇒ Object
- .on_disconnected(&block) ⇒ Object
- .on_message(&block) ⇒ Object
- .sync_changes(changed_models) ⇒ Object
Instance Method Summary collapse
- #[](name = nil) ⇒ Object
- #[]=(name, value) ⇒ Object
-
#initialize(ws) ⇒ Client
constructor
A new instance of Client.
- #sync_changes(changed_models) ⇒ Object
- #trigger(*args) ⇒ Object
Constructor Details
#initialize(ws) ⇒ Client
Returns a new instance of Client.
41 42 43 44 45 46 47 48 |
# File 'lib/apiway/client.rb', line 41 def initialize( ws ) @ws = ws @ws.onopen { on_connected } @ws. { |msg| msg } @ws.onclose { on_disconnected } @storage = {} @resources = {} end |
Class Method Details
.all ⇒ Object
16 17 18 19 20 |
# File 'lib/apiway/client.rb', line 16 def all @@all ||= [] @@all.each { |client| yield client } if block_given? @@all end |
.on_connected(&block) ⇒ Object
26 27 28 |
# File 'lib/apiway/client.rb', line 26 def on_connected( &block ) block_given? ? @@on_connected = block : @@on_connected ||= Proc.new {} end |
.on_disconnected(&block) ⇒ Object
34 35 36 |
# File 'lib/apiway/client.rb', line 34 def on_disconnected( &block ) block_given? ? @@on_disconnected = block : @@on_disconnected ||= Proc.new {} end |
.on_message(&block) ⇒ Object
30 31 32 |
# File 'lib/apiway/client.rb', line 30 def ( &block ) block_given? ? @@on_message = block : @@on_message ||= Proc.new {} end |
.sync_changes(changed_models) ⇒ Object
22 23 24 |
# File 'lib/apiway/client.rb', line 22 def sync_changes( changed_models ) all { |client| client.sync_changes changed_models } end |
Instance Method Details
#[](name = nil) ⇒ Object
50 51 52 |
# File 'lib/apiway/client.rb', line 50 def []( name = nil ) @storage[ name ] end |
#[]=(name, value) ⇒ Object
54 55 56 57 58 |
# File 'lib/apiway/client.rb', line 54 def []=( name, value ) @storage[ name ] = value Thread.current[ :changed_models ].concat Apiway::Model.all value end |
#sync_changes(changed_models) ⇒ Object
60 61 62 |
# File 'lib/apiway/client.rb', line 60 def sync_changes( changed_models ) @resources.values.each { |resource| resource.sync_changes changed_models } end |