Class: Statics::Collection
- Inherits:
-
Object
- Object
- Statics::Collection
- Extended by:
- Forwardable
- Includes:
- Enumerable
- Defined in:
- lib/statics/collection.rb
Instance Method Summary collapse
- #each(&block) ⇒ Statics::Collection
- #find_by(conditions) ⇒ Statics::Model
-
#initialize(records) ⇒ Collection
constructor
A new instance of Collection.
- #keys ⇒ Array<Symbol>
- #pluck(*attributes) ⇒ Array<Object>
- #reject(&block) ⇒ Statics::Collection
- #select(&block) ⇒ Statics::Collection
- #where(conditions) ⇒ Statics::Collection
- #where_not(conditions) ⇒ Statics::Collection
Constructor Details
#initialize(records) ⇒ Collection
Returns a new instance of Collection.
12 13 14 |
# File 'lib/statics/collection.rb', line 12 def initialize(records) @records = records end |
Instance Method Details
#each(&block) ⇒ Statics::Collection
17 18 19 |
# File 'lib/statics/collection.rb', line 17 def each(&block) self.class.new(records.each(&block)) end |
#find_by(conditions) ⇒ Statics::Model
45 46 47 |
# File 'lib/statics/collection.rb', line 45 def find_by(conditions) find { |record| filter?(record, conditions) } end |
#keys ⇒ Array<Symbol>
50 51 52 |
# File 'lib/statics/collection.rb', line 50 def keys pluck(:key) end |
#pluck(*attributes) ⇒ Array<Object>
56 57 58 59 |
# File 'lib/statics/collection.rb', line 56 def pluck(*attributes) map { |record| record.attributes.slice(*attributes).values } .tap { |result| result.flatten! if attributes.size == 1 } end |
#reject(&block) ⇒ Statics::Collection
27 28 29 |
# File 'lib/statics/collection.rb', line 27 def reject(&block) self.class.new(records.reject(&block)) end |
#select(&block) ⇒ Statics::Collection
22 23 24 |
# File 'lib/statics/collection.rb', line 22 def select(&block) self.class.new(records.select(&block)) end |
#where(conditions) ⇒ Statics::Collection
33 34 35 |
# File 'lib/statics/collection.rb', line 33 def where(conditions) select { |record| filter?(record, conditions) } end |
#where_not(conditions) ⇒ Statics::Collection
39 40 41 |
# File 'lib/statics/collection.rb', line 39 def where_not(conditions) reject { |record| filter?(record, conditions) } end |