Class: ArSync::Collection
- Inherits:
-
Object
- Object
- ArSync::Collection
- Defined in:
- lib/ar_sync/collection.rb
Instance Attribute Summary collapse
-
#direction ⇒ Object
readonly
Returns the value of attribute direction.
-
#first ⇒ Object
readonly
Returns the value of attribute first.
-
#klass ⇒ Object
readonly
Returns the value of attribute klass.
-
#last ⇒ Object
readonly
Returns the value of attribute last.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#ordering ⇒ Object
readonly
Returns the value of attribute ordering.
Class Method Summary collapse
- ._sync_child_info(key) ⇒ Object
- ._sync_children_info ⇒ Object
- .defined_collections ⇒ Object
- .find(klass, name) ⇒ Object
Instance Method Summary collapse
- #_sync_notify_child_added(child, _name, to_user) ⇒ Object
- #_sync_notify_child_changed(_name, _to_user) ⇒ Object
- #_sync_notify_child_removed(child, _name, to_user, _owned) ⇒ Object
-
#initialize(klass, name, first: nil, last: nil, direction: nil) ⇒ Collection
constructor
A new instance of Collection.
- #to_a ⇒ Object
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
#direction ⇒ Object (readonly)
Returns the value of attribute direction.
2 3 4 |
# File 'lib/ar_sync/collection.rb', line 2 def direction @direction end |
#first ⇒ Object (readonly)
Returns the value of attribute first.
2 3 4 |
# File 'lib/ar_sync/collection.rb', line 2 def first @first end |
#klass ⇒ Object (readonly)
Returns the value of attribute klass.
2 3 4 |
# File 'lib/ar_sync/collection.rb', line 2 def klass @klass end |
#last ⇒ Object (readonly)
Returns the value of attribute last.
2 3 4 |
# File 'lib/ar_sync/collection.rb', line 2 def last @last end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
2 3 4 |
# File 'lib/ar_sync/collection.rb', line 2 def name @name end |
#ordering ⇒ Object (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_info ⇒ Object
44 45 46 |
# File 'lib/ar_sync/collection.rb', line 44 def self._sync_children_info @sync_children_info ||= {} end |
.defined_collections ⇒ Object
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_a ⇒ Object
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 |