Class: Seiten::PageCollection

Inherits:
Object
  • Object
show all
Defined in:
lib/seiten/page_collection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ PageCollection

Returns a new instance of PageCollection.



5
6
7
8
# File 'lib/seiten/page_collection.rb', line 5

def initialize(options = {})
  @navigation_id = options[:navigation_id]
  @pages         = options[:pages] || []
end

Instance Attribute Details

Returns the value of attribute navigation_id.



3
4
5
# File 'lib/seiten/page_collection.rb', line 3

def navigation_id
  @navigation_id
end

#pagesObject

Returns the value of attribute pages.



3
4
5
# File 'lib/seiten/page_collection.rb', line 3

def pages
  @pages
end

Instance Method Details

#allObject



18
19
20
# File 'lib/seiten/page_collection.rb', line 18

def all
  pages.to_a
end

#build(options = {}) ⇒ Object



14
15
16
# File 'lib/seiten/page_collection.rb', line 14

def build(options = {})
  Seiten::PageCollectionBuilder.call(self, options)
end

#find(id) ⇒ Object



22
23
24
# File 'lib/seiten/page_collection.rb', line 22

def find(id)
  find_by(id: id)
end

#find_by(params) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/seiten/page_collection.rb', line 26

def find_by(params)
  @find_by ||= {}
  return @find_by[params] if @find_by.key?(params)

  @find_by[params] = pages.find do |page|
    params.all? do |k, v|
      page.send(k) == v
    end
  end
end


10
11
12
# File 'lib/seiten/page_collection.rb', line 10

def navigation
  Seiten::Navigation.find_by(id: navigation_id)
end

#new(params = {}) ⇒ Object



48
49
50
51
52
# File 'lib/seiten/page_collection.rb', line 48

def new(params = {})
  page = Seiten::Page.new(params.merge(navigation_id: navigation_id))
  pages << page
  page
end

#where(params) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/seiten/page_collection.rb', line 37

def where(params)
  @where ||= {}
  return @where[params] if @where.key?(params)

  @where[params] = pages.select do |page|
    params.all? do |k, v|
      page.send(k) == v
    end
  end
end