Class: Deepstream::Record
- Inherits:
-
Object
- Object
- Deepstream::Record
- Defined in:
- lib/deepstream/record.rb
Direct Known Subclasses
Instance Method Summary collapse
- #delete ⇒ Object
- #get_name ⇒ Object
-
#initialize(client, name) ⇒ Record
constructor
A new instance of Record.
- #inspect ⇒ Object
- #method_missing(name, *args) ⇒ Object
- #patch(version, path, value) ⇒ Object
- #read(version, data) ⇒ Object
- #set(*args) ⇒ Object
- #unsubscribe ⇒ Object
- #update(version, data) ⇒ Object
Constructor Details
#initialize(client, name) ⇒ Record
7 8 9 10 11 12 13 |
# File 'lib/deepstream/record.rb', line 7 def initialize(client, name) @client = client @name = name @data = {} @version = nil @client.(TOPIC::RECORD, ACTION::CREATEORREAD, @name) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args) ⇒ Object
63 64 65 66 67 68 |
# File 'lib/deepstream/record.rb', line 63 def method_missing(name, *args) name = name.to_s return @data.fetch(name, nil) if args.empty? return set(name[0..-2], *args) if name.end_with?('=') && !args.empty? raise(NoMethodError, name) end |
Instance Method Details
#delete ⇒ Object
27 28 29 |
# File 'lib/deepstream/record.rb', line 27 def delete @client.delete(@name) end |
#get_name ⇒ Object
15 16 17 |
# File 'lib/deepstream/record.rb', line 15 def get_name @name end |
#inspect ⇒ Object
19 20 21 |
# File 'lib/deepstream/record.rb', line 19 def inspect "#{self.class} #{@name} #{@version} #{@data}" end |
#patch(version, path, value) ⇒ Object
49 50 51 52 53 54 |
# File 'lib/deepstream/record.rb', line 49 def patch(version, path, value) @version = version.to_i set_path(@data, path, Helpers.to_type(value)) rescue => e @client.on_exception(e) end |
#read(version, data) ⇒ Object
45 46 47 |
# File 'lib/deepstream/record.rb', line 45 def read(version, data) update(version, data) end |
#set(*args) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/deepstream/record.rb', line 31 def set(*args) if args.size == 1 raise(ArgumentError, "Record data must be a hash") unless args.first.is_a?(Hash) @data = args.first @client.(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.(TOPIC::RECORD, ACTION::PATCH, @name, (@version += 1), path, Helpers.to_deepstream_type(value)) if @version end rescue => e @client.on_exception(e) end |
#unsubscribe ⇒ Object
23 24 25 |
# File 'lib/deepstream/record.rb', line 23 def unsubscribe @client.(TOPIC::RECORD, ACTION::UNSUBSCRIBE, name) end |
#update(version, data) ⇒ Object
56 57 58 59 60 61 |
# File 'lib/deepstream/record.rb', line 56 def update(version, data) @version = version.to_i @data = JSON.parse(data) rescue => e @client.on_exception(e) end |