Class: Deepstream::Record

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

Direct Known Subclasses

List

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, name) ⇒ Record

Returns a new instance of Record.



9
10
11
12
13
14
15
# File 'lib/deepstream/record.rb', line 9

def initialize(client, name)
  @client = client
  @name = name
  @data = {}
  @version = nil
  @client.send_message(TOPIC::RECORD, ACTION::CREATEORREAD, @name)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object

Raises:

  • (NoMethodError)


54
55
56
57
# File 'lib/deepstream/record.rb', line 54

def method_missing(name, *args)
  return @data.fetch(name.to_s, nil) if args.empty?
  raise(NoMethodError, name)
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



7
8
9
# File 'lib/deepstream/record.rb', line 7

def data
  @data
end

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/deepstream/record.rb', line 7

def name
  @name
end

#versionObject (readonly)

Returns the value of attribute version.



7
8
9
# File 'lib/deepstream/record.rb', line 7

def version
  @version
end

Instance Method Details

#deleteObject



25
26
27
# File 'lib/deepstream/record.rb', line 25

def delete
  @client.delete(@name)
end

#inspectObject



17
18
19
# File 'lib/deepstream/record.rb', line 17

def inspect
  "#{self.class} #{@name} #{@version} #{@data}"
end

#patch(version, path, value) ⇒ Object



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

def patch(version, path, value)
  @version = version.to_i
  set_path(@data, path, Helpers.to_type(value))
end

#read(version, data) ⇒ Object



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

def read(version, data)
  update(version, data)
end

#set(*args) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'lib/deepstream/record.rb', line 29

def set(*args)
  if args.one?
    @data = args.first
    @client.send_message(TOPIC::RECORD, ACTION::UPDATE, @name, (@version += 1), @data.to_json) if @version
  elsif args.size == 2
    path, value = args
    set_path(@data, path, value)
    @client.send_message(TOPIC::RECORD, ACTION::PATCH, @name, (@version += 1), path, Helpers.to_deepstream_type(value)) if @version
  end
end

#unsubscribeObject



21
22
23
# File 'lib/deepstream/record.rb', line 21

def unsubscribe
  @client.send_message(TOPIC::RECORD, ACTION::UNSUBSCRIBE, name)
end

#update(version, data) ⇒ Object



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

def update(version, data)
  @version = version.to_i
  @data = JSON.parse(data)
end