Class: Deepstream::List

Inherits:
Record
  • Object
show all
Defined in:
lib/deepstream/list.rb

Constant Summary collapse

LIST_CALLBACKS =
%i{added removed}

Instance Attribute Summary

Attributes inherited from Record

#data

Instance Method Summary collapse

Methods inherited from Record

#__name, #__version, #delete, #inspect, #is_reinitializing?, #method_missing, #patch, #reset_version, #start_reinitializing, #unsubscribe, #when_ready

Constructor Details

#initialize(*args) ⇒ List

Returns a new instance of List.



8
9
10
11
12
# File 'lib/deepstream/list.rb', line 8

def initialize(*args)
  super
  @data = []
  @handlers = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Deepstream::Record

Instance Method Details

#add(record_name) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'lib/deepstream/list.rb', line 14

def add(record_name)
  unless @data.include?(record_name)
    @data << record_name
    set
    notify_listeners(:removed, record_name)
  end
rescue => e
  @client.on_exception(e)
end

#allObject



42
43
44
# File 'lib/deepstream/list.rb', line 42

def all
  @data.map { |record_name| @client.get(record_name) }
end

#end_reinitializingObject



46
47
48
49
50
# File 'lib/deepstream/list.rb', line 46

def end_reinitializing
  reset_version
  set
  @is_reinitializing = false
end

#keysObject



38
39
40
# File 'lib/deepstream/list.rb', line 38

def keys
  @data
end

#off(cb_name, &block) ⇒ Object



67
68
69
70
71
72
73
74
75
76
# File 'lib/deepstream/list.rb', line 67

def off(cb_name, &block)
  if block_given?
    @handlers[cb_name].delete block
  elsif cb_name
    @handlers[cb_name] = []
  else
    @handlers = {}
  end
  nil
end

#on(cb_name, &block) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/deepstream/list.rb', line 59

def on(cb_name, &block)
  unless LIST_CALLBACKS.include?(cb_name)
    raise(UnknownListCallback, "Uknown callback name: #{cb_name}. Must be one of: #{LIST_CALLBACKS}")
  end
  (@handlers[cb_name] ||= []).push(block)
  nil
end

#read(version, data) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/deepstream/list.rb', line 24

def read(version, data)
  @version = version.to_i
  data = JSON.parse(data)
  if data.is_a?(Array)
    set_new_data (@data + data).uniq
    set if @data.size > data.size
  end
end

#remove(record_name) ⇒ Object



33
34
35
36
# File 'lib/deepstream/list.rb', line 33

def remove(record_name)
  set if @data.delete(record_name)
  notify_listeners(:removed, record_name)
end

#update(version, data) ⇒ Object



52
53
54
55
56
57
# File 'lib/deepstream/list.rb', line 52

def update(version, data)
  @version = version.to_i
  set_new_data JSON.parse(data)
rescue => e
  @client.on_exception(e)
end