Class: ActiveCollection::Base
- Inherits:
-
Object
- Object
- ActiveCollection::Base
- Includes:
- Enumerable
- Defined in:
- lib/active_collection/base.rb
Instance Attribute Summary collapse
-
#params ⇒ Object
readonly
Returns the value of attribute params.
Instance Method Summary collapse
-
#===(other) ⇒ Object
Forwards
===
explicitly to the collection because the instance method removal above doesn’t catch it. -
#each(&block) ⇒ Object
Implements Enumerable.
-
#empty? ⇒ Boolean
The emptiness of the collection (limited by query and pagination).
-
#initialize(params = {}) ⇒ Base
constructor
Create a Collection by passing the important query params from the controller.
-
#length ⇒ Object
The size of the collection (limited by query and pagination).
-
#loaded? ⇒ Boolean
true if the collection data has been loaded.
- #proxy_respond_to? ⇒ Object
-
#respond_to?(*args) ⇒ Boolean
Does the ActiveCollection or it’s target collection respond to method?.
- #send(method, *args) ⇒ Object
-
#size ⇒ Object
The size of the collection (limited by query and pagination).
-
#total_entries ⇒ Object
Always returns the total count of all records that can be in this collection.
- #unload! ⇒ Object
-
#unloading_dup {|d| ... } ⇒ Object
dup that doesn’t include the collection if it’s loaded.
Constructor Details
#initialize(params = {}) ⇒ Base
Create a Collection by passing the important query params from the controller.
Example:
BeerCollection.new(params.only("q","page"))
If any :page parameter is passed, nil or not, the assumption will be that the collection should be a paged collection and the current_page will default to 1.
29 30 31 |
# File 'lib/active_collection/base.rb', line 29 def initialize(params = {}) @params = params.symbolize_keys end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args) ⇒ Object (protected)
Pass methods on to the collection.
105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/active_collection/base.rb', line 105 def method_missing(method, *args) if Array.method_defined?(method) && !Object.method_defined?(method) if block_given? collection.send(method, *args) { |*block_args| yield(*block_args) } else collection.send(method, *args) end else super #message = "undefined method `#{method.to_s}' for \"#{collection}\":#{collection.class.to_s}" #raise NoMethodError, message end end |
Instance Attribute Details
#params ⇒ Object (readonly)
Returns the value of attribute params.
17 18 19 |
# File 'lib/active_collection/base.rb', line 17 def params @params end |
Instance Method Details
#===(other) ⇒ Object
Forwards ===
explicitly to the collection because the instance method removal above doesn’t catch it. Loads the collection if needed.
42 43 44 |
# File 'lib/active_collection/base.rb', line 42 def ===(other) other === collection end |
#each(&block) ⇒ Object
Implements Enumerable
63 64 65 |
# File 'lib/active_collection/base.rb', line 63 def each(&block) collection.each(&block) end |
#empty? ⇒ Boolean
The emptiness of the collection (limited by query and pagination)
68 69 70 |
# File 'lib/active_collection/base.rb', line 68 def empty? size.zero? end |
#length ⇒ Object
The size of the collection (limited by query and pagination)
Similar to ActiveRecord associations, length will always load the collection.
89 90 91 |
# File 'lib/active_collection/base.rb', line 89 def length collection.size end |
#loaded? ⇒ Boolean
true if the collection data has been loaded
94 95 96 |
# File 'lib/active_collection/base.rb', line 94 def loaded? !!@collection end |
#proxy_respond_to? ⇒ Object
33 |
# File 'lib/active_collection/base.rb', line 33 alias_method :proxy_respond_to?, :respond_to? |
#respond_to?(*args) ⇒ Boolean
Does the ActiveCollection or it’s target collection respond to method?
36 37 38 |
# File 'lib/active_collection/base.rb', line 36 def respond_to?(*args) proxy_respond_to?(*args) || collection.respond_to?(*args) end |
#send(method, *args) ⇒ Object
46 47 48 49 50 51 52 |
# File 'lib/active_collection/base.rb', line 46 def send(method, *args) if proxy_respond_to?(method) super else collection.send(method, *args) end end |
#size ⇒ Object
The size of the collection (limited by query and pagination)
It will avoid using a count query if the collection is already loaded.
(Note that the paginated count is added in the pagination module)
77 78 79 |
# File 'lib/active_collection/base.rb', line 77 def size loaded?? length : total_entries end |
#total_entries ⇒ Object
Always returns the total count of all records that can be in this collection.
82 83 84 |
# File 'lib/active_collection/base.rb', line 82 def total_entries @total_entries ||= load_count end |
#unload! ⇒ Object
98 99 100 |
# File 'lib/active_collection/base.rb', line 98 def unload! @collection = nil end |
#unloading_dup {|d| ... } ⇒ Object
dup that doesn’t include the collection if it’s loaded
55 56 57 58 59 60 |
# File 'lib/active_collection/base.rb', line 55 def unloading_dup d = dup d.unload! yield d if block_given? d end |