Class: Sitepress::Models::Collection

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/sitepress/models/collection.rb

Overview

Everything needed to iterate over a set of resources from a glob and wrap them in a model so they are returned as a sensible enumerable.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model:, &resources) ⇒ Collection

Returns a new instance of Collection.



10
11
12
13
# File 'lib/sitepress/models/collection.rb', line 10

def initialize(model:, &resources)
  @model = model
  @resources = resources
end

Instance Attribute Details

#modelObject (readonly)

Returns the value of attribute model.



8
9
10
# File 'lib/sitepress/models/collection.rb', line 8

def model
  @model
end

Instance Method Details

#each(&block) ⇒ Object

Wraps each resource in a model object.



20
21
22
23
24
25
26
# File 'lib/sitepress/models/collection.rb', line 20

def each(&block)
  return to_enum(:each) unless block_given?

  resources.each do |resource|
    yield model.new(resource)
  end
end

#resourcesObject



15
16
17
# File 'lib/sitepress/models/collection.rb', line 15

def resources
  @resources.call
end