Class: Jets::Cfn::Builders::PageBuilder

Inherits:
Object
  • Object
show all
Extended by:
Memoist
Defined in:
lib/jets/cfn/builders/page_builder.rb

Instance Method Summary collapse

Instance Method Details

#buildObject

Build page slices



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/jets/cfn/builders/page_builder.rb', line 7

def build
  map = build_map
  pages = []
  map.each do |path, existing_page|
    if existing_page
      pages[existing_page] ||= []
      pages[existing_page] << path
    end
  end

  # Remove existing paths from map. Leave behind new paths
  pages.each do |page|
    page.each do |i|
      map.delete(i)
    end
  end

  # Fill up available space in each page so all existing pages are full
  keys = map.keys
  pages.each do |page|
    break if keys.empty?
    while page.size < page_limit
      path = keys.shift
      break if path.nil?
      page << path
    end
  end

  # Add remaining slices to new additional pages
  pages += keys.each_slice(page_limit).to_a

  @@pages = pages

  pages
end

#build_mapObject

Build map that has paths as keys and page number as value Example: “a2”=>0, “b1”=>1, “b2”=>1, “c1”=>2, “c2”=>2



45
46
47
48
49
50
51
# File 'lib/jets/cfn/builders/page_builder.rb', line 45

def build_map
  map = {}
  new_paths.each do |path|
     map[path] = find_page_index(path)
  end
  map
end

#find_page_index(new_path) ⇒ Object



53
54
55
56
57
58
59
60
61
# File 'lib/jets/cfn/builders/page_builder.rb', line 53

def find_page_index(new_path)
  pages = old_pages || []
  pages.each_with_index do |slice, i|
    slice.find do |old_path|
      return i if old_path == new_path
    end
  end
  nil
end

#new_pathsObject



69
70
71
# File 'lib/jets/cfn/builders/page_builder.rb', line 69

def new_paths
  Jets::Router.all_paths.reject { |p| p == '' }
end

#old_pagesObject



63
64
65
66
# File 'lib/jets/cfn/builders/page_builder.rb', line 63

def old_pages
  state = Jets::Router::State.new
  state.load("pages")
end

#page_limitObject

Relevant is CloudFormation Outputs limit is 200 JETS_APIGW_PAGE_LIMIT is based on that docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cloudformation-limits.html



76
77
78
# File 'lib/jets/cfn/builders/page_builder.rb', line 76

def page_limit
  Integer(ENV['JETS_APIGW_PAGE_LIMIT'] || 200)
end