Class: Yuzu::Generators::CatalogPaginator

Inherits:
Object
  • Object
show all
Defined in:
lib/yuzu/generators/paginate.rb

Instance Method Summary collapse

Constructor Details

#initialize(website_file, catalog_generator) ⇒ CatalogPaginator

Determine whether each catalog requires pagination. Produce the new WebsiteFile objects by replacing each INSERTCATALOG(…) with a new one. Add the new WebsiteFile objects to the parent.



51
52
53
54
# File 'lib/yuzu/generators/paginate.rb', line 51

def initialize(website_file, catalog_generator)
  @website_file = website_file
  @catalog_generator = catalog_generator
end

Instance Method Details

#add_file(paginated_file) ⇒ Object

Takes the PaginatedWebsiteFile instance and adds it as a sibling to the root WebsiteFile being paginated.

Parameters:

Returns:

  • nothing



118
119
120
121
122
# File 'lib/yuzu/generators/paginate.rb', line 118

def add_file(paginated_file)
  container = @website_file.parent
  container.append_child(paginated_file)
  stash_paginated_file(paginated_file)
end

#catalogs_from_args(catalog_args) ⇒ Array

Return an Array of Catalog instances from the given user-specified catalog arguments.

Parameters:

  • catalog_args (Array)

    An Array of Hashes containing the arguments gathered from the INSERTCATALOG directives of the page.

Returns:

  • (Array)

    An Array of corresponding Catalog objects.



74
75
76
# File 'lib/yuzu/generators/paginate.rb', line 74

def catalogs_from_args(catalog_args)
  catalog_args.collect {|args| Yuzu::Filters.catalog_for(@website_file, args)}
end

#generate!Object



56
57
58
59
60
# File 'lib/yuzu/generators/paginate.rb', line 56

def generate!
  catalog_args = @website_file.raw_contents.scan(@catalog_generator.regex).flatten
  catalogs = catalogs_from_args(catalog_args)
  generate_files_from_catalogs!(catalogs)
end

#generate_files_for_paginating_catalog!(catalog) ⇒ Object

Given a Catalog instance, check to see if it requires pagination and generate the appropriate files for the given content.

Parameters:

  • catalog (Catalog)

    The Yuzu::Generator.Catalog object that represents the contents to insert into the page.

Returns:

  • nothing



84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/yuzu/generators/paginate.rb', line 84

def generate_files_for_paginating_catalog!(catalog)
  return if catalog.nil?

  num_pages = catalog.num_pages
  return if num_pages == 1

  # Don't generate the first page, it's all ready to go.
  num_pages_to_generate = num_pages - 1
  num_pages_to_generate.times do |page|
    generate_page!(page + 2, catalog)
  end
end

#generate_files_from_catalogs!(catalogs) ⇒ Object



62
63
64
65
66
67
# File 'lib/yuzu/generators/paginate.rb', line 62

def generate_files_from_catalogs!(catalogs)
  paginating_catalogs = catalogs.select {|cat| cat.should_paginate?}
  first_paginating_catalog = paginating_catalogs.empty? ? nil : paginating_catalogs[0]

  generate_files_for_paginating_catalog!(first_paginating_catalog)
end

#generate_page!(page, catalog) ⇒ Object

Add a single PaginatedWebsiteFile instance and add it to the parent of the given WebsiteFile as a sibling for the specified page of the given Catalog.

Parameters:

  • page (Fixnum)

    The page to generate, indexed by 1.

  • catalog (Catalog)

    The Catalog being paginated.

Returns:

  • nothing



103
104
105
106
107
108
109
110
111
# File 'lib/yuzu/generators/paginate.rb', line 103

def generate_page!(page, catalog)
  original_directive = catalog.original_directive
  directive_for_page = catalog.directive_for_page(page)

  new_raw_contents = @website_file.raw_contents.sub(original_directive, directive_for_page)

  paginated_file = Yuzu::Core::PaginatedWebsiteFile.new(@website_file, new_raw_contents, page)
  add_file(paginated_file)
end

#stash_paginated_file(paginated_file) ⇒ Object

Registers the generated file in the seed file’s stash, enabling us to generate pagination links and other niceties later.

Parameters:

Returns:

  • nothing



129
130
131
132
# File 'lib/yuzu/generators/paginate.rb', line 129

def stash_paginated_file(paginated_file)
  paginated_siblings = @website_file.stash[:paginated_siblings] || []
  @website_file.stash(:paginated_siblings => paginated_siblings + [paginated_file])
end