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, first: nil, last: nil, direction: nil) ⇒ Collection

Returns a new instance of Collection.



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

def initialize(klass, name, first: nil, last: nil, direction: nil)
  direction ||= :asc
  @klass = klass
  @name = name
  @first = first
  @last = last
  @direction = direction
  @ordering = { first: first, last: last, direction: direction }.compact
  self.class.defined_collections[[klass, name]] = self
  define_singleton_method(name) { to_a }
end

Instance Attribute Details

#directionObject (readonly)

Returns the value of attribute direction.



2
3
4
# File 'lib/ar_sync/collection.rb', line 2

def direction
  @direction
end

#firstObject (readonly)

Returns the value of attribute first.



2
3
4
# File 'lib/ar_sync/collection.rb', line 2

def first
  @first
end

#klassObject (readonly)

Returns the value of attribute klass.



2
3
4
# File 'lib/ar_sync/collection.rb', line 2

def klass
  @klass
end

#lastObject (readonly)

Returns the value of attribute last.



2
3
4
# File 'lib/ar_sync/collection.rb', line 2

def last
  @last
end

#nameObject (readonly)

Returns the value of attribute name.



2
3
4
# File 'lib/ar_sync/collection.rb', line 2

def name
  @name
end

#orderingObject (readonly)

Returns the value of attribute ordering.



2
3
4
# File 'lib/ar_sync/collection.rb', line 2

def ordering
  @ordering
end

Class Method Details

._sync_child_info(key) ⇒ Object



48
49
50
# File 'lib/ar_sync/collection.rb', line 48

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

._sync_children_infoObject



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

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

.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_notify_child_added(child, _name, to_user) ⇒ Object



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

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



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

def _sync_notify_child_changed(_name, _to_user); end

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



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

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



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

def to_a
  if first
    klass.order(id: direction).limit(first).to_a
  elsif last
    rev = direction == :asc ? :desc : :asc
    klass.order(id: rev).limit(last).reverse
  else
    klass.all.to_a
  end
end