Class: DexieCable::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/dexiecable/query.rb

Overview

Builds a chain of Dexie.js operations and transmits them as a single payload once a write operation is reached.

DexieCable::Query.new(channel, "messages")
.where(:room_id).equals(5)
.add(text: "hello")

Constant Summary collapse

WRITE_OPS =
%i[
  add bulkAdd put bulkPut bulkDelete
  upsert update delete modify clear
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(transmitter, name) ⇒ Query

Returns a new instance of Query.



19
20
21
22
# File 'lib/dexiecable/query.rb', line 19

def initialize(transmitter, name)
  @transmitter = transmitter
  @query = { table: name, ops: [] }
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *params) ⇒ Object



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

def method_missing(method, *params)
  @query[:ops] << { method: method, params: params }
  if WRITE_OPS.include?(method)
    @transmitter.transmit(query)
  else
    self
  end
end

Instance Attribute Details

#queryObject

Returns the value of attribute query.



17
18
19
# File 'lib/dexiecable/query.rb', line 17

def query
  @query
end

Instance Method Details

#respond_to_missing?(method) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/dexiecable/query.rb', line 33

def respond_to_missing?(method, *)
  true
end