Class: ArSync::Collection

Inherits:
Object
  • Object
show all
Defined in:
lib/ar_sync/collection.rb

Direct Known Subclasses

Graph, Tree

Defined Under Namespace

Classes: Graph, Tree

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, name, limit: nil, order: nil) ⇒ Collection

Returns a new instance of Collection.



5
6
7
8
9
10
11
12
# File 'lib/ar_sync/collection.rb', line 5

def initialize(klass, name, limit: nil, order: nil)
  @klass = klass
  @name = name
  @limit = limit
  @order = order
  self.class.defined_collections[[klass, name]] = self
  define_singleton_method(name) { to_a }
end

Instance Attribute Details

#klassObject (readonly)

Returns the value of attribute klass.



4
5
6
# File 'lib/ar_sync/collection.rb', line 4

def klass
  @klass
end

#limitObject (readonly)

Returns the value of attribute limit.



4
5
6
# File 'lib/ar_sync/collection.rb', line 4

def limit
  @limit
end

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/ar_sync/collection.rb', line 4

def name
  @name
end

#orderObject (readonly)

Returns the value of attribute order.



4
5
6
# File 'lib/ar_sync/collection.rb', line 4

def order
  @order
end

Class Method Details

.defined_collectionsObject



26
27
28
# File 'lib/ar_sync/collection.rb', line 26

def self.defined_collections
  @defined_collections ||= {}
end

.find(klass, name) ⇒ Object



30
31
32
# File 'lib/ar_sync/collection.rb', line 30

def self.find(klass, name)
  defined_collections[[klass, name]]
end

Instance Method Details

#sync_send_event(type:, to_user: nil, data:) ⇒ Object



14
15
16
17
# File 'lib/ar_sync/collection.rb', line 14

def sync_send_event(type:, to_user: nil, data:)
  event_data = { type: type, data: data }
  ArSync.sync_tree_send to: self, action: :event, path: [], data: event_data, to_user: to_user
end

#to_aObject



19
20
21
22
23
24
# File 'lib/ar_sync/collection.rb', line 19

def to_a
  all = klass.all
  all = all.order id: order if order
  all = all.limit limit if limit
  all
end