Class: Hubkit::ChainableCollection Abstract
- Inherits:
-
Object
- Object
- Hubkit::ChainableCollection
- Defined in:
- lib/hubkit/chainable_collection.rb
Overview
A class which wraps an array (or Enumerable) and provides convenience
methods for chainable filters, e.g.:
Direct Known Subclasses
Class Method Summary collapse
-
.scope(name) {|...| ... } ⇒ Object
Allows definition of new chainable filters within the class definition.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Returns true if the other collection contains the same elements.
-
#initialize(inner) ⇒ ChainableCollection
constructor
Create a new ChainableCollection.
-
#method_missing(name, *args) {|...| ... } ⇒ Object
Call into the wrapped collection if a method has not been implemented on the ChainableCollection.
-
#not ⇒ NotCollection
Returns a collection which will contain all elements which are contained in this ChainableCollection, but NOT matching any additional chained filters.
-
#respond_to?(name, include_all = false) ⇒ Boolean
Check if a method is implemented by either this method or the wrapped collection.
-
#select {|item| ... } ⇒ Object
Return a Hubkit::ChainableCollection containing all members for which the block is true.
-
#wrap(items) ⇒ Object
Return a collection of the same type as
self, containingitems.
Constructor Details
#initialize(inner) ⇒ ChainableCollection
Create a new ChainableCollection
23 24 25 |
# File 'lib/hubkit/chainable_collection.rb', line 23 def initialize(inner) @inner = inner end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args) {|...| ... } ⇒ Object
Call into the wrapped collection if a method has not been implemented on the ChainableCollection
60 61 62 63 |
# File 'lib/hubkit/chainable_collection.rb', line 60 def method_missing(name, *args, &block) return super unless @inner.respond_to?(name) @inner.send(name, *args, &block) end |
Class Method Details
.scope(name) {|...| ... } ⇒ Object
Allows definition of new chainable filters within the class definition
12 13 14 15 16 17 18 |
# File 'lib/hubkit/chainable_collection.rb', line 12 def self.scope(name, &block) define_method name do |*args| wrap( instance_exec(*args, &block), ) end end |
Instance Method Details
#==(other) ⇒ Boolean
Returns true if the other collection contains the same elements
79 80 81 |
# File 'lib/hubkit/chainable_collection.rb', line 79 def ==(other) other == self.to_a end |
#not ⇒ NotCollection
Returns a collection which will contain all elements which are contained in this ChainableCollection, but NOT matching any additional chained filters
71 72 73 |
# File 'lib/hubkit/chainable_collection.rb', line 71 def not NotCollection.new(self) end |
#respond_to?(name, include_all = false) ⇒ Boolean
Check if a method is implemented by either this method or the wrapped collection
49 50 51 |
# File 'lib/hubkit/chainable_collection.rb', line 49 def respond_to?(name, include_all = false) super || @inner.respond_to?(name) end |
#select {|item| ... } ⇒ Object
Return a Hubkit::ChainableCollection containing all members for which the block is true. This new Hubkit::ChainableCollection will also be filterable in the same way.
31 32 33 34 |
# File 'lib/hubkit/chainable_collection.rb', line 31 def select(&block) return wrap(@inner.select &block) if block_given? wrap(@inner.select) end |
#wrap(items) ⇒ Object
Return a collection of the same type as self, containing items
39 40 41 |
# File 'lib/hubkit/chainable_collection.rb', line 39 def wrap(items) self.class.new(items) end |