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 collapse

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.



10
11
12
13
14
# File 'lib/deepstream/list.rb', line 10

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 Attribute Details

#versionObject (readonly)

Returns the value of attribute version.



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

def version
  @version
end

Instance Method Details

#add(record_name) ⇒ Object



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

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

#allObject



44
45
46
# File 'lib/deepstream/list.rb', line 44

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

#end_reinitializingObject



48
49
50
51
52
# File 'lib/deepstream/list.rb', line 48

def end_reinitializing
  reset_version
  set
  @is_reinitializing = false
end

#keysObject



40
41
42
# File 'lib/deepstream/list.rb', line 40

def keys
  @data
end

#off(cb_name, &block) ⇒ Object



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

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



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

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



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

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



35
36
37
38
# File 'lib/deepstream/list.rb', line 35

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

#update(version, data) ⇒ Object



54
55
56
57
58
59
# File 'lib/deepstream/list.rb', line 54

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