Class: ArSync::Collection

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

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

._sync_child_info(key) ⇒ Object



43
44
45
# File 'lib/ar_sync/collection.rb', line 43

def self._sync_child_info(key)
  _sync_children_info[key]
end

._sync_children_infoObject



39
40
41
# File 'lib/ar_sync/collection.rb', line 39

def self._sync_children_info
  @sync_children_info ||= {}
end

.defined_collectionsObject



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

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

.find(klass, name) ⇒ Object



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

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

Instance Method Details

#_sync_notify_child_added(child, _name, to_user) ⇒ Object



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

def _sync_notify_child_added(child, _name, to_user)
  ArSync.sync_send to: self, action: :add, model: child, path: :collection, to_user: to_user
end

#_sync_notify_child_changed(_name, _to_user) ⇒ Object



29
# File 'lib/ar_sync/collection.rb', line 29

def _sync_notify_child_changed(_name, _to_user); end

#_sync_notify_child_removed(child, _name, to_user, _owned) ⇒ Object



35
36
37
# File 'lib/ar_sync/collection.rb', line 35

def _sync_notify_child_removed(child, _name, to_user, _owned)
  ArSync.sync_send to: self, action: :remove, model: child, path: :collection, to_user: to_user
end

#to_aObject



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

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